Skip to content

Instantly share code, notes, and snippets.

@maclandrol
Created August 16, 2017 21:49
Show Gist options
  • Save maclandrol/cc80d18901ce3ca62f145a0bb828aaaf to your computer and use it in GitHub Desktop.
Save maclandrol/cc80d18901ce3ca62f145a0bb828aaaf to your computer and use it in GitHub Desktop.
#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment