Beispiel #1
0
def add_node_depth(tree: tree_utils.Node, max_depth: int) -> None:
    """
    add the column depth of each node on the tree, where the root is column 1 and the tips are column x - 1
    where x is the last column which will contain the tip names
    """
    tree.node_depth = max_depth - tree.max_node_tip_count()
    for d in tree.descendants:
        add_node_depth(d, max_depth)
Beispiel #2
0
def build_minimal_bst(array, start, end):
    """Build a minimal height binary search tree given a sorted arry.
    Child trees have equal size, so the resulted bst might not be complete.
    """
    if start >= end:
        return None
    mid = (start + end) // 2
    root = Node(array[mid])
    root.left = build_minimal_bst(array, start, mid)
    root.right = build_minimal_bst(array, mid + 1, end)
    return root
def build_minimal_bst(array, start, end):
    """Build a minimal height binary search tree given a sorted arry.
    Child trees have equal size, so the resulted bst might not be complete.
    """
    if start >= end:
        return None
    mid = (start + end) // 2
    root = Node(array[mid])
    root.left = build_minimal_bst(array, start, mid)
    root.right = build_minimal_bst(array, mid + 1, end)
    return root
Beispiel #4
0
def build_complete_bst(array, start, end):
    """Build a complete binary search tree given a sorted array."""
    if start >= end:
        return None
    # find the root node index in the given array
    l = end - start
    height = int(math.log(l, 2))
    num_of_leafs = l - (2**height - 1)
    if num_of_leafs > 2**(height - 1):
        left_tree_size = 2**height - 1
    else:
        left_tree_size = l - 2**(height - 1)
    root_index = start + left_tree_size
    # recursively build bst
    root = Node(array[root_index])
    root.left = build_complete_bst(array, start, root_index)
    root.right = build_complete_bst(array, root_index + 1, end)
    return root
def build_complete_bst(array, start, end):
    """Build a complete binary search tree given a sorted array."""
    if start >= end:
        return None
    # find the root node index in the given array
    l = end - start
    height = int(math.log(l, 2))
    num_of_leafs = l - (2 ** height - 1)
    if num_of_leafs > 2 ** (height - 1):
        left_tree_size = 2 ** height - 1
    else:
        left_tree_size = l - 2 ** (height - 1)
    root_index = start + left_tree_size
    # recursively build bst
    root = Node(array[root_index])
    root.left = build_complete_bst(array, start, root_index)
    root.right = build_complete_bst(array, root_index + 1, end)
    return root
Beispiel #6
0
def calculate_tree(tree: tree_utils.Node, nrows: int, ncols: int, rows_per_tip: int,
                   label_branches: bool, scale_branches: bool = False) -> Tuple[list, list, list]:
    taxa = []
    branches = []
    vlines = []
    if scale_branches:
        tree_depth = tree.max_node_tip_length()
        scale = (ncols - 1) / tree_depth
    else:
        scale = 1
    tree_recursion(tree, 1, ncols, 1, nrows, taxa, branches, vlines, rows_per_tip, label_branches, scale_branches,
                   scale)
    return taxa, branches, vlines
Beispiel #7
0
 def setUp(self):
     self.t1 = Node(1)
     self.t2 = Node(2, Node(2))
     self.t3 = Node(3, Node(2), Node(1))
     self.t4 = Node(3, Node(2, Node(1)))
Beispiel #8
0
 def test_false(self):
     tree = Node(2, Node(0, None, Node(5)), Node(4, Node(3)))
     self.assertFalse(self.is_bst(tree))
Beispiel #9
0
 def test_true(self):
     tree = Node(2, Node(0, None, Node(1)), Node(4, Node(3)))
     self.assertTrue(self.is_bst(tree))
Beispiel #10
0
 def test_one_node(self):
     self.assertTrue(self.is_bst(Node(0)))
 def setUp(self):
     self.root = Node(0, Node(1, Node(2), Node(3, None, Node(4))),
                      Node(5, Node(6), None))
Beispiel #12
0
 def setUp(self):
     self.root = Node(1, Node(7, Node(3, Node(0)), Node(5)),
                      Node(4, Node(3), Node(2, Node(1))))
Beispiel #13
0
 def setUp(self):
     self.x = Node(0, Node(1, None, Node(3)), Node(2))
     self.y = Node(0, Node(2))
     self.z = Node(1, None, Node(3))