Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from ete3 import Tree
  3.  
  4. # pseudo code
  5. # binary rooted tree comparison
  6. def equals(tree1, tree2):
  7.  
  8. if tree1 == tree2:
  9. return True
  10. # checking only leaf names
  11. if tree1.is_leaf() and tree2.is_leaf():
  12. if tree1.name == tree2.name:
  13. return True
  14. return False
  15. if tree1 is None or tree2 is None:
  16. return False
  17. if len(tree1) != len(tree2):
  18. return False
  19. elif (len(tree1.get_children()) == 2 and len(tree2.get_children()) ==2):
  20. return (equals(tree1.children[0], tree2.children[0]) and
  21. equals(tree1.children[1], tree2.children[1])) or (equals(tree1.children[0], tree2.children[1]) and equals(tree1.children[1], tree2.children[0]))
  22. return False
  23.  
  24. if __name__ == '__main__':
  25. t1 = Tree('(a,(c,d));')
  26. t2 = Tree('(a, (d,c));')
  27. t3 = Tree('((a,b),(d,c));')
  28. print equals(t1, t2)
  29. print equals(t1, t3)
  30. print equals(t2, t3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement