Posts

Showing posts with the label binary-search-tree

Python: Using recursive methods to get Binary Search Tree Height

Image
Clash Royale CLAN TAG #URR8PPP Python: Using recursive methods to get Binary Search Tree Height for my assignment, I am trying to get height of the Binary Search Tree through linear time. The prof specified that he wants us to keep track of the height in our recursive insertion and removal methods, yet mine never works during the test. The prof's requirement is: "Add an attribute to the __Node class to store the height of the subtree rooted at that node. Just before returning a node reference at the end of a recursive call, update that node's height field to be correct." My thought is, instead of making changes at a root, if the insertion or removal moves down left or right, the height increases/decrease by one (but I'm not sure how it works if there is still something left at level-n). I have attached my code over here: class Binary_Search_Tree: class __BST_Node: def __init__(self, value): self.value = value self.left=None self.right=Non...