-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathTree_example.py
99 lines (67 loc) · 1.97 KB
/
Tree_example.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class Node:
def __init__(self):
self.data = None
self.children = []
def create_tree(node, num_row, num_child):
node.data = num_row
if num_row > 0:
for i in range(num_child):
child = create_tree(Node(), num_row-1, num_child)
node.children.append(child)
return node
def DFS_recursive(node):
if node.data != None:
print(node.data)
for child in node.children:
DFS_recursive(child)
def DFS_recursive_postorder(node):
for child in node.children:
DFS_recursive_postorder(child)
if node.data != None:
print(node.data)
# This assumes only 2 children, but accounts for other possibilities
def DFS_recursive_inorder_btree(node):
if (len(node.children) == 2):
DFS_recursive_inorder_btree(node.children[0])
print(node.data)
DFS_recursive_inorder_btree(node.children[1])
elif (len(node.children) == 1):
DFS_recursive_inorder_btree(node.children[0])
print(node.data)
elif (len(node.children) == 0):
print(node.data)
else:
print("Not a binary tree!")
def DFS_stack(node):
stack = []
stack.append(node)
temp = None
while len(stack) > 0:
print(stack[-1].data)
temp = stack.pop()
for child in temp.children:
stack.append(child)
def BFS_queue(node):
queue = []
queue.append(node)
temp = None
while len(queue) > 0:
print(queue[0].data)
temp = queue.pop(0)
for child in temp.children:
queue.append(child)
def main():
tree = create_tree(Node(), 3, 3)
print("Recursive:")
DFS_recursive(tree)
print("Recursive Postorder:")
DFS_recursive_postorder(tree)
print("Stack:")
DFS_stack(tree)
print("Queue:")
BFS_queue(tree)
binaryTree = create_tree(Node(), 3, 2)
print("Recursive Inorder Binary Tree:")
DFS_recursive_inorder_btree(binaryTree)
if __name__ == '__main__':
main()