Algorithms-3.2 Binary Search Trees 二叉查找树
Binary Search Trees
- 1 BSTs 二叉查找树
- 2 ordered operations in BSTs 有序性相关方法
- 2.1 最大键和最小键
- 2.2 向上取整和向下取整
- 2.3 排名
- 2.3.1 size()
- 2.3.2 rank()
- 2.3.3 中序遍历
- 2.3.4 summary
- 3 deletion 删除操作
- 3.1 删除最小键
- 3.2 Hibbard deletion
- 3.3 summary
1 BSTs 二叉查找树
- no more tree (search miss)
package Chapter03;//基于二叉查找树的符号表
public class BST<Key extends Comparable<Key>, Value>{private Node root; //二叉查找树的根结点//内部类private class Node{private Key key;private Value val;private Node left, right; //指向子树的链接//构造器public Node(Key key, Value val){this.key = key;this.val = val;}}public void put(Key key, Value val){//查找key,找到则更新它的值,否则为它创造一个新的结点root = put(root, key, val);//root根结点}//cost: number of compare is equal to 1 + depth of nodeprivate Node put(Node x, Key key, Value val){//如果key存在于以x为根结点的子树中则更新它的值,否则将以key和val为键值对的新结点插入到该子树中if (x == null) return new Node(key,val);int cmp = key.compareTo(x.key);if (cmp < 0) x.left = put(x.left,key,val);//递归else if (cmp > 0) x.right = put(x.right,key,val);else x.val = val;return x;//最终回到一个node}//return value corresponding to given key, or null if no such key//cost: number of compare is equal to 1 + depth of nodepublic Value get(Key key){Node x = root;while (x != null){int cmp = key.compareTo(x.key); //key和key比较if (cmp < 0) x = x.left;else if (cmp > 0) x = x.right;else return x.val;}return null;}
}
- worst case和链表一样,按字母从小到大的顺序输入的
2 ordered operations in BSTs 有序性相关方法
2.1 最大键和最小键
- 从root开始一直往右,直到找到一个null的右链接,就找到了最大键;同理,一直向左,可找到最小键
public Key min(){return min(root).key;}private Node min(Node x){if (x.left == null) return x;return min(x.left);}
- max()将left和right调换
2.2 向上取整和向下取整
//寻找小于等于key的最大的Keypublic Key floor(Key key){Node x = floor(root, key);if (x == null) return null;return x.key;}private Node floor(Node x, Key key){if (x == null) return null;int cmp = key.compareTo(x.key);if (cmp == 0) return x;if (cmp < 0) return floor(x.left,key);//cmp > 0Node t = floor(x.right,key);if (t != null) return t;//在x.right子树中找到一个nodeelse return x;//如果是空,回到上一级Key,上一级Key即为最终结果,说明右边子树不符合小于等于key的条件}
- 把所有的左变右(同时将小于变成大于)就能得到ceiling()的算法
2.3 排名
2.3.1 size()
package Chapter03;//基于二叉查找树的符号表
public class BST<Key extends Comparable<Key>, Value>{private Node root; //二叉查找树的根结点//内部类private class Node{private Key key;private Value val;private Node left, right; //指向子树的链接private int count;//number of nodes in subtrees//构造器public Node(Key key, Value val, int count){this.key = key;this.val = val;this.count = count;}}public void put(Key key, Value val){//查找key,找到则更新它的值,否则为它创造一个新的结点root = put(root, key, val);//root根结点}//cost: number of compare is equal to 1 + depth of nodeprivate Node put(Node x, Key key, Value val){//如果key存在于以x为根结点的子树中则更新它的值,否则将以key和val为键值对的新结点插入到该子树中if (x == null) return new Node(key,val,1);int cmp = key.compareTo(x.key);if (cmp < 0) x.left = put(x.left,key,val);//递归else if (cmp > 0) x.right = put(x.right,key,val);else x.val = val;x.count = 1 + size(x.left) + size(x.right);//递归return x;//最终回到一个node}public int size(){return size(root);}private int size(Node x){if (x == null) return 0;return x.count;}
}
2.3.2 rank()
- how many keys < k ?
public int rank(Key key){return rank(key, root);}private int rank(Key key, Node x){//返回以x为根结点的子树中小于x.key的键的数量if (x == null) return 0;int cmp = key.compareTo(x.key);if (cmp < 0) return rank(key, x.left);//若key小于根结点,返回key在左子树的排名(递归)else if (cmp > 0) return 1 + size(x.left) + rank(key, x.right);else return size(x.left);//如果key和根结点的key相等,rank就是左边子树的结点总数}
2.3.3 中序遍历
遍历过程如下:
- 访问左子树,
- 访问根节点,
- 访问右子树。
- 队列(queue)是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作。
//中序遍历:迭代public Iterable<Key> keys(){Queue<Key> q = new Queue<Key>();inorder(root, q);return q;}private void inorder(Node x, Queue<Key> q){if (x == null) return;inorder(x.left, q);//递归q.enqueue(x.key);//把根结点加入队列inorder(x.right, q);}
2.3.4 summary
3 deletion 删除操作
- tombstone 墓碑
- 如果有大量的删除操作,内存过载
3.1 删除最小键
//删除最小键public void deleteMin(){root = delete(root);}private Node deleteMin(Node x){if (x.left == null) return x.right;x.left = deleteMin(x.left);//x.left被重新赋值,新的left是原来的left的rightx.count = size(x.left) + size(x.right) + 1;return x;}
3.2 Hibbard deletion
//删除键值对public void delete(Key key){root = delete(root, key);}private Node delete(Node x, Key key){if (x == null) return null;int cmp = key.compareTo(x.key);if (cmp < 0) x.left = delete(x.left, key);//递归else if (cmp > 0) x.right = delete(x.right, key);else{//已找到要删除的键if (x.right == null) return x.left;//no right childif (x.left == null) return x.right;//no left child//replace with successor,有两个子树的情况Node t = x;//x为即将被删除的结点x = min(t.right);//x更新为x的右子树的最小结点,作为新"根"结点x.right = deleteMin(t.right);//将x的右链接x.left = t.left;//把t的左子树安装到后继结点(新x)的左边}x.count = size(x.left) + size(x.right) + 1;//update subtree countsreturn x;}
- 删除会使得二叉树变得不对称
- after a sequence of insert and delete, 树的高度变为square root of N --> 根号N,比期待的logN大很多
3.3 summary
Algorithms-3.2 Binary Search Trees 二叉查找树相关推荐
- Unique Binary Search Trees II -- LeetCode
原题链接: http://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 这道题是求解所有可行的二叉查找树,从 Unique Bi ...
- [LeetCode]: 96: Unique Binary Search Trees
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- 【Unique Binary Search Trees II】cpp
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- LeetCode-Unique Binary Search Trees
研究生开学以来一直在看书看文献,很少有机会去写代码.为了不让以后校招时被虐的太惨,准备每天看一道LeetCode上的题,练练手. 因此准备开一个新的专题,记录下LeetCode上的一些有意思的小的算法 ...
- LeetCode: Unique Binary Search Trees [095]
[题目] Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
- 【LeetCode从零单排】No96 Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ex ...
- LeetCode:Unique Binary Search Trees
问题描写叙述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? F ...
- BST(Binary Search Tree 二叉查找树模版)
/****************************************** 数据结构: BST(Binary Search Tree),二叉查找树;性质: 若结点的左子树不空,则左子树上所 ...
- leetcode 95. Unique Binary Search Trees II | 96. Unique Binary Search Trees
95. Unique Binary Search Trees II https://leetcode.com/problems/unique-binary-search-trees-ii/ 题解 题是 ...
- LeetCode-95-Unique Binary Search Trees II
算法描述: Given an integer n, generate all structurally unique BST's (binary search trees) that store va ...
最新文章
- 财报上的云计算战场: 巨头们垄断加剧
- Openstack组件实现原理 — Glance架构(V1/V2)
- 如何在FBL5N中增加所需字段
- 在linux中查找运行程序句柄,如何查找我的进程在Linux中打开的文件句柄?
- dj鲜生-09-商品应用-首页的显示
- php七牛云rtmp直播推流,GitHub - jangocheng/FlutterQiniucloudLivePlugin: Flutter 七牛云直播云 推流/播放 SDK集成...
- pytorch修改tensor的维度(修改为任意维度,或单纯的增减维度)
- linux系统的文件系统tmpfs,linux里tmpfs文件系统
- python不变的数据结构是_Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法...
- 3.14 在金山做的动态建模讲座
- Tarjan边的双联通
- ENVI辐射定标出现Calibration requires gain and offset for each band错误提示
- Jmeter测试计划无法保存或者另存为
- 【前端】倒计时、秒杀、定时器
- 系统架构设计师 - ESB 企业服务总线
- 计算机无法识别游戏手柄,windows10系统下游戏手柄无法识别或工作怎么解决
- 牛客网——找出直系亲属
- Ballot evaluation
- MATLAB 3db波束宽度提取,秒换算毫秒(3db波束宽度估算公式)
- (转)C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
热门文章
- 国内主流大数据平台对比
- Win10双系统安装教程(适用所有计算机)
- 给3D打印机升级的记录( i3 Mega S)
- 洗衣机测试点 思维导图
- windows7时钟屏保怎么设置_w7时钟屏保怎么设置
- 关于a标签 中特有的tilte属性 和 字符串文字换行
- python中re模块的span_python3正则模块re的使用方法详解
- html背景左右渐变,css 背景 上下渐变 左右渐变
- android改手机名称,如何修改手机 App 的名称?「App Name Editor」可轻松自订(Android)...
- 格力集团投资小米,或是对董明珠和格力电器的复仇