二叉搜索树
Posted yu-liang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉搜索树相关的知识,希望对你有一定的参考价值。
二叉搜索树
1 class Node: 2 def __init__(self, key, right, left, p): 3 ‘‘‘节点(关键字,左孩子,右孩子,父节点)‘‘‘ 4 self.key = key 5 self.right = right 6 self.left = left 7 self.p = p 8 9 class Binary_search_tree: 10 ‘‘‘二叉搜索树‘‘‘ 11 def __init__(self, root): 12 ‘‘‘以根节点初始化一棵树‘‘‘ 13 self.root = root 14 def tree_insert(self, z): 15 ‘‘‘在树中插入节点‘‘‘ 16 y = None 17 x = self.root 18 while x != None: 19 y = x 20 if z.key < x.key: 21 x = x.left 22 else: 23 x = x.right 24 z.p = y 25 if y == None: 26 self.root = z 27 elif z.key < y.key: 28 y.left = z 29 else: 30 y.right = z 31 32 def inorder_tree_walk(self, x): 33 ‘‘‘中序遍历‘‘‘ 34 if x != None: 35 self.inorder_tree_walk(x.left) 36 print(x.key) 37 self.inorder_tree_walk(x.right) 38 39 root=Node(6,None,None,None) 40 tree=Binary_search_tree(root) 41 List=[5,2,5,7,8] 42 for i in List: 43 node=Node(i,None,None,None) 44 tree.tree_insert(node) 45 print(tree) 46 print(tree.inorder_tree_walk(tree.root)) 47 ------------------------------------------------------- 48 <__main__.Binary_search_tree object at 0x032B7890> 49 2 50 5 51 5 52 6 53 7 54 8 55 None
1 def tree_search(tree, x, k): 2 ‘‘‘查找一个具有给定关键字的节点,x表示指向树根的指针‘‘‘ 3 if x == None or k == x.key: 4 return x 5 if k < x.key: 6 return tree_search(tree,x.left, k) 7 else: 8 return tree_search(tree,x.right, k) 9 10 def iterative_tree_search(tree, x, k): 11 ‘‘‘迭代的树的搜索‘‘‘ 12 while x != None and k != x.key: 13 if k < x.key: 14 x = x.left 15 else: 16 x = x.right 17 return x 18 19 def tree_minimum(tree, x): 20 ‘‘‘最小关键字元素,最左边的节点‘‘‘ 21 while x.left != None: 22 x = x.left 23 return x 24 25 def tree_maximum(tree, x): 26 ‘‘‘最大关键字元素,最右边的节点‘‘‘ 27 while x.right != None: 28 x = x.right 29 return x 30 31 def tree_successor(tree, x): 32 ‘‘‘树的前继‘‘‘ 33 if x.right != None: 34 return tree_minimum(tree,x.right) 35 y = x.p 36 while y != None and x == y.right: 37 x = y 38 y = y.p 39 return y 40 41 root=Node(15,None,None,None) 42 tree=Binary_search_tree(root) 43 List=[6,18,3,7,17,20,2,4,13,9] 44 for i in List: 45 node=Node(i,None,None,None) 46 tree.tree_insert(node) 47 print(tree) 48 print(tree_search(tree, root, 13).key) 49 print(tree_maximum(tree,root).key) 50 print(tree_minimum(tree,root).key) 51 print(tree_successor(tree,tree_search(tree, root, 13)).key) 52 --------------------------------------------------------------------- 53 <__main__.Binary_search_tree object at 0x0391B530> 54 13 55 20 56 2 57 15
删除节点
1 def tree_delete(tree, z): 2 ‘‘‘树的删除节点,分情况讨论‘‘‘ 3 if z.left == None: 4 #1.z没有左孩子节点,用右孩子来替代他 5 transplant(tree,z, z.right) 6 elif z.right == None: 7 #2.若z没有右孩子,用他的左孩子来替代他 8 transplant(tree,z, z.left) 9 else: 10 #如果有两个孩子,在z的右子树中找他的后继(最小关键字),并让y替代z的位置 11 y = tree_minimum(tree,z.right) 12 if y.p != z: 13 #若y不是z的左孩子,用y右孩子替换y并成为y的双亲的一个孩子,再将z的右孩子转换为y的右孩子 14 transplant(tree,y, y.right) 15 y.right = z.right 16 y.right.p = y 17 #用y替换z并成为z的双亲的一个孩子,再用z的左孩子替换为y的左孩子 18 transplant(tree,z, y) 19 y.left = z.left 20 y.left.p = y 21 22 def transplant(tree, u, v): 23 ‘‘‘删除u后,树的替代,以一棵v为根的子树来替换一棵u为根的子树 24 结果:u的双亲变为v的双亲,v成为u的双亲的相应孩子‘‘‘ 25 if u.p == None: 26 #1.若u是根节点,直接初始化一个v为根节点的树 27 tree.root = v 28 elif u == u.p.left: 29 #2.若u是他父节点的左孩子,更新他父节点的左孩子为v 30 u.p.left = v 31 else: 32 #3.若u是他父节点的右孩子,更新他父节点的右孩子为v 33 u.p.right = v 34 if v != None: 35 #如果v不为空,u的父节点变为v的父节点 36 v.p = u.p 37 38 root=Node(15,None,None,None) 39 tree=Binary_search_tree(root) 40 List=[6,18,3,7,17,20,2,4,13,9] 41 for i in List: 42 node=Node(i,None,None,None) 43 tree.tree_insert(node) 44 45 print(tree.inorder_tree_walk(tree.root)) 46 print(‘#‘*30) 47 tree_delete(tree,tree_search(tree, root, 13)) 48 print(tree.inorder_tree_walk(tree.root)) 49 ------------------------------------------------------------------------ 50 2 51 3 52 4 53 6 54 7 55 9 56 13 57 15 58 17 59 18 60 20 61 None 62 ############################## 63 2 64 3 65 4 66 6 67 7 68 9 69 15 70 17 71 18 72 20 73 None
以上是关于二叉搜索树的主要内容,如果未能解决你的问题,请参考以下文章
代码随想录Day20-Leetcode654.最大二叉树,617.合并二叉树,700.二叉搜索树中的搜索,98.验证二叉搜索树