-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbtree_traversal.py
68 lines (54 loc) · 1.5 KB
/
btree_traversal.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
import sys
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def __str__(self):
left = self.left.key if self.left else None
right = self.right.key if self.right else None
return 'key: {}, left: {}, right: {}'.format(self.key, left, right)
def read_data():
n = int(input())
nodes = [Node(0) for _ in range(n)]
for i in range(n):
key, left, right = map(int, sys.stdin.readline().split())
nodes[i].key = key
if left != -1:
nodes[i].left = nodes[left]
if right != -1:
nodes[i].right = nodes[right]
return nodes[0]
def in_order(current, result):
if not current:
return
in_order(current.left, result)
result.append(current.key)
in_order(current.right, result)
return result
def pre_order(current):
if not current:
return
print(current.key, end=' ')
pre_order(current.left)
pre_order(current.right)
def breadth_first_search(root):
queue = [root]
while queue:
tmp_queue = []
for element in queue:
print(element.key, end=' ')
if element.left:
tmp_queue.append(element.left)
if element.right:
tmp_queue.append(element.right)
queue = tmp_queue
print()
def main():
root = read_data()
print(in_order(root, []))
pre_order(root)
print()
print('-'*80)
breadth_first_search(root)
main()