Function for insertion sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | class Node: def __init(self,key): self.data=key self.left=None self.right=None #Compute maxwidth among all the widths obtained def getmaxwidth(root): maxwidth=0 h=height(root) #Get width of each level and compare the width with maximum width for i in range(1,h+1): width=getwidth(root,i) if (width>maxwidth): maxwidth=width return maxwidth #get width of tree at certain level def getwidth(root,level): if root is None: return 0 if level ==1: return 1 elif level >1: return (getwidth(root.left,level+1)+getwidth(root.right,level+1)) def height(node): if node is None: return 0 else: lheight=height(node.left) rheight=height(node.right) #Use the larger one return max(lheight+1,rheight+1) #Driver program to test above function root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.right = Node(8) root.right.right.left = Node(6) root.right.right.right = Node(7) print "Maximum width is %d" %(getmaxwidth(root)) |