def test_height(self): # Create node with no children node = BinaryTreeNode(2) assert node.height() == 0 # Attach left child node node.left = BinaryTreeNode(1) assert node.height() == 1 # Attach right child node node.right = BinaryTreeNode(3) assert node.height() == 1 # Attach right child node to right node child node.right.right = BinaryTreeNode(5) assert node.height() == 2 # Attach right child node to previously attached right node child node.right.right.right = BinaryTreeNode(7) assert node.height() == 3 # Detach right leaf node node.right.right.right = None assert node.height() == 2 # Detach right leaf node again node.right.right = None assert node.height() == 1 # Detach left child node node.left = None assert node.height() == 1 # Detach right child node node.right = None assert node.height() == 0
def test_height(self): second_child_node = BinaryTreeNode(7) second_child_node.right = BinaryTreeNode(8) child_node = BinaryTreeNode(1) child_node.left = second_child_node child_node.right = BinaryTreeNode(6) node = BinaryTreeNode(2) node.right = BinaryTreeNode(3) assert node.height() == 1 node.left = child_node assert node.height() == 3
def test_is_branch(self): # Create node with no children node = BinaryTreeNode(2) assert node.is_branch() is False # Attach left child node node.left = BinaryTreeNode(1) assert node.is_branch() is True # Attach right child node node.right = BinaryTreeNode(3) assert node.is_branch() is True # Detach left child node node.left = None assert node.is_branch() is True # Detach right child node node.right = None assert node.is_branch() is False
def test_height(self): left_node = BinaryTreeNode(3) root_node = BinaryTreeNode(5) right_node = BinaryTreeNode(8) root_node.left = left_node root_node.right = right_node left_node_of_left = BinaryTreeNode(2) right_node_of_Left = BinaryTreeNode(4) left_node.left = left_node_of_left left_node.right = right_node_of_Left left_node_of_left_node_of_left = BinaryTreeNode(1) left_node_of_left.left = left_node_of_left_node_of_left right_node_right = BinaryTreeNode(9) right_node.right = right_node_right assert root_node.height() == 3
def test_height(self): # Create node with no children node = BinaryTreeNode(4) assert node.height() == 0 # Attach left child node node.left = BinaryTreeNode(2) assert node.height() == 1 # Attach right child node node.right = BinaryTreeNode(6) assert node.height() == 1 # Attach left-left grandchild node node.left.left = BinaryTreeNode(1) assert node.height() == 2 # Attach right-right grandchild node node.right.right = BinaryTreeNode(8) assert node.height() == 2 # Attach right-right-left great-grandchild node node.right.right.left = BinaryTreeNode(7) assert node.height() == 3
while len(stack) != 0: node, depth = stack.pop() if isLeaf(node): if deepest is None and shallowest is None: deepest, shallowest = depth, depth if depth > deepest: deepest = depth if depth < shallowest: shallowest = depth if (deepest - shallowest) > 1: return False if node.left is not None: stack.append((node.left, depth+1)) if node.right is not None: stack.append((node.right, depth+1)) return True def isLeaf(node: BinaryTreeNode): return node.left is None and node.right is None sb = BinaryTreeNode(1) sb.left = BinaryTreeNode(2) sb.right = BinaryTreeNode(3) nb = BinaryTreeNode(1) nb.left =BinaryTreeNode(2) nb.right = BinaryTreeNode(3) nb.left.left = BinaryTreeNode(4) nb.left.left.left = BinaryTreeNode(5) print(isSuperBalanced(sb))
def second_largest(root: BinaryTreeNode) -> BinaryTreeNode: node = root parent = root while node.right is not None: parent = node node = node.right if node.left is None: return parent else: node = node.left while node.right is not None: node = node.right return node no_left_bst = BinaryTreeNode(5) no_left_bst.insert_left(4) no_left_bst.insert_right(6) no_left_bst.right.insert_right(7) print(second_largest(no_left_bst).value) left_bst = BinaryTreeNode(5) left_bst.left = BinaryTreeNode(3) left_bst.right = BinaryTreeNode(8) left_bst.right.left = BinaryTreeNode(7) left_bst.right.right = BinaryTreeNode(12) #largest left_bst.right.right.left = BinaryTreeNode(10) #left tree of largest left_bst.right.right.left.left = BinaryTreeNode(9) left_bst.right.right.left.right = BinaryTreeNode(11) #second largest print(second_largest(left_bst).value)