Skip to content

Commit ed32f14

Browse files
committed
[Conclusion]
1.Binary Tree
1 parent c70529d commit ed32f14

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

Algorithm(4th_Edition)/Notes/QueueConclusion.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* ArrayBlockingQueue :一个由数组支持的有界队列。
2323
* 如果到达了上界,将无法添加新的元素进入。
2424
* FIFO
25+
>ArrayBlockingQueue在构造时需要指定容量, 并可以选择是否需要公平性,如果公平参数被设置true,等待时间最长的线程会优先得到处理(其实就是通过将ReentrantLock设置为true来 达到这种公平性的:即等待时间最长的线程会先操作)。通常,公平性会使你在性能上付出代价,只有在的确非常需要的时候再使用它。它是基于数组的阻塞循环队 列,此队列按 FIFO(先进先出)原则对元素进行排序。
2526
```Java
2627
public boolean offer(E e) {
2728
checkNotNull(e);
@@ -87,6 +88,7 @@
8788
#### LinkedBlockingQueue
8889
* 一个由链接节点支持的可选有界队列。
8990
* 内部维护了一个Node类
91+
>LinkedBlockingQueue的容量是没有上限的(说的不准确,在不指定时容量为Integer.MAX_VALUE,不要然的话在put时怎么会受阻呢),但是也可以选择指定其最大容量,它是基于链表的队列,此队列按 FIFO(先进先出)排序元素。
9092
```Java
9193
static class Node<E> {
9294
E item;
@@ -160,6 +162,7 @@ static class Node<E> {
160162

161163
#### PriorityBlockingQueue
162164
* 一个由优先级堆支持的无界优先级队列。
165+
>PriorityBlockingQueue是一个带优先级的 队列,而不是先进先出队列。元素按优先级顺序被移除,该队列也没有上限(看了一下源码,PriorityBlockingQueue是对 PriorityQueue的再次包装,是基于堆数据结构的,而PriorityQueue是没有容量限制的,与ArrayList一样,所以在优先阻塞 队列上put时是不会受阻的。虽然此队列逻辑上是无界的,但是由于资源被耗尽,所以试图执行添加操作可能会导致 OutOfMemoryError),但是如果队列为空,那么取元素的操作take就会阻塞,所以它的检索操作take是受阻的。另外,往入该队列中的元 素要具有比较能力。
163166
```Java
164167
public boolean offer(E e) {
165168
if (e == null)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 二叉树 Binary Tree
2+
3+
## 二叉树的优势
4+
>在实际使用时会根据链表和有序数组等数据结构的不同优势进行选择。有序数组的优势在于二分查找,链表的优势在于数据项的插入和数据项的删除。但是在有序数组中插入数据就会很慢,同样在链表中查找数据项效率就很低。综合以上情况,二叉树可以利用链表和有序数组的优势,同时可以合并有序数组和链表的优势,二叉树也是一种常用的数据结构。
5+
6+
* 构成树的基本元素是结点, 一个结点存储了两个指针left, right, 键(用于定位node的位置), 值。
7+
```Java
8+
protected class Node{
9+
protected K k;
10+
protected V v;
11+
protected Node left, right;
12+
protected int N; //当前节点,以及其子结点的所有的节点的个数。
13+
public Node(K k, V v, int n) {
14+
this.k = k; this.v = v; N = n;
15+
}
16+
}
17+
```
18+
* 检查当前节点以及其子结点的个数。
19+
```Java
20+
public Integer size(Node n){
21+
if(null == n) return 0;
22+
return n.N;
23+
}
24+
```
25+
26+
* 向二叉树中插入元素, 通过递归向二叉树中插入元素。
27+
```Java
28+
protected Node root; //对一个二叉树类维护了一个root节点
29+
private Node put(Node node, K k, V v){
30+
if(null == node) return new Node(k, v, 1);
31+
if(k.compareTo(node.k) < 0) node.left = put(node.left, k, v);
32+
else if(k.compareTo(node.k) > 0) node.right = put(node.right, k, v);
33+
else node.v = v;
34+
node.N = size(node.left) + size(node.right) + 1;
35+
return node;
36+
}
37+
public void put(K k, V v){
38+
root = put(root, k, v);
39+
}
40+
```
41+
42+
* 从二叉树中获取元素
43+
```Java
44+
private V get(Node node, K k){
45+
if(null == node) return null;
46+
if(k.compareTo(node.k) < 0) return get(node.left, k);
47+
else if (k.compareTo(node.k) > 0) return get(node.right, k);
48+
else return node.v;
49+
}
50+
public V get(K k){
51+
return get(root, k);
52+
}
53+
```

0 commit comments

Comments
 (0)