def test_none_tree_none(): custom_left = BinaryTreeNode() node = BinaryTreeNode(None, custom_left, None) assert node.data == None assert node.left == custom_left assert node.right == None print ("Test NTN przeszedl")
def test_none_none_tree(): custom_right = BinaryTreeNode() node = BinaryTreeNode(None, None, custom_right) assert node.data == None assert node.left == None assert node.right == custom_right print ("Test NNT przeszedl")
def test_data_tree_tree(): custom_data = 13 custom_left = BinaryTreeNode() custom_right = BinaryTreeNode() node = BinaryTreeNode(custom_data, custom_left, custom_right) assert node.data == custom_data assert node.left == custom_left assert node.right == custom_right print ("Test DTT przeszedl") print node.data print node.right
def test_data_init(): # D - jakas wartosc dla value # T - drzewo # N - None custom_data = 5 custom_left = BinaryTreeNode() node = BinaryTreeNode(data=custom_data, left=custom_left, right=None) assert node.left == custom_left assert node.right == None assert node.data == custom_data print ("Test DTN przeszedl")
def test_default_init(): node = BinaryTreeNode(None, None, None) # N - Trzy warosci None assert node.left is None assert node.right is None assert node.data is None print ("Test NNN przeszedl")
class BSTUtils: minval = float('inf') prevval = float('-inf') def minDistanceBST(self, root): if root == None: return None self.minDistanceBST(root.left) self.minval = min(self.minval, abs(root.data - self.prevval)) self.prevval = root.data self.minDistanceBST(root.right) return self.minval tree = BinaryTreeNode(17) tree.left = BinaryTreeNode(10) tree.right = BinaryTreeNode(25) tree.left.left = BinaryTreeNode(7) tree.left.right = BinaryTreeNode(12) tree.right.left = BinaryTreeNode(19) tree.right.right = BinaryTreeNode(32) # BinaryTreeUtils().inorder_traversal(tree) print(BSTUtils().minDistanceBST(tree))
parents = current current = list() data = list() for parent in parents: if parent.left: current.append(parent.left) data.append(parent.left.data) if parent.right: current.append(parent.right) data.append(parent.right.data) return treelists tree = BinaryTreeNode(1) tree.left = BinaryTreeNode(2) tree.right = BinaryTreeNode(3) tree.left.left = BinaryTreeNode(4) tree.left.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(6) tree.right.right = BinaryTreeNode(7) tree.left.left = BinaryTreeNode(4) tree.left.right = BinaryTreeNode(5) tree.right.left = BinaryTreeNode(6) tree.right.right = BinaryTreeNode(7) print(listOfDepths(tree, [])) print(listOfDepthsBFS(tree))
def test_data_none_none(): custom_data = 13 node = BinaryTreeNode(custom_data, None, None) assert node.data == custom_data assert node.left == node.right == None print ("Test DNN przeszedl")
test_none_tree_none() test_data_none_none() test_data_tree_tree() test_no_data() nodeLeft = BinarySearchTreeNode(222, None, None) nodeRight = BinarySearchTreeNode(333, None, None) node = BinarySearchTreeNode(112, nodeLeft, nodeRight) print (node.data) print (node.left.data) a = 77 node.add_val(a) print (node.data) node.add_left(BinaryTreeNode()) print (node.left) node.add_right(BinaryTreeNode()) print (node.right) root = BinarySearchTreeNode(0) k = root.data print (k) l = root.left print (l) rootLeft = BinarySearchTreeNode( BinarySearchTreeNode()) print (rootLeft.data)