-
Notifications
You must be signed in to change notification settings - Fork 0
/
116.py
57 lines (43 loc) · 1.44 KB
/
116.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Problem:
Generate a finite, but an arbitrarily large binary tree quickly in O(1).
That is, generate() should return a tree whose size is unbounded but finite.
"""
from random import random, randint
import matplotlib.pyplot as plt
from DataStructures.Tree import BinaryTree, Node
def generate_helper(
node: Node,
probability_add_children: float = 0.5,
probability_add_branch: float = 0.5,
) -> None:
if random() > probability_add_children:
return
# generating the left branch
if random() < probability_add_branch:
node.left = Node(randint(1, 1000))
generate_helper(node.left, probability_add_children, probability_add_branch)
# generating the right branch
if random() < probability_add_branch:
node.right = Node(randint(1, 1000))
generate_helper(node.right, probability_add_children, probability_add_branch)
def generate() -> BinaryTree:
tree = BinaryTree()
tree.root = Node(randint(1, 1000))
generate_helper(tree.root, 0.7, 0.7)
# suggestion: don't use higher values for probability, it will lead to recursion
# error
return tree
if __name__ == "__main__":
tree_length_list = []
for i in range(1000):
tree_length_list.append(len(generate()))
plt.hist(tree_length_list)
plt.show()
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
[n nodes cannot be generated in O(1) time, but since n is finite it may be considered
constant]
"""