def ConvertToBinaryTree(self, sibs): root = self.store[0] if self.store[1] == []: succ = [] else: if len(self.store[1]) != 1: succsib = self.store[1][1:] else: succsib = [] succ = self.store[1][0] succ = succ.ConvertToBinaryTree(succsib) if sibs == []: sib = [] else: if len(sibs) != 1: sibsib = sibs[1:] else: sibsib = [] sib = sibs[0] sib = sib.ConvertToBinaryTree(sibsib) node = binary_tree(root) if succ != []: node.AddLeft(binary_tree(succ.store[0])) if sib != []: node.AddRight(binary_tree(sib.store[0])) return node
def ConvertToBinaryTree_2(self, bnode, successor, sibling): if successor != []: bnode.AddLeft(binary_tree(successor[0].store[0])) self.ConvertToBinaryTree_2(bnode.store[1], successor[0].store[1], successor[1:]) if sibling != []: bnode.AddRight(binary_tree(sibling[0].store[0])) self.ConvertToBinaryTree_2(bnode.store[2], sibling[0].store[1], sibling[1:]) return bnode
def ConvertToBinaryTree(self, sibs): for x in sibs: print(x.store[0]) root = self.store[0] if len(self.store[1]) == 0: succ = [] else: if len(self.store[1])!=1: succsib = self.store[1][1:] else: succsib = [] succ = self.store[1][0].ConvertToBinaryTree(succsib) if sibs == []: sib = [] else: if len(sibs) != 1: sibsib = sibs[1:] else: sibsib = [] sib = sibs[0].ConvertToBinaryTree(sibsib) node = binary_tree(root) if succ != []: print("Added Left " + succ.store[0]+ " to " + root) node.AddLeft(succ) if sib != []: print("Added Right " + sib.store[0] + " to " + root) node.AddRight(sib) return node
def ConvertToBinaryTree_helper(self, siblings): left_node = False right_node = False if len(self.store[1]) == 1: left_node = self.store[1][0].ConvertToBinaryTree_helper([]) elif len(self.store[1]) > 1: left_node = self.store[1][0].ConvertToBinaryTree_helper( self.store[1][1:]) if len(siblings) == 1: right_node = siblings[0].ConvertToBinaryTree_helper([]) elif len(siblings) > 1: right_node = siblings[0].ConvertToBinaryTree_helper(siblings[1:]) node = binary_tree(self.store[0]) if left_node != False: node.AddLeft(left_node) if right_node != False: node.AddRight(right_node) return node
def BThelper(self, siblings): root = self.store[0] if self.store[1] == []: left = [] else: if len(self.store[1]) == 1: leftsiblings = [] else: leftsiblings = self.store[1][1:] left = self.store[1][0].BThelper(leftsiblings) if siblings == []: right = [] else: if len(siblings) == 1: rightsiblings = [] else: rightsiblings = siblings[1:] right = siblings[0].BThelper(rightsiblings) x = binary_tree(root) if left != []: x.AddLeft(left) if right != []: x.AddRight(right) return x
def ConvertToBinaryTree(self): p= self.hel(self.store[0], self.store, []) y=binary_tree(p) return y
def __init__(self,x): if type(x)==list: self.s = [x[0], binary_tree(x[1]), binary_tree(x[2])] else: self.s = [x, None, None] self.l = [[]]
def ConvertToBinaryTree(x): b = binary_tree(x.store[0]) if len(x.store[1]) == 2: b.AddLeft(ConvertToBinaryTree(x.store[1][0])) b.AddRight(ConvertToBinaryTree(x.store[1][1])) return b
root.AddSuccessor(y) root.AddSuccessor(z) x.AddSuccessor(x1) x.AddSuccessor(x2) x.AddSuccessor(x3) y.AddSuccessor(y1) z.AddSuccessor(z1) z1.AddSuccessor(z11) print(root.Print_DepthFirst()) print(root.Get_LevelOrder()) a=root.ConvertToBinaryTree() print(a.Get_LevelOrder()) j=binary_tree(5) d=binary_tree(4) t=binary_tree(6) h=binary_tree(7) j.AddLeft(d) d.AddLeft(h) d.AddRight(t) print(j.Get_LevelOrder()) y=a.ConvertToTree() print((y[1]).Get_LevelOrder())
z.AddSuccessor(c) z.AddSuccessor(a) y.AddSuccessor(b) s.AddSuccessor(d) print('INFO: We are now printing depth first') x.Print_DepthFirst() print('INFO: Converting the tree into a level-ordered list:') x.Get_LevelOrder() correctList = [1000, 2000, 3000, 4000, 7, 5, 6, 8] if (x.Get_LevelOrder() == correctList): print('Passed test for Get_LevelOrder') print('INFO: Testing Assignment 4') print x.ConvertToBinaryTree # this test code below is for binary_tree.py X = binary_tree(1000) Y = binary_tree(2000) Z = binary_tree(3000) X.AddLeft(Y) X.AddRight(Z) C = binary_tree(5) Z.AddRight(C) print('INFO: Printing depth first for the binary tree') X.Print_DepthFirst() print('INFO: Testing Assignment 5') print X.ConvertToTree()
def ConvertToBinaryTree(self): return self.ConvertToBinaryTree_2(binary_tree(self.store[0]), self.store[1], [])
s = tree(7) w.AddSuccessor(t) w.AddSuccessor(l) e.AddSuccessor(s) q.AddSuccessor(w) q.AddSuccessor(e) q.AddSuccessor(r) # p = q.hel(q.store[0], q.store, []) p = q.ConvertToBinaryTree() print p.Get_LevelOrder() #print "Done" a = binary_tree(1) b = binary_tree(2) c = binary_tree(3) d = binary_tree(4) e = binary_tree(5) f = binary_tree(6) g = binary_tree(7) e.AddRight(f) b.AddLeft(e) c.AddLeft(g) c.AddRight(d) b.AddRight(c) a.AddLeft(b)
y.AddSuccessor(c) y.AddSuccessor(d) z.AddSuccessor(d) d.AddSuccessor(e) x.Print_DepthFirst() print x.Get_LevelOrder() print "\n----Converting to binary tree----" btree=x.ConvertToBinaryTree() btree.Print_DepthFirst() print btree.Get_LevelOrder() print "\n----binary tree----" a=binary_tree(1000) b=binary_tree(2000) c=binary_tree(3000) j=binary_tree(400) k=binary_tree(500) l=binary_tree(600) f=binary_tree(70) g=binary_tree(80) a.AddLeft(b) a.AddRight(c) b.AddLeft(j) b.AddRight(k) c.AddLeft(l) k.AddLeft(f) l.AddRight(g) print "left up, right down\n"
import binary_tree import tree addSuccessor(y) x.AddSuccessor(z) c = tree(5) m = tree(33) y.AddSuccessor(m) z.AddSuccessor(c) print(x) x.Print_DepthFirst() lst = x.Get_LevelOrder() for i in range(0, len(lst), 1): print(lst[i]) q = binary_tree(1) q.AddLeft(binary_tree(2)) a = binary_tree(3) a.AddLeft(binary_tree(4)) a.AddRight(binary_tree(5)) q.AddRight(y) q.Print_Depth(0) ls = q.GetLevelOrder() for i in range(0, len(ls), 1): print(ls[i])
from binary_tree import * a = binary_tree("A") b = binary_tree("B") e = binary_tree("E") c = binary_tree("C") e.AddRight(binary_tree("F")) b.AddLeft(e) c.AddRight(binary_tree("D")) b.AddRight(c) a.AddLeft(b) a.Print_tree("") a.Get_LevelOrder() b = a.ConvertToTree() b.Print_DepthFirst()