Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from ete3 import Tree
- # pseudo code
- # binary rooted tree comparison
- def equals(tree1, tree2):
- if tree1 == tree2:
- return True
- # checking only leaf names
- if tree1.is_leaf() and tree2.is_leaf():
- if tree1.name == tree2.name:
- return True
- return False
- if tree1 is None or tree2 is None:
- return False
- if len(tree1) != len(tree2):
- return False
- elif (len(tree1.get_children()) == 2 and len(tree2.get_children()) ==2):
- return (equals(tree1.children[0], tree2.children[0]) and
- equals(tree1.children[1], tree2.children[1])) or (equals(tree1.children[0], tree2.children[1]) and equals(tree1.children[1], tree2.children[0]))
- return False
- if __name__ == '__main__':
- t1 = Tree('(a,(c,d));')
- t2 = Tree('(a, (d,c));')
- t3 = Tree('((a,b),(d,c));')
- print equals(t1, t2)
- print equals(t1, t3)
- print equals(t2, t3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement