if node.left: left_result = helper(node.left, depth + 1) if node.right: right_result = helper(node.right, depth + 1) if left_result[0] and right_result[0]: if left_result[1] > right_result[1]: return left_result else: return right_result elif left_result[0]: return left_result elif right_result[0]: return right_result return helper(root, 0) if __name__ == '__main__': tree = BTree() node_1 = Node(50) node_2 = Node(102) node_3 = Node(40) node_4 = Node(400) tree.insert(node_1) tree.insert(node_2) tree.insert(node_3) tree.insert(node_4) tree.show() node, depth = deepest_node(tree.root) print('deepest node is', node.data)
def _evaluate(self, field, move): """Builds an initial tree with copy of current field as a root, runs recursion.""" f = deepcopy(field) self._move(f, move) # performing suggested move tree = BTree(f) return self._perform_recursion(tree)
return parent.data # right subtree elif parent.right == item: item = parent while item.parent: if item.parent.left == item: return item.parent.data else: item = item.parent return None else: return mininum_node(item.right).data if __name__ == '__main__': tree = BTree() node_1 = Node(10) node_2 = Node(5) node_3 = Node(30) node_4 = Node(22) node_5 = Node(35) node_6 = Node(6) node_7 = Node(7) tree.insert_with_parent_setting(node_1) tree.insert_with_parent_setting(node_2) tree.insert_with_parent_setting(node_3) tree.insert_with_parent_setting(node_4) tree.insert_with_parent_setting(node_5) tree.insert_with_parent_setting(node_6) tree.insert_with_parent_setting(node_7) tree.show()
def setUp(self): self.btree = BTree() self.node = BTree.Node(1)
class TestBtree(unittest.TestCase): def setUp(self): self.btree = BTree() self.node = BTree.Node(1) def test_node_complete(self): self.node.left = BTree.Node(2) self.node.right = BTree.Node(3) self.assertEqual(self.node.complete(), True) def test_node_incomplete(self): self.node.left = BTree.Node(2) self.assertEqual(self.node.complete(), False) def test_set_root(self): self.btree.push(10) self.assertEqual(self.btree._root.value, 10) # self.assertEqual(self.btree._root.index, 0) def test_push_on_leaf(self): self.btree.push(self.node) self.btree.push(2) self.assertEqual(self.btree._root.left.value, 2) # self.assertEqual(self.btree._last.index, 1) def test_push_on_left_filled(self): self.btree.push(self.node) self.btree.push(2) self.btree.push(3) self.assertEqual(self.btree._root.right.value, 3) # self.assertEqual(self.btree._root.left.index, 1) # self.assertEqual(self.btree._root.right.index, 2) def test_push_on_complete(self): self.btree.push(self.node) self.btree.push(2) self.btree.push(3) self.btree.push(4) self.assertEqual(self.btree._root.left.left.value, 4) def test_push_to_6(self): self.btree.push(self.node) self.btree.push(2) self.btree.push(3) self.btree.push(4) self.btree.push(5) self.btree.push(6) self.assertEqual(self.btree._root.left.right.value, 5) self.assertEqual(self.btree._root.right.left.value, 6) def test_breadth_first_traversal(self): self.btree.push(1) self.btree.push(2) self.btree.push(3) self.btree.push(4) self.btree.push(5) self.assertEqual(self.btree.breadth_first_traversal(), [1, 2, 3, 4, 5]) def test_breadth_traversal(self): self.btree.push(1) self.btree.push(2) self.btree.push(3) self.btree.push(4) self.btree.push(5) self.assertEqual(len(self.btree._breadth_traversal()._contents), 5) def test_node_has_children(self): self.btree.push(1) self.btree.push(2) self.btree.push(3) self.assertEqual(self.btree._root.children(), [2, 3])
def helper(item, current_level, target_level): if not item: return [] if current_level == target_level: return [item] elif current_level > target_level: return [] return helper(item.left, current_level + 1, target_level) + \ helper(item.right, current_level + 1, target_level) node_level = get_level(root, node) return helper(root, 1, node_level) if __name__ == '__main__': tree = BTree() node_1 = Node(50) node_2 = Node(130) node_3 = Node(40) node_4 = Node(152) node_5 = Node(102) tree.insert(node_1) tree.insert(node_2) tree.insert(node_3) tree.insert(node_4) tree.insert(node_5) cousins = find_cousins(tree.root, node_5) for cousin in cousins: print(cousin)