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=None# Insertion operation - Add your code heredefinsert(self,data):#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 →
Python template