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 ...
随机推荐
- jQuery的AJax异步訪问
用一个样例用以说明:点击button,将input内用户输入的数据发送给服务端.并将结果返回给页面. 首先是html承载内容: <!DOCTYPE html> <html> & ...
- 2014/4/18 ① button与submit的区别 ②现象 : 数据库中其他值可以取到 有的却取不到 解决 看获取时“#”有无
①<input type="button" /> 这就是一个按钮.如果你不写javascript 的话,按下去什么也不会 发生. <input type=&quo ...
- <label>标签for属性
label 元素不会向用户呈现任何特殊效果.不过,它为鼠标用户改进了可用性.如果您在 label 元素内点击文本,就会触发此控件.就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控 ...
- Linux MTD下获取Nand flash各个参数的过程的详细解析【转】
本文转载自:https://www.crifan.com/files/doc/docbook/nand_get_type/release/html/nand_get_type.html 文章不错可以看 ...
- adbi命令【转】
本文转载自:https://zmywly8866.github.io/2015/01/24/all-adb-command.html ADB很强大,记住一些ADB命令有助于提高工作效率. 获取序列 ...
- ssh配置无密码登录
1.在master机器上生成公钥: [root@master ~]# ssh-keygen -t rsa 注:一直按enter键就可以生成了 Generating public/private ...
- 优化VMware提高虚拟机运行速度的技巧
vmware虚拟机如何设置不当的话会造成运行速度慢,并影响主机运行,甚至会出现死机. 以下是提高vmware虚拟机运行速度的几个技巧: 文章来自:http://blog.csdn.net/shanzh ...
- oracle分区表有什么作用
oracle分区表有什么作用 https://zhidao.baidu.com/question/1818955865408544348.html (1) 表空间及分区表的概念 表空间: 是一个或多个 ...
- Linux 常用命令二 pwd cd
一.pwd命令 显示整个路径名: wang@wang:~$ pwd /home/wang 二.cd命令 切换到其他路径(相对路径方式): wang@wang:~$ cd workpalce/ wang ...
- python 高阶函数二 map()和reduce()
一.map()函数 map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. >>> fro ...