from BaseClass import BTree, BST bTree = BTree() bs = BST() bTree.insertL(bTree.root, 2) bTree.insertL(bTree.root, 3) bTree.insertR(bTree.root, 4) bTree.insertL(bTree.root, 5) bTree.insertL(bTree.root, 6) bTree.insertR(bTree.root, 7) for i in range(10): bs.ins(bs.root, i) def inorder(x): if x == None: return if x.left != None: inorder(x.left) print x if x.right != None: inorder(x.right) def preorder(x): if x == None: return print x if x.left != None: preorder(x.left) elif x.right != None:
def FindDia(x, H): lh = Height() rh = Height() if x == None: H.h = 0 return 0 lh.h += 1 rh.h += 1 ld = FindDia(x.left, lh) rd = FindDia(x.right, rh) H.h = max(lh.h, rh.h) + 1 return max(lh.h + rh.h + 1, max(ld, rd)) a = BTree() a.insertL(a.root, 9) a.insertL(a.root, 8) a.insertL(a.root, 7) a.insertL(a.root, 6) a.insertL(a.root, 5) a.insertL(a.root, 4) a.insertL(a.root, 3) a.insertL(a.root, 2) a.insertL(a.root, 1) a.insertR(a.root, 0) a.insertR(a.root, 15) a.insertR(a.root, 11) a.insertR(a.root, 14) s = Height()
from BaseClass import BTree, BST bTree = BTree() bs = BST() bTree.insertL(bTree.root,2) bTree.insertL(bTree.root,3) bTree.insertR(bTree.root,4) bTree.insertL(bTree.root,5) bTree.insertL(bTree.root,6) bTree.insertR(bTree.root,7) for i in range(10): bs.ins(bs.root,i) def inorder(x): if x==None: return if x.left != None: inorder(x.left) print x if x.right != None: inorder(x.right) def preorder(x): if x==None: return print x if x.left !=None: preorder(x.left) elif x.right!=None: preorder(x.right) def postorder(x):
def __init__(self): self.h =0 def FindDia(x,H): lh =Height() rh =Height() if x==None: H.h =0 return 0 lh.h +=1 rh.h +=1 ld = FindDia(x.left,lh) rd = FindDia(x.right,rh) H.h =max(lh.h,rh.h)+1 return max(lh.h + rh.h + 1, max(ld,rd)) a = BTree() a.insertL(a.root,9) a.insertL(a.root,8) a.insertL(a.root,7) a.insertL(a.root,6) a.insertL(a.root,5) a.insertL(a.root,4) a.insertL(a.root,3) a.insertL(a.root,2) a.insertL(a.root,1) a.insertR(a.root,0) a.insertR(a.root,15) a.insertR(a.root,11) a.insertR(a.root,14) s = Height()