Пример #1
0
from Trees.BinaryTree import BinaryTree, Node

_example0 = BinaryTree()

_example1 = BinaryTree()
_example1.root = Node(1)
_example1.root.left = Node(2)
_example1.root.left.left = Node(4)
_example1.root.left.right = Node(5)
_example1.root.right = Node(3)

_example2 = BinaryTree()
_example2.root = Node(0)
_example2.root.left = Node(1)
_example2.root.left.left = Node(2)
_example2.root.left.left.left = Node(3)
_example2.root.left.left.left.left = Node(4)
_example2.root.left.left.left.left.left = Node(5)
_example2.root.left.left.left.left.left.left = Node(6)
_example2.root.left.left.left.left.left.right = Node(7)
_example2.root.left.left.right = Node(8)
_example2.root.left.left.right.left = Node(9)
_example2.root.left.left.right.right = Node(10)
_example2.root.left.right = Node(11)
_example2.root.right = Node(12)
_example2.root.right.right = Node(13)
_example2.root.right.right.right = Node(14)

_example3 = BinaryTree()
_example3.root = Node(0)
_example3.root.left = Node(1)
Пример #2
0
        if current.val >= val:
            if current.left is None:
                current.left = TreeNode(val)
                return
            return self.insertRecur(current.left, val)
        else:
            if current.right is None:
                current.right = TreeNode(val)
                return
            return self.insertRecur(current.right, val)

    def insert(self, val):
        if self.root is None:
            self.root = TreeNode(val)
        self.insertRecur(self.root, val)

if __name__ == "__main__":
    s = BinarySearchTree()
    s.root = s.create_bst()
    s.insert(9)
    s.insert(17)
    s.insert(50)
    s.insert(60)
    s.insert(70)
    b = BinaryTree()
    b.root = s.root
    print(b)
    print(s.search(60))
    print(s.search(8))
    print(s.search(90))