美文网首页
Swift 5.3 数据结构 —— 二叉搜索树 BinarySe

Swift 5.3 数据结构 —— 二叉搜索树 BinarySe

作者: Sunooo | 来源:发表于2020-11-08 23:36 被阅读0次

二叉搜索树 BinarySearchTree

二叉搜索树是二叉树的一种,特点是左节点小于本身,本身小于右节点
leftChild.value < value < rightChild.value

1.二叉搜索树定义

因为要保持自身的特性,所以root是只读属性,二叉树里的元素必须遵守Comparable协议

public struct BinarySearchTree<Element: Comparable> {
    public private(set) var root: BinaryNode<Element>?
    public init() {}
}

2.插入方法

根据二叉搜索本身的特性,插入和搜索的速度都是很快的,小的向左,大的向右

extension BinarySearchTree {
    public mutating func insert(_ value: Element) {
        root = insert(from: root, value: value)
    }
    
    private mutating func insert(from node: BinaryNode<Element>?, value: Element) -> BinaryNode<Element> {
        guard let node = node else {
            return BinaryNode(value: value)
        }
        
        if value < node.value {
            node.leftChild = insert(from: node.leftChild, value: value)
        } else {
            node.rightChild = insert(from: node.rightChild, value: value)
        }
        
        return node
    }
}

3. 查询方法

常规的二叉树查询方法,是依次遍历每一个节点,二叉搜索树可以根据比较大小更快的完场查找遍历

public extension BinarySearchTree {
//    func contains(_ value: Element) -> Bool {
//        guard let root = root else {
//            return false
//        }
//
//        var found = false
//        root.traverseInOrder {
//            if $0 == value {
//                found = true
//            }
//        }
//        return found
//    }
    
    func contains(_ value: Element) -> Bool {
        var current = root
        while let node = current {
            if node.value == value {
                return true
            }
        
            if value < node.value {
                current = node.leftChild
            } else {
                current = node.rightChild
            }
        }
        return false
    }
}

4. 删除方法

删除节点的方法,要考虑三种情况
1)左右节点都是nil,可以直接删除
2)左节点或者右节点为nil,删除的时候,需要把子节点跟父节点对接
3)左右节点都不是nil,需要拿到右节点的最小值替换到被删除的节点,然后在右节点移除最小值

extension BinarySearchTree {
    public mutating func remove(_ value: Element) {
        
    }
    
    private func remove(node: BinaryNode<Element>?, value: Element) -> BinaryNode<Element>? {
        guard let node = node else {
            return nil
        }
        if value == node.value {
            if node.leftChild == nil && node.rightChild == nil {
                return nil
            }
            if node.leftChild == nil {
                return node.rightChild
            }
            if node.rightChild == nil {
                return node.leftChild
            }
            node.value = node.rightChild!.min.value
            node.rightChild = remove(node: node.rightChild, value: node.value)
            
        } else if value < node.value {
            node.leftChild = remove(node: node.leftChild, value: value)
        } else {
            node.rightChild = remove(node: node.rightChild, value: value)
        }
        
        return node
    }
}

github仓库地址 https://github.com/SunZhiC/DataStructuresInSwift

References

Data Structures & Algorithms in Swift by Matthijs Hollemans.
https://github.com/apple/swift-algorithms
https://github.com/raywenderlich/swift-algorithm-club

相关文章

  • Swift 5.3 数据结构 —— 二叉搜索树 BinarySe

    二叉搜索树 BinarySearchTree 二叉搜索树是二叉树的一种,特点是左节点小于本身,本身小于右节点lef...

  • Go语言数据结构和算法-BinarySearchTree(二叉搜

    Go语言数据结构和算法-BinarySearchTree(二叉搜索树) 二叉搜索树的数据结构 Insert(val...

  • 数据结构-二叉搜索树

    数据结构-二叉搜索树(BST) 定义:二叉搜索树是一个节点类的二叉树数据结构,包含以下特点: -左子树仅包含比根节...

  • Swift实现搜索二叉树(BST)

    Swift实现搜索二叉树(BST) 二叉搜索树(BST)关于索索二叉树这里有详细的教程,下面我们主要针对二叉树的一...

  • 二叉树

    参考文章 百度 数据结构中有很多树的结构,其中包括二叉树、二叉搜索树、2-3树、红黑树等等。本文中对数据结构中常见...

  • Day3--搜索树

    在二叉判定树以及二分搜索的基础上,基于二叉树的搜索树产生,该数据结构可以随时进行调整,可以保证高效的搜索效率。 内...

  • 【学习】数据结构和算法

    数据结构 线性表:数组 栈 队列 链表树:二叉树 二叉树遍历 二叉搜索树 B树 红黑树 堆图:图其他:哈希表...

  • Go:实现二叉搜索树(BST)

    二叉搜索树是一种支持快速查找、增加和删除元素的数据结构。二叉搜索树中的节点是有序的,因此支持快速查找。该数据结构由...

  • 如何在Java中实现二叉搜索树

    二叉搜索树或BST是一种流行的数据结构,用于保持元素的顺序。二叉搜索树是二叉树,其中左子节点的值小于或等于父节点,...

  • 二叉排序树

    注 关于二叉搜索树更为详细的解释请详看 《大话数据结构》第八章 查找 中二叉搜索树这一小节 二叉排序树(Binar...

网友评论

      本文标题:Swift 5.3 数据结构 —— 二叉搜索树 BinarySe

      本文链接:https://www.haomeiwen.com/subject/ghrkbktx.html