算法导论 python代码 第十二章
来源:互联网 发布:人工智能编程语言 编辑:程序博客网 时间:2024/06/04 18:09
# author Ttssxuan# chapter 12# the binary search treeclass Node: ''' the node of the binary tree Properties: p - the parent of the current node left - the left child of the current node right - the right child of the current node value - the value of this node ''' def __init__(self): self.p = None self.left = None self.right = None self.value = None def __init__(self, other): self.p = other.p self.left = other.left self.right = other.right self.value = other.value def __str__(self): return str(self.value)def inorder_tree_walk(x): ''' scan all keys by inorder tree walk method Parameters: x - the root of the binary search tree Returns: none ''' if x != None: inorder_tree_walk(x.left) print(x + " ") inorder_tree_walk(x.right)def interative_inorder_tree_wal(x): ''' 12.1.3 scan all keys by inorder tree walk method with nonrecursive Parameters: x - the root of the binary search tree Returns: none ''' done = False cur = Node(x) stack = [] while(done): if cur != None: stack.append(cur) cur = cur.left else: if len(stack) > 0: cur = stack.pop() print(cur + " ") cur = cur.right else: done = Truedef preorder_tree_walk(x): ''' 12.1.4 scan all keys by preorder tree walk method Parameters: x - the root of the binary search tree Returns: none ''' if x != None: print(x + " ") preorder_tree_walk(x.left) preorder_tree_walk(x.right)def postorder_tree_walk(x): ''' 12.1.4 scan all keys by postorder tree walk method Parameters: x - the root of the binary search tree Returns: none ''' if x != None: postorder_tree_walk(x) postorder_tree_walk(x) print(x + " ")def tree_search(x, k): ''' 12.2 search the given key of the binary search tree Parameters: x - the root of the binary search tree k - the key need to search Returns: the node that contains the k ''' if x == None or k == x.value: return x if k < x.value: return tree_search(x.left, k) else: return tree_search(x.right, k)def interative_tree_search(x, k): ''' 12.2 the unrolling version of searching the binary search tree Parameters: x - the root of the binary search tree k - the key need to search Returns: the node that contains the k ''' while x != None and k != x.value: if k < x.value: x = x.left else: x = x.right return xdef tree_minimum(x): ''' 12.2 find the minimum key of the binary search tree Parameters: x - the root of the binary search tree Returns: the node that contains the minimum key ''' if x == None: return None while x.left != None: x = x.left return xdef tree_maximum(x): ''' 12.2 find the maximum key of the binary search tree Parameters: x - the root of the binary search tree Returns: the node that contains the maximum key ''' if x == None: return None while x.right != None: x = x.right return xdef tree_successor(x): ''' 12.2 find the successor of the given node Parameters: x - the node need to find the successor Returns: the node which contains the successor of x ''' if x.right != None: return tree_minimum(x.right) y = x.p while y != None and x == y.right: x = y y = y.p return Nonedef tree_predecessor(x): ''' 12.2.3 find the predecessor of the given node Parameters: x - the node need to find the predecessor Returns: the node whick contains the predecessor of x ''' if x.left != None: return tree_maximum(x.left) return x.pdef tree_insert(t, x): ''' 12.3 insert the given node into the binary search tree Parameters: t - the given binary search tree x - the node needs to insert into the binary search tree Returns: none ''' y = None x = t.root while x != None: y = x if z.value < x.value: x = x.left else: x = x.right z.p = y if y == None: t.root = z elif z.key < y.key: y.left = z else: y.right = zdef transplant(t, u, v): ''' move subtrees around within the binary search tree Parameters: t - the given binary tree u - the subtree was to be replaced v - the subtree used to replacing the subtree u Return: None ''' if u.p == None: t.root = v elif u == u.p.left: u.p.left = v else: u.p.right = v if v != None: v.p = u.pdef tree_delete(t, z): ''' delete the given node z which belongs to the tree t ''' if z.left == None: transplant(t, z, z.right) elif z.right == None: transplant(t, z, z.left) else: y = tree_minimum(z.right) if y.p != z: transplant(t, y, y.right) y.right = z.right y.right.p = y transplant(t, z, y) y.left = z.left y.left.p = y
0 0
- 算法导论 python代码 第十二章
- 算法导论第十二章
- 算法导论第十二章-二叉搜索树-Cpp代码实现
- 算法导论 第十二章总结
- 算法导论 python代码
- 算法导论 python代码 第二章
- 算法导论 python代码 第四章
- 算法导论 python代码 第六章
- 算法导论 python代码 第七章
- 算法导论 python代码 第八章
- 算法导论 python代码 第九章
- 算法导论 python代码 第十章
- 算法导论第十二章:二叉查找树
- 算法导论 第十二章 二分检索树
- [算法导论]第十二章《二叉搜索树》
- 算法导论 第十二章 二叉搜索树
- 算法导论第十二章——二叉查找树的C++代码实现
- 软件工程导论第十二章
- 基于数组实现Java 自定义Stack栈类及应用
- java基础—GUI概述
- 一维数组和数组指针
- 设计模式-创建型模式#工厂方法FactoryMethod
- linux系统详细目录说明
- 算法导论 python代码 第十二章
- pulltorefresh工程研究(二)
- Bitmap处理 之一 简介
- 使用ANT打包Android应用
- UIScrollView常见属性
- 记录我第一次在Android开发图像处理算法的经历
- linux常用命令总结 (一)
- Pascal's Triangle I,II
- 数据结构之---C语言实现二叉树的顺序存储