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)
from pythonds.trees.bst import BinarySearchTree def inorder(tree): if tree != None: inorder(tree.leftChild) print tree.key inorder(tree.rightChild) tree=BinarySearchTree() #30,15,18,7,8,6,60,72 def height(tree): if tree==None: return -1 else: return 1+max(height(tree.leftChild),height(tree.rightChild)) inorder(tree.root) def isBalaanced(tree,done=False): 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):
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)
from pythonds.trees.bst import BinarySearchTree def inorder(tree): if tree != None: inorder(tree.leftChild) print tree.key inorder(tree.rightChild) tree = BinarySearchTree() #30,15,18,7,8,6,60,72 def height(tree): if tree == None: return -1 else: return 1 + max(height(tree.leftChild), height(tree.rightChild)) inorder(tree.root) def isBalaanced(tree, done=False): if tree != None: print("visit", tree.key) rightHeight = False if (tree == None): return -1 else: