python - Return a string indicating what type of imbalance exists in a binary search tree -
having added new node avl. kind of rotation need fix it? need write fuction takes binary tree , returns string saying type of imbalaced exist
class node: """ node in bst. may have left , right subtrees """ def __init__(self, item, left=none, right=none): self.item = item self.left = left self.right = right class bst: """ implementation of binary search tree """ def __init__(self, lst=none): self.root = none if lst != none: x in lst: self.add(x) def recurse_add(self, ptr, item): if ptr == none: return node(item) elif item < ptr.item: ptr.left = self.recurse_add(ptr.left, item) elif item > ptr.item: ptr.right = self.recurse_add(ptr.right, item) return ptr def add(self, item): """ add item correct position on tree """ self.root = self.recurse_add(self.root, item) def r_height(self, ptr): if ptr == none: return 0 else: return 1 + max(self.r_height(ptr.left), self.r_height(ptr.right)) def height(self): return self.r_height(self.root)
Comments
Post a Comment