コード例 #1
0
 def test_delete_not_in_the_tree(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     tree.add_child(2)
     tree.add_child(7)
     self.assertEqual(tree.display_preorder(), [5, 2, 7])
     tree.delete(6)
     self.assertEqual(tree.display_preorder(), [5, 2, 7])
コード例 #2
0
 def test_delete_root_node(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     tree.add_child(4)
     tree.add_child(6)
     tree.delete(5)
     self.assertEqual(tree.display_preorder(), [6, 4])
コード例 #3
0
 def test_get_parent(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     tree.add_child(1)
     tree.add_child(4)
     tree.add_child(7)
     tree.add_child(9)
     tree.add_child(6)
     self.assertEqual(tree.get_parent(7), 5)
コード例 #4
0
 def test_find_not_in_tree(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     tree.add_child(1)
     tree.add_child(4)
     tree.add_child(7)
     tree.add_child(9)
     tree.add_child(6)
     self.assertEqual(tree.find(8), False)
     self.assertEqual(tree.find(2), False)
コード例 #5
0
 def test_find_in_tree(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     tree.add_child(1)
     tree.add_child(4)
     tree.add_child(7)
     tree.add_child(9)
     tree.add_child(6)
     self.assertEqual(tree.find(4), True)
     self.assertEqual(tree.find(6), True)
コード例 #6
0
ファイル: splay_tree.py プロジェクト: cssubedi/DataStructures
 def __init__(self):
     BinarySearchTree.__init__(self)
コード例 #7
0
 def test_display_postorder(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     tree.add_child(2)
     tree.add_child(1)
     tree.add_child(4)
     tree.add_child(7)
     tree.add_child(9)
     tree.add_child(6)
     tree.add_child(0)
     tree.add_child(8)
     tree.add_child(3)
     self.assertEqual(tree.display_postorder(),
                      [5, 7, 9, 8, 6, 2, 4, 3, 1, 0])
コード例 #8
0
 def test_empty_tree(self):
     tree = BinarySearchTree()
     self.assertIsNone(tree.root)
     self.assertIsNone(tree.display_preorder())
コード例 #9
0
 def test_display_inorder(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     tree.add_child(2)
     tree.add_child(1)
     tree.add_child(4)
     tree.add_child(7)
     tree.add_child(9)
     tree.add_child(6)
     tree.add_child(0)
     tree.add_child(8)
     tree.add_child(3)
     self.assertEqual(tree.display_inorder(),
                      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
コード例 #10
0
 def test_tree_add_child_already_in_tree(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     tree.add_child(2)
     tree.add_child(4)
     self.assertEqual(tree.add_child(2), False)
コード例 #11
0
 def test_tree_add_children(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     tree.add_child(2)
     tree.add_child(4)
     self.assertEqual(tree.display_preorder()[1:], [2, 4])
コード例 #12
0
 def test_add_child(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     self.assertEqual(tree.add_child(2), True)
コード例 #13
0
 def test_delete_node_with_children(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     tree.add_child(1)
     tree.add_child(4)
     tree.add_child(7)
     tree.add_child(9)
     tree.add_child(6)
     tree.add_child(8)
     self.assertEqual(tree.display_preorder(), [5, 1, 4, 7, 6, 9, 8])
     tree.delete(7)
     self.assertEqual(tree.display_preorder(), [5, 1, 4, 8, 6, 9])
     tree.delete(8)
     self.assertEqual(tree.display_preorder(), [5, 1, 4, 9, 6])
コード例 #14
0
 def test_tree_root(self):
     tree = BinarySearchTree()
     tree.add_child(5)
     self.assertEqual(tree.root.value, 5)