二叉树的实现(补充)
本次实现的二叉树包括二叉树的先序遍历,中序遍历和后序遍历以及二叉树的层序遍历,还包括二叉树的高度,叶子节点以及反转二叉树
二叉树的层序遍历依然是使用Python内置的deque实现一个队列,根据队列先进先出(FIFO)的性质,先把二叉树的根节点放入队列中,判断队列是否为空,如果不为空,则弹出一个元素,打印该元素的值,如果该元素有左孩子,则把左孩子入队,如果有右孩子,则把有孩子入队
代码:
1 from collections import deque 2 3 4 class Queue: # 借助内置的 deque 我们可以迅速实现一个 Queue 5 def __init__(self): 6 self._elem = deque() 7 8 def append(self, value): 9 return self._elem.append(value) 10 11 def pop(self): 12 return self._elem.popleft() 13 14 def empty(self): 15 return len(self._elem) == 0 16 17 18 class BTree(object): 19 def __init__(self, data=None, left=None, right=None): 20 self.data = data 21 self.left = left 22 self.right = right 23 24 def preorder(self): 25 if self.data is not None: 26 print(self.data, end=',') 27 if self.left is not None: 28 self.left.preorder() 29 if self.right is not None: 30 self.right.preorder() 31 32 def midorder(self): 33 if self.left is not None: 34 self.left.midorder() 35 if self.data is not None: 36 print(self.data, end=',') 37 if self.right is not None: 38 self.right.midorder() 39 40 def postorder(self): 41 if self.left is not None: 42 self.left.postorder() 43 if self.right is not None: 44 self.right.postorder() 45 if self.data is not None: 46 print(self.data, end=',') 47 48 def levelorder(self): 49 50 # 返回某个节点的左孩子 51 def LChild_Of_Node(node): 52 return node.left if node.left is not None else None 53 54 # 返回某个节点的右孩子 55 def RChild_Of_Node(node): 56 return node.right if node.right is not None else None 57 58 # 层序遍历列表 59 level_order = [] 60 # 是否添加根节点中的数据 61 if self.data is not None: 62 level_order.append([self]) 63 64 # 二叉树的高度 65 height = self.height() 66 if height >= 1: 67 # 对第二层及其以后的层数进行操作, 在level_order中添加节点而不是数据 68 for _ in range(2, height + 1): 69 level = [] # 该层的节点 70 for node in level_order[-1]: 71 # 如果左孩子非空,则添加左孩子 72 if LChild_Of_Node(node): 73 level.append(LChild_Of_Node(node)) 74 # 如果右孩子非空,则添加右孩子 75 if RChild_Of_Node(node): 76 level.append(RChild_Of_Node(node)) 77 # 如果该层非空,则添加该层 78 if level: 79 level_order.append(level) 80 81 # 取出每层中的数据 82 for i in range(0, height): # 层数 83 for index in range(len(level_order[i])): 84 level_order[i][index] = level_order[i][index].data 85 86 return level_order 87 88 def height(self): 89 if self.data is None: 90 return 0 91 elif self.left is None and self.right is None: 92 return 1 93 elif self.left is None and self.right is not None: 94 return 1 + self.left.height() 95 elif self.left is not None and self.right is None: 96 return 1 + self.left.height() 97 else: 98 return 1 + max(self.left.height(), self.right.height()) 99 100 def leaves(self): 101 if self.data is None: 102 return None 103 elif self.left is None and self.right is None: 104 print(self.data, end=',') 105 elif self.left is None and self.right is not None: 106 self.right.leaves() 107 elif self.left is not None and self.right is None: 108 self.left.leaves() 109 else: 110 self.left.leaves() 111 self.right.leaves() 112 113 # 反转二叉树 114 def reverse(self): 115 if self.data is not None: 116 if self.left and self.right: 117 self.left, self.right = self.right, self.left 118 self.left.reverse() 119 self.right.reverse() 120 121 # @staticmethod 122 # def layer_trav(subtree): 123 # cur_nodes = [subtree] # current layer nodes 124 # next_nodes = [] 125 # while cur_nodes or next_nodes: 126 # for node in cur_nodes: 127 # print(node.data, end=',') 128 # if node.left: 129 # next_nodes.append(node.left) 130 # if node.right: 131 # next_nodes.append(node.right) 132 # cur_nodes = next_nodes # 继续遍历下一层 133 # next_nodes = [] 134 135 @staticmethod 136 def layer_trav(subtree): 137 q = Queue() 138 q.append(subtree) 139 while not q.empty(): 140 node = q.pop() 141 print(node.data, end=',') 142 if node.left: 143 q.append(node.left) 144 if node.right: 145 q.append(node.right) 146 147 148 if __name__ == '__main__': 149 right_tree = BTree(6) 150 right_tree.left = BTree(2) 151 right_tree.right = BTree(4) 152 153 left_tree = BTree(5) 154 left_tree.left = BTree(1) 155 left_tree.right = BTree(3) 156 157 tree = BTree(11) 158 tree.left = left_tree 159 tree.right = right_tree 160 161 left_tree = BTree(7) 162 left_tree.left = BTree(3) 163 left_tree.right = BTree(4) 164 165 right_tree = tree # 增加新的变量 166 tree = BTree(18) 167 tree.left = left_tree 168 tree.right = right_tree 169 tree.root = tree 170 171 print('先序遍历为:') 172 tree.preorder() 173 print(' ') 174 175 print('后序遍历为:') 176 tree.postorder() 177 print(' ') 178 179 print('中序遍历为:') 180 tree.midorder() 181 print(' ') 182 183 print('层序遍历为:') 184 level_order = tree.levelorder() 185 tree.layer_trav(tree) 186 print(' ') 187 print(level_order) 188 189 print('叶子节点为:') 190 tree.leaves() 191 print(' ') 192 193 print('树的高度为:') 194 print(tree.height()) 195 print(' ') 196 197 print('反转二叉树') 198 tree.reverse() 199 print('先序遍历') 200 tree.preorder() 201 print(' ') 202 print('层序遍历') 203 tree.layer_trav(tree)