二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
查找
public TreeNode search(int val) {
TreeNode cur = root;
while (cur != null) {
if(cur.val < val) {
cur = cur.right;
}else if(cur.val > val) {
cur = cur.left;
}else {
return cur;
}
}
return null;
}
插入
public boolean insert(int key) {
TreeNode node = new TreeNode(key);
//第一次插入的时候
if(root == null) {
root = node;
return true;
}
TreeNode cur = root;
TreeNode parent = null;
while (cur != null) {
if(cur.val < key) {
parent = cur;
cur = cur.right;
}else if(cur.val == key) {
return false;//一样的值 不能进行插入
}else {
parent = cur;
cur = cur.left;
}
}
if(parent.val > key) {
parent.left = node;
}else {
parent.right = node;
}
return true;
}
删除
设待删除结点为 cur, 待删除结点的双亲结点为 parent
public void removeNode(int key) {
TreeNode cur = root;
TreeNode parent = null;
while (cur != null) {
if(cur.val < key) {
parent = cur;
cur = cur.right;
}else if(cur.val > key) {
parent = cur;
cur = cur.left;
}else {
remove(cur,parent);
return;
}
}
}
private void remove(TreeNode cur, TreeNode parent) {
if(cur.left == null) {
if(cur == root) {
root = cur.right;
}else if(cur == parent.left) {
parent.left = cur.right;
}else {
parent.right = cur.right;
}
}else if(cur.right == null) {
if(cur == root) {
root = cur.left;
}else if(parent.left == cur) {
parent.left = cur.left;
}else {
parent.right = cur.left;
}
}else {
//cur的左右两边 都不为空 !!
TreeNode targetParent = cur;
TreeNode target = cur.right;
while (target.left != null) {
targetParent = target;
target = target.left;
}
cur.val = target.val;
if(target == targetParent.left) {
targetParent.left = target.right;
}else {
targetParent.right = target.right;
}
}
}
TreeMap 和 TreeSet 即 java 中利用二叉搜索树实现的 Map 和 Set
实际上用的是红黑树,而红黑树是一棵近似平衡的二叉搜索树
概念和场景
Map和set是一种专门用来进行搜索的容器或者数据结构,其搜索的效率与其具体的实例化子类有关
直接遍历,时间复杂度为O(N),元素如果比较多效率会非常慢
二分查找,时间复杂度为O(logN) ,但搜索前必须要求序列是有序的
直接查找和二分查找都比较适合静态类型的查找
Map和Set是一种适合动态查找(可能在查找时进行一些插入和删除的操作)的集合容器
模型
一般把搜索的数据称为关键字(Key),和关键字对应的称为值(Value),将其称之为Key-value的键值对,所以模型会有两种:
Map中存储的就是key-value的键值对,Set中只存储了Key
Map是一个接口类,该类没有继承自Collection,该类中存储的是
结构的键值对,并且K一定是唯一的,不能重复
1. 关于Map.Entry
Map.Entry
方法 | 解释 |
K getKey() | 返回 entry 中的 key |
V getValue() | 返回 entry 中的 value |
V setValue(V value) | 将键值对中的value替换为指定value |
TreeMap 和 HashMap 的区别
Map底层结构 | TreeMap | HashMap |
底层结构 | 红黑树 | 哈希桶 |
插入/删除/查找时间复杂度 | O(logN) | O(1) |
是否有序 | 关于Key有序 | 无序 |
线程安全 | 不安全 | 不安全 |
插入/删除/查找区别 | 需要进行元素比较 | 通过哈希函数计算哈希地址 |
比较与覆写 | key必须能够比较,否则会抛出 ClassCastException异常 | 自定义类型需要覆写equals和 hashCode方法 |
应用场景 | 需要Key有序场景下 | Key是否有序不关心,需要更高的时间性能 |
Set与Map主要的不同有两点:Set是继承自Collection的接口类,Set中只存储了Key
TreeSet 和 HashSet 的区别
Map底层结构 | TreeMap | HashMap |
底层结构 | 红黑树 | 哈希桶 |
插入/删除/查找时间复杂度 | O(logN) | O(1) |
是否有序 | 关于Key有序 | 无序 |
线程安全 | 不安全 | 不安全 |
插入/删除/查找区别 | 按照红黑树的特性来进行插入和删除 | 先计算key哈希地址,然后进行插入和删除 |
比较与覆写 | key必须能够比较,否则会抛出 ClassCastException异常 | 自定义类型需要覆写equals和 hashCode方法 |
应用场景 | 需要Key有序场景下 | Key是否有序不关心,需要更高的时间性能 |
通过某种函数(hashFunc)使元素的存储位置与它的关键码之间能够建立一一映射的关系,那么在查找时不经过任何比较,一次直接从表中得到要搜索的元素的数据结构
该方式即为哈希(散列)方法,哈希方法中使用的转换函数称为哈希(散列)函数,构造出来的结构称为哈希表(HashTable)(或者称散列表)
哈希函数设置为:hash(key) = key % capacity; capacity为存储元素底层空间总的大小
(1)概念
不同关键字通过相同哈希哈数计算出相同的哈希地址,该种现象称为哈希冲突或哈希碰撞。
把具有不同关键码而具有相同哈希地址的数据元素称为“同义词”
(2)冲突避免
首先,由于我们哈希表底层数组的容量往往是小于实际要存储的关键字的数量的,所以冲突的发生是必然的,我们能做的应该是尽量的降低冲突率
下面介绍两种避免冲突的方法
第一种:哈希函数设计
引起哈希冲突的一个原因可能是:哈希函数设计不够合理。
哈希函数设计原则:
常见哈希函数
哈希桶,链表长度 = 8的时候,会变成一个红黑树,进一步提高效率(不然就要遍历链表了)
从1.8版本开始,用的是尾插法
第二种:负载因子调节
散列表的载荷因子定义:α = 填入表中的元素个数 / 散列表的长度
所以当冲突率达到一个无法忍受的程度时,我们需要通过降低负载因子来变相的降低冲突率。
已知哈希表中已有的关键字个数是不可变的,那我们能调整的就只有哈希表中的数组的大小。
一般默认的的负载因子是0.75,所以每次插入一个元素,都要进行负载因子的计算,如果负载因子过大就要进行扩容
(2)冲突解决
一、闭散列
闭散列:也叫开放定址法,当发生哈希冲突时,如果哈希表未被装满,说明在哈希表中必然还有空位置,那么可以把key存放到冲突位置中的“下一个” 空位置中去
下面是两种找下一个空位置的方法
(1)线性探测
线性探测:从发生冲突的位置开始,依次向后探测,直到寻找到下一个空位置为止。
二、开散列/哈希桶(重点掌握)
开散列法又叫链地址法(开链法),首先对关键码集合用散列函数计算散列地址,具有相同地址的关键码归于同一子集合,每一个子集合称为一个桶,各个桶中的元素通过一个单链表链接起来,各链表的头结点存储在哈希表中
public class HashBucket {
static class Node {
private int key;
private int value;
private Node next;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
public Node[] array;
public int usedSize;
//默认的负载因子为0.75
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
public HashBucket() {
this.array = new Node[10];
}
public void put(int key,int val) {
Node node = new Node(key,val);
int index = key % array.length;//
//遍历index位置下方的链表
Node cur = array[index];
while (cur != null) {
if(cur.key == key) {
cur.value = val;
return;
}
cur = cur.next;
}
//cur == null 没有这个key 那么 进行头插法
node.next = array[index];
array[index] = node;
usedSize++;
//计算负载因子
if(loadFactor() >= DEFAULT_LOAD_FACTOR) {
//扩容!!
resize();
}
}
//重新哈希原来的数据 !!!
private void resize() {
//2倍扩容
Node[] tmpArray = new Node[array.length * 2];
//遍历原来的数组 下标的每个链表
for (int i = 0; i < array.length; i++) {
Node cur = array[i];
while (cur != null) {
Node curNext = cur.next;//需要记录下来 原来链表的下一个节点的位置
int index = cur.key % tmpArray.length;//新数组的位置
//采用头插法 放到新数组的index位置
cur.next = tmpArray[index];//这里修改之后 cur的next已经变了
tmpArray[index] = cur;
cur = curNext;
}
}
array = tmpArray;
}
private float loadFactor() {
return usedSize*1.0f / array.length;
}
/**
* 通过key值 返回value
* @param key
* @return
*/
public int get(int key) {
int index = key % array.length;//
Node cur = array[index];
while (cur != null) {
if(cur.key == key) {
return cur.value;
}
cur = cur.next;
}
return -1;
}
}
public class HashBucket {
static class Node {
private int key;
private int value;
private Node next;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
public Node[] array;
public int usedSize;
//默认的负载因子为0.75
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
public HashBucket() {
this.array = new Node[10];
}
public void put(int key,int val) {
Node node = new Node(key,val);
int index = key % array.length;//
//遍历index位置下方的链表
Node cur = array[index];
while (cur != null) {
if(cur.key == key) {
cur.value = val;
return;
}
cur = cur.next;
}
//cur == null 没有这个key 那么 进行头插法
node.next = array[index];
array[index] = node;
usedSize++;
//计算负载因子
if(loadFactor() >= DEFAULT_LOAD_FACTOR) {
//扩容!!
resize();
}
}
//重新哈希原来的数据 !!!
private void resize() {
//2倍扩容
Node[] tmpArray = new Node[array.length * 2];
//遍历原来的数组 下标的每个链表
for (int i = 0; i < array.length; i++) {
Node cur = array[i];
while (cur != null) {
Node curNext = cur.next;//需要记录下来 原来链表的下一个节点的位置
int index = cur.key % tmpArray.length;//新数组的位置
//采用头插法 放到新数组的index位置
cur.next = tmpArray[index];//这里修改之后 cur的next已经变了
tmpArray[index] = cur;
cur = curNext;
}
}
array = tmpArray;
}
private float loadFactor() {
return usedSize*1.0f / array.length;
}
/**
* 通过key值 返回value
* @param key
* @return
*/
public int get(int key) {
int index = key % array.length;//
Node cur = array[index];
while (cur != null) {
if(cur.key == key) {
return cur.value;
}
cur = cur.next;
}
return -1;
}
}
(3)冲突严重时的解决办法
刚才我们提到了,哈希桶其实可以看作将大集合的搜索问题转化为小集合的搜索问题了,那如果冲突严重,就意味着小集合的搜索性能其实也时不佳的,这个时候我们就可以将这个所谓的小集合搜索问题继续进行转化,例如:
(1)性能分析
虽然哈希表一直在和冲突做斗争,但在实际使用过程中,我们认为哈希表的冲突率是不高的,冲突个数是可控的,也就是每个桶中的链表的长度是一个常数,所以,通常意义下,我们认为哈希表的插入/删除/查找时间复杂度是O(1)
(2)和java类集的关系