def test_delete_root_leaf(self): self.assertEqual(1, self.tree.Count()) self.tree.DeleteNodeByKey(1) self.assertEqual(None, self.tree.Root) self.assertEqual(0, self.tree.Count()) self.tree.AddKeyValue(1, 'key1') self.tree.AddKeyValue(-5, 'key-5') self.tree.AddKeyValue(-2, 'key-2') self.tree.AddKeyValue(-6, 'key-6') self.assertEqual(4, self.tree.Count()) self.tree.DeleteNodeByKey(1) self.assertEqual(3, self.tree.Count()) self.assertEqual(-5, self.tree.Root.NodeKey) self.assertEqual(-2, self.tree.Root.RightChild.NodeKey) self.assertEqual(-6, self.tree.Root.LeftChild.NodeKey) self.tree = BST(BSTNode(1, 'key1', None)) self.tree.AddKeyValue(12, 'key12') self.tree.AddKeyValue(0, 'key0') self.tree.AddKeyValue(11, 'key11') self.tree.AddKeyValue(16, 'key16') self.tree.AddKeyValue(13, 'key13') self.tree.AddKeyValue(20, 'key20') self.tree.AddKeyValue(8, 'key8') self.assertEqual(8, self.tree.Count()) self.tree.DeleteNodeByKey(1) self.assertEqual(7, self.tree.Count())
def test_another_one_deleting(self): self.tree = BST(BSTNode(50, 'key50', None)) self.tree.AddKeyValue(20, 'key20') self.tree.AddKeyValue(70, 'key70') self.tree.AddKeyValue(10, 'key10') self.tree.AddKeyValue(40, 'key40') self.tree.AddKeyValue(60, 'key60') self.tree.AddKeyValue(90, 'key90') self.tree.AddKeyValue(30, 'key30') self.tree.AddKeyValue(35, 'key30') self.tree.AddKeyValue(45, 'key45') self.assertEqual(10, self.tree.Count()) self.tree.DeleteNodeByKey(10) self.assertEqual(9, self.tree.Count())
def test_delete_node_with_both_child_no_right_leaf(self): self.tree = BST(BSTNode(50, 'key50', None)) self.tree.AddKeyValue(20, 'key20') self.tree.AddKeyValue(70, 'key70') self.tree.AddKeyValue(10, 'key10') self.tree.AddKeyValue(40, 'key40') self.tree.AddKeyValue(60, 'key60') self.tree.AddKeyValue(90, 'key90') self.tree.AddKeyValue(30, 'key30') self.tree.AddKeyValue(45, 'key45') self.assertEqual(9, self.tree.Count()) self.tree.DeleteNodeByKey(20) self.assertEqual(8, self.tree.Count()) self.assertEqual(None, self.tree.FindNodeByKey(40).Node.LeftChild) self.assertEqual(45, self.tree.FindNodeByKey(40).Node.RightChild.NodeKey)
def setUp(self): # 50 # / \ # 35 55 # / \ \ # 30 40 60 # / \ / # 37 45 57 self.parent_node = BSTNode(50, 'key50', None) self.tree = BST(self.parent_node) self.tree.AddKeyValue(35, 'key35') self.tree.AddKeyValue(55, 'key55') self.tree.AddKeyValue(30, 'key30') self.tree.AddKeyValue(40, 'key40') self.tree.AddKeyValue(60, 'key60') self.tree.AddKeyValue(37, 'key37') self.tree.AddKeyValue(45, 'key45') self.tree.AddKeyValue(57, 'key57')
def test_delete_root_node_with_both_child(self): self.tree = BST(BSTNode(50, 'key50', None)) self.tree.AddKeyValue(20, 'key20') self.tree.AddKeyValue(70, 'key70') self.tree.AddKeyValue(10, 'key10') self.tree.AddKeyValue(40, 'key40') self.tree.AddKeyValue(60, 'key60') self.tree.AddKeyValue(90, 'key90') self.tree.AddKeyValue(30, 'key30') self.tree.AddKeyValue(35, 'key30') self.tree.AddKeyValue(45, 'key45') self.assertEqual(10, self.tree.Count()) self.tree.DeleteNodeByKey(50) self.assertEqual(9, self.tree.Count()) self.assertEqual(False, self.tree.FindNodeByKey(50).NodeHasKey) self.assertEqual(60, self.tree.Root.NodeKey) self.assertEqual(None, self.tree.Root.Parent) self.assertEqual(20, self.tree.Root.LeftChild.NodeKey) self.assertEqual(70, self.tree.Root.RightChild.NodeKey)
def setUp(self): self.parent_node = BSTNode(1, 'key1', None) self.tree = BST(self.parent_node)