if (not t1) and (not t2): return True elif (not t1) or (not t2): return False else: loopSubTree(t1,t2) def loopSubTree(t1,t2): if t1 and (t1.key==t2.key): if treeMatch(t2,t1): return True else: return False elif not t1: return False return loopSubTree(t1.leftChild,t2) or loopSubTree(t1.rightChild,t2) while(True): tree1=BinarySearchTree() user_input=input("Enter Tree1 :") for i in user_input: tree1.put(i,i) tree2=BinarySearchTree() user_input=input("Enter Tree2 :") for i in user_input: tree2.put(i,i) print loopSubTree(tree1.root,tree2.root)
if tree!=None: print ("visit",tree.key) rightHeight=False if(tree==None): return -1 else: leftHeight=isBalaanced(tree.leftChild) print(leftHeight) if (type(leftHeight)==int): rightHeight=isBalaanced(tree.rightChild) val1=False val2=False if(type(leftHeight)==int): val1=True if(type(rightHeight)==int): val2=True if not(val1 and val2): print val1,val2 return False if abs(leftHeight-rightHeight)>1: print ("f**k shit") return False else: return 1+max(leftHeight,rightHeight) user_input=input("enter tree data with comma separated values:") for i in user_input: tree.put(i,i) print isBalaanced(tree.root)
from pythonds.trees.bst import TreeNode, BinarySearchTree def listAtDepth(tree): list = [] root = tree.root temp = [root] while len(temp) > 0: ls = [] list.append(temp) for node in temp: if node.hasLeftChild(): ls.append(node.leftChild) if node.hasRightChild(): ls.append(node.rightChild) temp = ls return list while (True): tree = BinarySearchTree() data = input("please enter tree data : ") for item in data: tree.put(item, item) tree.preorder() print("level order") for level in listAtDepth(tree): for item in level: print item.key print("new level")
##Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D,you'll have D linked lists). from pythonds.trees.bst import TreeNode,BinarySearchTree def listAtDepth(tree): list=[] root=tree.root temp=[root] while len(temp)>0: ls=[] list.append(temp) for node in temp: if node.hasLeftChild(): ls.append(node.leftChild) if node .hasRightChild(): ls.append(node.rightChild) temp=ls return list while(True): tree=BinarySearchTree() data=input("please enter tree data : ") for item in data: tree.put(item,item) tree.preorder() print("level order") for level in listAtDepth(tree): for item in level: print item.key print ("new level")
def isSubtree(t1, t2): if (not t1) and (not t2): return True elif (not t1) or (not t2): return False else: loopSubTree(t1, t2) def loopSubTree(t1, t2): if t1 and (t1.key == t2.key): if treeMatch(t2, t1): return True else: return False elif not t1: return False return loopSubTree(t1.leftChild, t2) or loopSubTree(t1.rightChild, t2) while (True): tree1 = BinarySearchTree() user_input = input("Enter Tree1 :") for i in user_input: tree1.put(i, i) tree2 = BinarySearchTree() user_input = input("Enter Tree2 :") for i in user_input: tree2.put(i, i) print loopSubTree(tree1.root, tree2.root)
if tree != None: print("visit", tree.key) rightHeight = False if (tree == None): return -1 else: leftHeight = isBalaanced(tree.leftChild) print(leftHeight) if (type(leftHeight) == int): rightHeight = isBalaanced(tree.rightChild) val1 = False val2 = False if (type(leftHeight) == int): val1 = True if (type(rightHeight) == int): val2 = True if not (val1 and val2): print val1, val2 return False if abs(leftHeight - rightHeight) > 1: print("f**k shit") return False else: return 1 + max(leftHeight, rightHeight) user_input = input("enter tree data with comma separated values:") for i in user_input: tree.put(i, i) print isBalaanced(tree.root)