class BinarySearchTreeTest(unittest.TestCase):
    def setUp(self):
        self.tree = BinarySearchTree()

    def getRoot(self):
        root = Node(7, None)
        left = root.left = Node(4, root)
        right = root.right = Node(13, root)
        left.left = Node(1, left)
        left.right = Node(6, left)
        right.left = Node(10, right)
        right.right = Node(15, right)

        return root

    def test_find(self):
        root = self.getRoot()
        self.assertEqual(7, self.tree.find(7, root).key)
        self.assertEqual(13, self.tree.find(13, root).key)
        self.assertEqual(6, self.tree.find(6, root).key)
        self.assertEqual(10, self.tree.find(10, root).key)
        self.assertEqual(1, self.tree.find(0, root).key)
        self.assertEqual(1, self.tree.find(2, root).key)
        self.assertEqual(10, self.tree.find(8, root).key)
        self.assertEqual(15, self.tree.find(16, root).key)

    def test_next(self):
        root = self.getRoot()
        self.assertEqual(4, self.tree.next(self.tree.find(1, root)).key)
        self.assertEqual(7, self.tree.next(self.tree.find(6, root)).key)
        self.assertEqual(6, self.tree.next(self.tree.find(4, root)).key)
        self.assertEqual(10, self.tree.next(self.tree.find(7, root)).key)
        self.assertIsNone(self.tree.next(self.tree.find(15, root)))

    def test_rangeSearch(self):
        root = self.getRoot()
        self.assertEqual([6, 7, 10], self.tree.rangeSearch(5, 12, root))
        self.assertEqual([1], self.tree.rangeSearch(1, 3, root))
        self.assertEqual([], self.tree.rangeSearch(16, 20, root))

    '''
                7
            4       13
         1    6   10   15
          3
    '''

    def test_insert(self):
        root = self.tree.insert(7, None)
        root = self.tree.insert(4, root)
        root = self.tree.insert(13, root)
        root = self.tree.insert(1, root)
        root = self.tree.insert(6, root)
        root = self.tree.insert(10, root)
        root = self.tree.insert(15, root)
        root = self.tree.insert(3, root)

        self.assertEqual(7, root.key)
        self.assertEqual(4, root.left.key)
        self.assertEqual(13, root.right.key)
        self.assertEqual(1, root.left.left.key)
        self.assertEqual(6, root.left.right.key)
        self.assertEqual(10, root.right.left.key)
        self.assertEqual(15, root.right.right.key)
        self.assertEqual(3, root.left.left.right.key)

        result = []
        self.tree.toString(root, result)
        self.assertEqual([1, 3, 4, 6, 7, 10, 13, 15], result)

    def test_remove(self):
        root = self.tree.insert(7, None)
        root = self.tree.insert(4, root)
        root = self.tree.insert(13, root)
        root = self.tree.insert(1, root)
        root = self.tree.insert(6, root)
        root = self.tree.insert(10, root)
        root = self.tree.insert(15, root)
        root = self.tree.insert(3, root)
        self.tree.root = root

        for i in range(8):
            self.tree.remove(self.tree.root)
            result = []
            self.tree.toString(self.tree.root, result)
            print(result)
Ejemplo n.º 2
0
Archivo: main.py Proyecto: seen2/Python
from binarySearchTree import BinarySearchTree, Node

tree = BinarySearchTree()

tree.insert(tree.root, Node(34))

tree.insert(tree.root, Node(32))
tree.insert(tree.root, Node(39))

tree.insert(tree.root, Node(30))

print(tree.search(tree.root, 32))
print(tree.search(tree.root, 34))
print(tree.search(tree.root, 30))
print(tree.search(tree.root, 39))
Ejemplo n.º 3
0
from binarySearchTree import BinarySearchTree

# using BST
binary_search_tree = BinarySearchTree()

#        3
#     /      \
#    2        5
#   / \      / \
#  1  2.5   4   6

binary_search_tree.insert(3)
binary_search_tree.insert(5)
binary_search_tree.insert(4)
binary_search_tree.insert(6)
binary_search_tree.insert(2)
binary_search_tree.insert(1)
binary_search_tree.insert(2.5)

# get maximum of our BST
print(f"get maximum of our BST: {binary_search_tree.getMaxValue()}")

# traverse BST in order, like: 1, 2, 2.5, 3, 4, 5, 6
print("traverse BST in order: ")
binary_search_tree.traverse()
Ejemplo n.º 4
0
from tree import Tree
from binarySearchTree import BinarySearchTree

t = Tree()
t.insert_root("A")
t.insert_left(t.root, "B")
t.insert_right(t.root, "C")
t.insert_left(t.root.left, "D")
t.insert_right(t.root.left, "E")
t.insert_left(t.root.right, "F")
print(t)


t2 = BinarySearchTree()
t2.insert(2)
t2.insert(5)
t2.insert(8)
t2.insert(11)
t2.insert(1)
t2.insert(7)
print(t2)
print(t2.inorder())