二叉树的实现(补充)
程序员文章站
2022-06-14 11:20:18
二叉树的实现(补充) 本次实现的二叉树包括二叉树的先序遍历,中序遍历和后序遍历以及二叉树的层序遍历,还包括二叉树的高度,叶子节点以及反转二叉树 二叉树的层序遍历依然是使用Python内置的deque实现一个队列,根据队列先进先出(FIFO)的性质,先把二叉树的根节点放入队列中,判断队列是否为空,如果 ......
二叉树的实现(补充)
本次实现的二叉树包括二叉树的先序遍历,中序遍历和后序遍历以及二叉树的层序遍历,还包括二叉树的高度,叶子节点以及反转二叉树
二叉树的层序遍历依然是使用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('\n') 174 175 print('后序遍历为:') 176 tree.postorder() 177 print('\n') 178 179 print('中序遍历为:') 180 tree.midorder() 181 print('\n') 182 183 print('层序遍历为:') 184 level_order = tree.levelorder() 185 tree.layer_trav(tree) 186 print('\n') 187 print(level_order) 188 189 print('叶子节点为:') 190 tree.leaves() 191 print('\n') 192 193 print('树的高度为:') 194 print(tree.height()) 195 print('\n') 196 197 print('反转二叉树') 198 tree.reverse() 199 print('先序遍历') 200 tree.preorder() 201 print('\n') 202 print('层序遍历') 203 tree.layer_trav(tree)
上一篇: 生病怎么吃大推荐
下一篇: javaScript知识点总结(必看篇)