Chapter 15: Binary Search Tree Challenges

Challenge 1: Create a function that checks if a binary tree is a binary search tree.

  • 思路: 暴力法就是檢查每個node是否都符合BST的定義。

    • T: O(n), 每個node都樣檢查

extension BinaryNode where Element: Comparable {

    var isBinarySearchTree: Bool {
        return isBST(self, min: nil, max: nil)
    }

    private func isBST(_ tree: BinaryNode<Element>?, min: Element?, max: Element?) -> Bool {
        guard let tree = tree else {
            // 空tree也是BST一種
            return true
        }

        // 進行node特質檢查
        if let min = min, tree.value <= min {
            return false
        } else if let max = max, tree.value > max {
            return false
        }

        // 遞迴檢查每個node
        return isBST(tree.leftChild, min: min, max: tree.value) && isBST(tree.rightChild, min: tree.value, max: max)
    }
}

Challenge 2: The binary search tree currently lacks Equatable conformance. Your challenge is to conform adopt the Equatable protocol.

  • 思路: 兩棵樹應該有相同的數量,相同的順序。

Challenge 3: Create a method that checks if the current tree contains all the elements of another tree.

  • 思路: 另外一棵樹的所有node應該都被包含到另外的樹中

Last updated