jweinst1
12/2/2016 - 8:12 PM

treecompareex.py

class tree:
    
    def __init__(self, entry):
        self.entry = entry
        self.branches = []
        
    def append(self, elem):
        self.branches.append(tree(elem))
    
    @property  
    def is_leaf(self):
        return self.branches == []
        
#find if two identical tree structures all hold the same values in the same positions    
def is_ident(t1, t2):
    result = True
    if t1.entry != t2.entry:
        result = result and False
    for i in range(len(t1.branches)):
        result = result and is_ident(t1.branches[i], t2.branches[i])
    return result
   
   
a = tree(4)
b = tree(4)
a.append(3)
b.append(3)
a.branches[0].append(2)
b.branches[0].append(2)
print(is_ident(a, b))