![]() The implementation of the methods uses the pseudocode from Cormens "Introduction to Algorithms". transplant(self, u, v) replaces subtree u with subtree v and is needed as helper method when deleting nodes.delete(k) deletes the node with key k in the tree.insert(k) inserts a node with key k in the tree.The class BST implements the methods to create a binary search tree. The method _init_ initializes the class on construction, the methods to_string and print are used for testing.Ĭlass TreeNode: def _init_(self, key): self.key = key self.parent = None self.left = None self.right = None def to_string(self): return str(self.key) def print(self): print("TreeNode: %d" % (self.key)) n10 = TreeNode(10) n5 = TreeNode(5) n14 = TreeNode(14) # Create tree by linking the tree nodes n5.parent = n10 n14.parent = n10 n10.left = n5 # left child n10.right = n14 # right child print() ![]() The parent pointer is useful when deleting nodes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |