We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importsyslines=[]forlineinsys.stdin:if"Exit"==line.rstrip():breaklines.append(line.split("\n")[0])nol=lines[0]treeInput=[int(x)forxinlines[1].split(" ")]toIns=lines[2]# print(nol)# print(treeInput)# print(toIns)classNode:def__init__(self,data):self.data=dataself.left=Noneself.right=Noneself.height=1#Initially,newnodeshaveheight1classAVLTree:def__init__(self):self.root=Nonedefget_height(self,node):returnnode.heightifnodeelse0defcalculate_height(self,node):return1+max(self.get_height(node.left),self.get_height(node.right))defgetBalance(self,node):returnself.get_height(node.left)-self.get_height(node.right)# Insertion operation - Add your code heredefinsert(self,data):self.root=self._insert(self.root,data)def_insert(self,node,data):ifnotnode:returnNode(data)ifdata<node.data:node.left=self._insert(node.left,data)elifdata>node.data:node.right=self._insert(node.right,data)else:returnnode.height=1+max(self.get_height(node.left),self.get_height(node.right))bf=self.getBalance(node)# An assignment / return statement is needed because, when applying the rotations, the structure of the currennt node's subtree will change, pointing to the new corresponding newly "generated" subrtree.## LL rotationifbf>1anddata<node.left.data:returnself.rotate_right(node)## LR rotationifbf>1anddata>node.left.data:node.left=self.rotate_left(node.left)returnself.rotate_right(node)## RR rotationifbf<-1anddata>node.right.data:returnself.rotate_left(node)## RL rotationifbf<-1anddata<node.right.data:node.right=self.rotate_right(node.right)returnself.rotate_left(node)# A return statement is needed because, when applying the rotations, the structure of the currennt node's subtree will change, pointing to the new corresponding newly "generated" subrtree.returnnode# A return statement is needed because, when applying a rotation, the structure of the currennt node's subtree will change, pointing to the new corresponding newly "generated" subrtree.defrotate_right(self,node):left=node.leftleft_right=left.rightleft.right=nodenode.left=left_rightnode.height=self.calculate_height(node)left.height=self.calculate_height(left)returnleftdefrotate_left(self,node):right=node.rightright_left=right.leftright.left=nodenode.right=right_leftnode.height=self.calculate_height(node)right.height=self.calculate_height(right)returnright# Traversalsdefinorder(self,node):ifnodeisnotNone:self.inorder(node.left)print(f"{node.data}(BF={self.getBalance(node)})",end=" ")self.inorder(node.right)defpostorder(self,node):ifnodeisnotNone:print(f"{node.data}(BF={self.getBalance(node)})",end=" ")self.postorder(node.left)self.postorder(node.right)# Function to print BF at each nodedefprint_balance(self):self.inorder(self.root)print("")self.postorder(self.root)tree=AVLTree()forxintreeInput:tree.insert(x)tree.insert(int(toIns))tree.print_balance()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Self Balancing Tree
You are viewing a single comment's thread. Return to all comments →