15. 完全二叉树
约 250 字
预计阅读 1 分钟
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
|
class Node:
def __init__(self, data):
self.data = data
self.left_child = None
self.right_child = None
class Tree:
def __init__(self):
self.root = None
def add(self, item): # 添加节点
new_node = Node(item)
if self.root is None:
self.root = new_node
return
queue = [self.root]
while queue:
current_node = queue.pop(0)
if current_node.left_child is None:
current_node.left_child = new_node
return
else:
queue.append(current_node.left_child)
if current_node.right_child is None:
current_node.right_child = new_node
return
else:
queue.append(current_node.right_child)
def breadth_first_search(self):
if self.root is None:
return
queue = [self.root]
while queue:
current_node = queue.pop(0)
print(current_node.data, end=' ')
if current_node.left_child:
queue.append(current_node.left_child)
if current_node.right_child:
queue.append(current_node.right_child)
def depth_first_search_pre_order(self, node): # 先(根)序遍历
if node is None:
return
print(node.data, end=' ')
self.depth_first_search_pre_order(node.left_child)
self.depth_first_search_pre_order(node.right_child)
def depth_first_search_in_order(self, node): # 中(根)序遍历
if node is None:
return
self.depth_first_search_in_order(node.left_child)
print(node.data, end=' ')
self.depth_first_search_in_order(node.right_child)
def depth_first_search_post_order(self, node): # 后(根)序遍历
if node is None:
return
self.depth_first_search_post_order(node.left_child)
self.depth_first_search_post_order(node.right_child)
print(node.data, end=' ')
if __name__ == '__main__':
tree = Tree()
for i in range(5):
tree.add(i)
tree.breadth_first_search()
print()
tree.depth_first_search_pre_order(tree.root)
print()
tree.depth_first_search_in_order(tree.root)
print()
tree.depth_first_search_post_order(tree.root)
|