Example #1
0
 def test_insert_list_of_elements(self):
     avl = AVL()
     avl.insert([1, 2, 3, 4])
     self.assertEqual(avl.get_key_height_inorder(), [(1, 0), (2, 2), (3, 1),
                                                     (4, 0)])
     avl.insert([1, 2, 3, 4])
     self.assertEqual(avl.get_key_height_inorder(), [(1, 0), (2, 2), (3, 1),
                                                     (4, 0)])
     avl.insert(5)
     self.assertEqual(avl.get_key_height_inorder(), [(1, 0), (2, 2), (3, 0),
                                                     (4, 1), (5, 0)])
     avl.insert([6, 7])
     self.assertEqual(avl.get_key_height_inorder(), [(1, 0), (2, 1), (3, 0),
                                                     (4, 2), (5, 0), (6, 1),
                                                     (7, 0)])
Example #2
0
 def test_delete(self):
     # GIVEN
     avl = AVL()
     # THEN deletion of node not in tree
     self.assertFalse(avl.delete(1))
     # GIVEN
     avl.insert([4, 6, 8, 2, 1])
     # WHEN delete node without children
     self.assertTrue(avl.delete(4))
     self.assertTrue(avl.delete(1))
     # THEN
     self.assertListEqual(avl.get_key_height_inorder(), [(2, 0), (6, 1),
                                                         (8, 0)])
     # GIVEN
     avl.insert([1, 4])
     # WHEN delete node without childred
     self.assertTrue(avl.delete(8))
     # THEN
     self.assertListEqual(avl.get_key_height_inorder(), [(1, 0), (2, 2),
                                                         (4, 0), (6, 1)])
     # WHEN delete node with 1 child
     self.assertTrue(avl.delete(6))
     # THEN
     self.assertListEqual(avl.get_key_height_inorder(), [(1, 0), (2, 1),
                                                         (4, 0)])
     # WHEN delete root node with 2 childred
     self.assertTrue(avl.delete(2))
     self.assertListEqual(avl.get_key_height_inorder(), [(1, 0), (4, 1)])
     # GIVEN
     avl.insert([5, 2, 0])
     self.assertListEqual(avl.get_key_height_inorder(), [(0, 0), (1, 1),
                                                         (2, 0), (4, 2),
                                                         (5, 0)])
     # WHEN delete node with 2 children
     self.assertTrue(avl.delete(1))
     # THEN
     self.assertListEqual(avl.get_key_height_inorder(), [(0, 0), (2, 1),
                                                         (4, 2), (5, 0)])
Example #3
0
 def test_insert_single_element(self):
     avl = AVL()
     avl.insert(1)
     self.assertEqual(avl.get_key_height_inorder(), [(1, 0)])
     avl.insert(1)
     self.assertEqual(avl.get_key_height_inorder(), [(1, 0)])