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的更多相关文章

  1. Method for balancing binary search trees

    Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...

  2. 【二叉查找树】02不同的二叉查找树个数II【Unique Binary Search Trees II】

    提到二叉查找树,就得想到二叉查找树的递归定义, 左子树的节点值都小于根节点,右子树的节点值都大于根节点. +++++++++++++++++++++++++++++++++++++++++++++++ ...

  3. 【二叉查找树】01不同的二叉查找树的个数【Unique Binary Search Trees】

    当数组为1,2,3,4,...,n时,基于以下原则构建的BST树具有唯一性: 以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1, n]构成. 我们假定f(i)为以[1,i]能产生的U ...

  4. 【LeetCode】96. Unique Binary Search Trees 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化递归 动态规划 卡特兰数 日期 题目地址:ht ...

  5. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  6. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  7. 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 ...

  8. 【leetcode】Unique Binary Search Trees (#96)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. LEETCODE —— Unique Binary Search Trees [动态规划]

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. [BZOJ2144]国家集训队 跳跳棋

    题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...

  2. Ubuntu Bochs boot.asm 测试

    /********************************************************************* * Ubuntu Bochs boot.asm 测试 * ...

  3. 并不对劲的bzoj4651:loj2086:uoj222:p1712:[NOI2016]区间

    题目大意 有\(n\)(\(n\leq 5*10^5\))个闭区间\([L_1,R_1],[L_2,R_2],...,[L_n,R_n]\)(\(\forall i\in [1,n],0\leq L_ ...

  4. bzoj 4543 HOTEL 加强版

    题目大意: 求树上取三个点这三个点两两距离相等的方案数 思路: 远古时候的$n^2$做法是换根 但那样无法继续优化了 学习了一波长链剖分 考虑如何在一棵树上进行dp 设$f[i][j]$表示以$i$为 ...

  5. 洛谷P1527 矩阵乘法——二维树状数组+整体二分

    题目:https://www.luogu.org/problemnew/show/P1527 整体二分,先把所有询问都存下来: 然后二分一个值,小于它的加到二维树状数组的前缀和里,判断一遍所有询问,就 ...

  6. CodeForces 721B Journey (DP)

    题意:给定一个有向图,你从1出发到n,走尽可能多的点,并且使总权值不大于t. 析:在比赛时,竟然看成有向图了,就想了好久,感觉dp,但是不会啊...如果是有向图就好做多了,枚举边,然后打印就好,dp[ ...

  7. Ubuntu 安装MTP驱动访问安卓设备(转载)

    转自:http://www.ipython.me/ubuntu/how-to-connect-kindle-with-ubuntu.html 1.安装MTP工具集: mr_liu@i-it:~$ su ...

  8. ZOJ5593:Let's Chat(双指针)

    传送门 题意 给出x个a区间和y个b区间,询问a和b交区间的子区间长度为m的个数 分析 类似于双指针,具体见代码 trick 代码 #include <bits/stdc++.h> usi ...

  9. 洛谷 P3953 逛公园【spfa+记忆化dfs+bfs】

    spfa预处理出最短路数组dis,然后反向建边bfs出ok[u]表示u能到n点 然后发现有0环的话时候有inf解的,先dfs找0环判断即可 然后dfs,设状态f[u][v]为到u点,还可以跑最短路+v ...

  10. JAVA中抽象类不可以实例化,却可以创建数组

    这是我定义的一个抽象类: 如果你试图创建一个对象,当然是不行的,抽象类不能用new运算符创建对象. 这是错误提示,还记得instantiate这个单词吗?在我的这篇随笔第二篇(那些JAVA程序BUG中 ...