12. binary search Trees
12. binary search Trees
The search tree data structure supports many dynamic-set operations,including search ,minimum,maximum,predecessor,successor ,insert ,and delete.
Thus, we can use a search tree both as a dictionary and as a priority queue.
因此,我们拿搜索树既可以用做字典,也可以用作优先队列。
12.1 what is a binary search tree ?
A binary search tree is organized, as the name suggests, in a binary tree, as shown in Figure 12.1.
binary-search-tree property:
Let x be a node in a binary search tree. If y is a node in the left subtree of x, then y.key <=x:key. If y is a node in the right subtree of x, then y:key >= x:key.
二分搜索树的性质:
x是一个二分搜索树,如果y是x的左子树的一个节点,则y.key<=x.key ,如果y是x的右子树,则y.key>=x.key.
The binary-search-tree property allows us to print out all the keys in a binary search tree in sorted order by a simple recursive algorithm, called an inorder tree walk.
This algorithm is so named because it prints the key of the root of a subtree between printing the values in its left subtree and printing those in its right subtree.
12.2 Query a binary search tree.
Besides the SEARCH operation, binary search trees can support such queries as MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR
Searching
Given a pointer to the root of the tree and a key k, TREE-SEARCH returns a pointer to a node with key k if one exists; otherwise, it returns NIL.
Minimum and maximum
Successor and predecessor
Given a node in a binary search tree, sometimes we need to find its successor in the sorted order determined by an inorder tree walk.
给定一个节点,有时候我们需要找到利用in order tree wark 决定的顺序的继任者。
If all keys are distinct, the
successor of a node x is the node with the smallest key greater than x:key.
如果所有的关键字是不同的,那么节点x的接任者就是比x.key 大的最小值。
Insertion and deletion
modifying the tree to insert a new element is relatively straightforward,but handling deletion is somewhat more intricate .
插入相对直接,但是删除可能麻烦些。
INSERTION
DELETION
The overall strategy for deleting a node ́ from a binary search tree T has three basic cases but, as we shall see, one of the cases is a bit tricky.
If z has no children, then we simply remove it by modifying its parent to re- place ́ with NIL as its child.
z没有孩子
If ́ has just one child, then we elevate that child to take ́'s position in the tree by modifying ́'s parent to replace ́ by ́'s child.
If ́ has two children, then we find ́'s successor y—which must be in ́'s right subtree—and have y take ́'s position in the tree. The rest of ́'s original right subtree becomes y's new right subtree, and ́'s left subtree becomes y's new left subtree. This case is the tricky one because, as we shall see, it matters whether y is ́'s right child.
It organizes its cases a bit differently from the three cases outlined previously by considering the four cases shown
In order to move subtrees around within the binary search tree ,we define a subroutine Transplant ,which replaces one subtree as a child of its parent with another subtree.
With the transplant procedure in hand ,here is the procedure that deletes node z from binary search tree T :
12. binary search Trees的更多相关文章
- Method for balancing binary search trees
Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...
- 【二叉查找树】02不同的二叉查找树个数II【Unique Binary Search Trees II】
提到二叉查找树,就得想到二叉查找树的递归定义, 左子树的节点值都小于根节点,右子树的节点值都大于根节点. +++++++++++++++++++++++++++++++++++++++++++++++ ...
- 【二叉查找树】01不同的二叉查找树的个数【Unique Binary Search Trees】
当数组为1,2,3,4,...,n时,基于以下原则构建的BST树具有唯一性: 以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1, n]构成. 我们假定f(i)为以[1,i]能产生的U ...
- 【LeetCode】96. Unique Binary Search Trees 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化递归 动态规划 卡特兰数 日期 题目地址:ht ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 2 Unique Binary Search Trees II_Leetcode
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LEETCODE —— Unique Binary Search Trees [动态规划]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- 2016/05/16 UEditor 文本编辑器 使用教程与使用方法
第一:百度UEditor编辑器的官方下载地址 ueditor 官方地址:http://ueditor.baidu.com/website/index.html 开发文档地址:http://uedito ...
- su: /bin/bash: Permission denied带来的疑惑
>客户一个oracle突然当机了,由于业务启动,客户下意识的重启了服务器,系统是起来了,准备切换到oracle用户下启动数据库,可以怎么都无法su切换,真是火上浇油呀,描述如下: 在root用户 ...
- C项目实践--图书管理系统(3)
接下来将要实现用户管理模块的相关功能,用户管理模块的主要功能包括增加用户,查找用户以及保存用户等功能,查找用户时,如果查找成功,充许对查找到用户进行更新或删除操作.如果查找不成功,则给出相应的提示信息 ...
- Page 实例的生命周期
注册页面 · 小程序 https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/page.html
- Windows7 配置匿名Samba文件共享
1.环境 系统:Windows 7 SP1 IP:192.168.118.151 2.配置 计算机|管理|本地用户和组|用户|Guest-->去掉账户已禁用 cmd|gpedit.msc|本地计 ...
- http_load常见问题及解决方案
1.错误信息:byte count wrong http_load在处理时会去关注每次访问同一个URL返回结果(即字节数)是否一致,若不一致就会抛出byte count wrong 所以动态页面可以忽 ...
- 一个简单的Viewport效果
<script src="../ext-4.2/bootstrap.js" type="text/javascript"></script&g ...
- wamp的手动安装
Wamp的手动安装 (http://www.cnblogs.com/homezzm/archive/2012/08/01/2618062.html) 一.Apache2.4安装 1.修改\Apache ...
- form表单提交的几种方法
form表单提交的几种方法 <form id="myform" name="myform" method="post" onsubmi ...
- [App Store Connect帮助]八、维护您的 App(2)将 App 从 App Store 中移除
如果您不想继续向顾客提供您的 App,您可以将其从 App Store 中移除,这样会移除该 App 的所有版本.拥有该 App 先前版本的用户将无法更新 App,但只要您的合约有效,用户便仍可下载最 ...