二叉树
节点定义
1 class Node(object): 2 def __init__(self, item): 3 self.item = item 4 self.left = None 5 self.right = None
二叉树定义
1 class Tree(object): 2 def __init__(self): 3 self.root = None 4 5 def add(self, item): 6 node = Node(item) 7 # 空树直接插入 8 if self.root == None: 9 self.root = node 10 11 else: 12 cur_list = [self.root] 13 while True: 14 # 定义一个列表存储当前遍历节点 15 cur_node = cur_list.pop(0) 16 17 # 当前节点左孩子为空直接插入,否则将该左孩子追加到遍历列表,以便当前节点右孩子也为非空时继续遍历下一层 18 if cur_node.left == None: 19 cur_node.left = node 20 break 21 else: 22 cur_list.append(cur_node.left) 23 # 当前节点右孩子为空直接插入,否则将该右孩子追加到遍历列表,以便继续遍历下一层 24 if cur_node.right == None: 25 cur_node.right = node 26 break 27 else: 28 cur_list.append(cur_node.right) 29 30 # 广度遍历 31 def travel(self): 32 if not self.root: 33 print(None) 34 return None 35 q = [self.root] 36 while q: 37 cur_node = q.pop(0) 38 print(cur_node.item) 39 if cur_node.left: 40 q.append(cur_node.left) 41 if cur_node.right: 42 q.append(cur_node.right) 43 44 # 深度遍历----前序遍历DLR 45 def dlr(self, node): 46 if node == None: 47 return 48 print(node.item) 49 self.dlr(node.left) 50 self.dlr(node.right) 51 52 # 深度遍历----中序遍历LDR 53 def ldr(self, node): 54 if node == None: 55 return 56 self.ldr(node.left) 57 print(node.item) 58 self.ldr(node.right) 59 60 # 深度遍历----后序遍历LRD 61 def lrd(self, node): 62 if node == None: 63 return 64 self.lrd(node.left) 65 self.lrd(node.right) 66 print(node.item)
二叉树使用
1 tree = Tree() 2 tree.add(1) 3 tree.add(2) 4 tree.add(3) 5 tree.add(4) 6 tree.add(5) 7 print("----广度遍历----") 8 tree.travel() 9 print("----深度遍历:DLR----") 10 tree.dlr(tree.root) 11 print("----深度遍历:LDR----") 12 tree.ldr(tree.root) 13 print("----深度遍历:LRD----") 14 tree.lrd(tree.root)
排序二叉树
节点定义
1 class Node(object): 2 def __init__(self, item): 3 self.item = item 4 self.left = None 5 self.right = None
排序二叉树定义
1 class SortTree(object): 2 def __init__(self): 3 self.root = None 4 5 def insert(self, item): 6 node = Node(item) 7 cur_node = self.root 8 if not self.root: 9 self.root = node 10 else: 11 while True: 12 if node.item < cur_node.item: 13 if cur_node.left == None: 14 cur_node.left=node 15 break 16 else: 17 cur_node=cur_node.left 18 else: 19 if cur_node.right == None: 20 cur_node.right=node 21 break 22 else: 23 cur_node=cur_node.right 24 25 # 广度遍历 26 def travel(self): 27 if not self.root: 28 print(None) 29 return None 30 q = [self.root] 31 while q: 32 cur_node = q.pop(0) 33 print(cur_node.item) 34 if cur_node.left: 35 q.append(cur_node.left) 36 if cur_node.right: 37 q.append(cur_node.right) 38 39 # 深度遍历----前序遍历DLR 40 def dlr(self, node): 41 if node == None: 42 return 43 print(node.item) 44 self.dlr(node.left) 45 self.dlr(node.right) 46 47 # 深度遍历----中序遍历LDR 48 def ldr(self, node): 49 if node == None: 50 return 51 self.ldr(node.left) 52 print(node.item) 53 self.ldr(node.right) 54 55 # 深度遍历----后序遍历LRD 56 def lrd(self, node): 57 if node == None: 58 return 59 self.lrd(node.left) 60 self.lrd(node.right) 61 print(node.item)
排序二叉树使用
1 tree=SortTree() 2 tree.insert(1) 3 tree.insert(30) 4 tree.insert(8) 5 tree.insert(20) 6 tree.ldr(tree.root)