コード例 #1
0
 def test_delete_right_heavy(self):
     sequence = [7, 3, 11, 1, 5, 9, 13, 8, 10, 12, 14]
     expected = [8, 3, 11, 1, 5, 9, 13, 10, 12, 14]
     Tree = bst.populate_tree(sequence)
     Tree.delete(7)
     tree_sequence = [n for n in Tree.breadth_first()]
     self.assertEqual(tree_sequence, expected)
コード例 #2
0
 def test_delete_balanced(self):
     sequence = [7, 3, 11, 1, 5, 9, 13, 0, 2, 4, 6, 8, 10, 12, 14]
     expected = [6, 3, 11, 1, 5, 9, 13, 0, 2, 4, 8, 10, 12, 14]
     Tree = bst.populate_tree(sequence)
     Tree.delete(7)
     tree_sequence = [n for n in Tree.breadth_first()]
     self.assertEqual(tree_sequence, expected)
コード例 #3
0
 def test_predecessor_leaf(self):
     sequence = [7, 3, 11, 1, 5, 9, 13, 0, 2, 4, 6, 5.5, 5.25, 5.75]
     expected = [6, 3, 11, 1, 5, 9, 13, 0, 2, 4, 5.5, 5.25, 5.75]
     Tree = bst.populate_tree(sequence)
     Tree.delete(7)
     tree_sequence = [n for n in Tree.breadth_first()]
     self.assertEqual(tree_sequence, expected)
コード例 #4
0
 def test_successor_children(self):
     sequence = [7, 3, 11, 1, 5, 9, 13, 8, 10, 12, 14, 8.5, 8.25, 8.75]
     expected = [8, 3, 11, 1, 5, 9, 13, 8.5, 10, 12, 14, 8.25, 8.75]
     Tree = bst.populate_tree(sequence)
     Tree.delete(7)
     tree_sequence = [n for n in Tree.breadth_first()]
     self.assertEqual(tree_sequence, expected)
コード例 #5
0
 def test_in_order(self):
     expected = [
         range(7),
         range(7),
         range(7),
         range(11)]
     for num in range(4):
         Tree = bst.populate_tree(self.tests[num])
         tree_sequence = [i for i in Tree.in_order()]
         self.assertOrder(tree_sequence, expected[num])
コード例 #6
0
 def test_breadth_first(self):
     expected = [
         [3, 1, 5, 0, 2, 4, 6],
         [5, 3, 6, 1, 4, 0, 2],
         [1, 0, 3, 2, 5, 4, 6],
         [5, 1, 9, 0, 3, 7, 10, 2, 4, 6, 8]]
     for num in range(4):
         Tree = bst.populate_tree(self.tests[num])
         tree_sequence = [i for i in Tree.breadth_first()]
         self.assertOrder(tree_sequence, expected[num])
コード例 #7
0
 def test_post_order(self):
     expected = [
         [0, 2, 1, 4, 6, 5, 3],
         [0, 2, 1, 4, 3, 6, 5],
         [0, 2, 4, 6, 5, 3, 1],
         [0, 2, 4, 3, 1, 6, 8, 7, 10, 9, 5]]
     for num in range(4):
         Tree = bst.populate_tree(self.tests[num])
         tree_sequence = [i for i in Tree.post_order()]
         self.assertOrder(tree_sequence, expected[num])
コード例 #8
0
 def test_pre_order(self):
     expected = [
         [3, 1, 0, 2, 5, 4, 6],
         [5, 3, 1, 0, 2, 4, 6],
         [1, 0, 3, 2, 5, 4, 6],
         [5, 1, 0, 3, 2, 4, 9, 7, 6, 8, 10]]
     for num in range(4):
         Tree = bst.populate_tree(self.tests[num])
         tree_sequence = [i for i in Tree.pre_order()]
         self.assertOrder(tree_sequence, expected[num])
コード例 #9
0
 def test_deletion_with_children(self):
     sequence = [3, 1, 5, 0, 2, 4, 6]
     delete_order = [1, 5]
     expected = [
         [3, 0, 5, 2, 4, 6],
         [3, 0, 4, 2, 6]
     ]
     Tree = bst.populate_tree(sequence)
     for num in xrange(2):
         Tree.delete(delete_order[num])
         tree_sequence = [n for n in Tree.breadth_first()]
         self.assertEqual(tree_sequence, expected[num])
コード例 #10
0
 def test_delete_leaves(self):
     sequence = [3, 1, 5, 0, 2, 4, 6]
     delete_order = [0, 2, 4, 6]
     expected = [
         [3, 1, 5, 2, 4, 6],
         [3, 1, 5, 4, 6],
         [3, 1, 5, 6],
         [3, 1, 5]
     ]
     Tree = bst.populate_tree(sequence)
     for num in xrange(4):
         Tree.delete(delete_order[num])
         tree_sequence = [n for n in Tree.breadth_first()]
         self.assertEqual(tree_sequence, expected[num])
コード例 #11
0
 def test_delete_single(self):
     sequence = [7]
     Tree = bst.populate_tree(sequence)
     Tree.delete(7)
     self.assertEqual(Tree.val, None)