def build_tree(node): if node.is_terminal(): return TreeLeaf(get_idx(node.name)) else: children = map(build_tree, node.clades) node = TreeNode() for child in children: node.add_child(child) return node
from trees.util import plot_tree from trees import Tree, TreeNode, TreeLeaf import matplotlib.pyplot as plt if __name__ == "__main__": leaf1 = TreeLeaf(1) leaf2 = TreeLeaf(2) leaf3 = TreeLeaf(3) node1 = TreeNode() node1.add_child(leaf1) node1.add_child(leaf2) node2 = TreeNode() node2.add_child(node1) node2.add_child(leaf3) tree = Tree(node2) plot_tree(tree) plt.show() p = leaf1.detach() plot_tree(tree) plt.show() leaf3.attach(p) plot_tree(tree) plt.show()
def build_location_tree(): root = TreeNode('Global') usa = TreeNode('USA') chl = TreeNode('Chile') # Build New jersie tree nej = TreeNode('New Jersie') nej.add_child(TreeNode('Princeton')) nej.add_child(TreeNode('Treton')) # Build california tree cal = TreeNode('California') cal.add_child(TreeNode('San Francisco')) cal.add_child(TreeNode('Mountain view')) cal.add_child(TreeNode('Palo Alto')) # Finish usa tree usa.add_child(nej) usa.add_child(cal) # Build Region Metropolitana region rm = TreeNode('Region Metropolitana') rm.add_child(TreeNode('Santiago')) rm.add_child(TreeNode('Maipu')) # Build Punta Arenas tree mas = TreeNode('Magallanes') mas.add_child(TreeNode('Punta Arenas')) mas.add_child(TreeNode('Puerto Natales')) # Finish Chile Tree chl.add_child(rm) chl.add_child(mas) # Finish all tree root.add_child(chl) root.add_child(usa) return root