def draw_binary_tree_dx(): dummy = random.uniform(0, 1) if bern([dummy]) == 0: BinaryTree.generated_black_root_node = True return draw_black_rooted_btree_dx() else: BinaryTree.generated_black_root_node = False return draw_white_rooted_btree_dx()
def draw_binary_tree(): #This probabiliy has to be calculated ---> just a dummy here dummy = random.uniform(0, 1) if bern([dummy]) == 0: BinaryTree.generated_black_root_node = True return draw_black_rooted_btree() else: BinaryTree.generated_black_root_node = false return draw_white_rooted_btree()
def draw_empty_or_rooted_btree(color): dummy = random.uniform(0, 1) decision_1 = bern([dummy]) if decision_1 == 0: #Empty left child return None else: if color == 0: #Generate new black-rooted binary tree return draw_black_rooted_btree() else: #Generate new white-rooted binary tree return draw_white_rooted_btree()
def draw_white_rooted_btree_dx(): dummy_1 = random.uniform(0, 1) decision = bern([dummy_1]) self.numb_white_nodes += 1 self.numb_nodes += 1 left_child = None right_child = None if decision == 0: left_child = draw_empty_or_rooted_btree(0) right_child = draw_black_rooted_btree_dx() else: left_child = draw_black_rooted_btree_dx() right_child = draw_empty_or_rooted_btree(0) return BinaryTree(left_child, right_child)
def draw_black_rooted_btree_dx(): dummy_1 = random.uniform(0, 1) dummy_2 = (1 - dummy_1) * 1 / 3 dummy_3 = (1 - dummy_1) * 2 / 3 decision = bern([dummy_1, dummy_2, dummy_3]) if decision == 0: return draw_black_rooted_btree() else: self.numb_black_nodes += 1 self.numb_nodes += 1 left_child = None right_child = None if decision == 1: left_child = draw_empty_or_rooted_btree(1) right_child = draw_white_rooted_btree_dx() else: left_child = draw_white_rooted_btree_dx() right_child = draw_empty_or_rooted_btree(1) return BinaryTree(left_child, right_child)