Skip to content

Commit a131fa8

Browse files
committed
[Function add]
1.Binary Tree.
1 parent ead08a0 commit a131fa8

File tree

3 files changed

+148
-4
lines changed

3 files changed

+148
-4
lines changed

Diff for: Algorithm(4th_Edition)/algorithm_note.txt

+60-4
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,66 @@
628628
quickSort3Way(a, lo, lt - 1);
629629
quickSort3Way(a, gt + 1, hi);
630630
}
631-
632-
633-
634-
631+
632+
8. 优先队列(PriorityQueue):
633+
1.并不需要将所有的数组排序,只需要提取出所有数组中优先级最高的元素。
634+
2.优先队列最重要的操作就是删除最大元素和插入元素。
635+
636+
->通过二叉堆实现优先队列:
637+
1.二叉堆表示法binary heap:
638+
每个中间元素都需要三个指针,指向父节点和两个子节点。
639+
在一个二叉堆中,位置k的节点父结点的位置为[k/2], 而它的两个子节点的位置分别为2k和2k+1.
640+
1
641+
/ \
642+
2 3
643+
/ | | \
644+
4 5 6 7
645+
/ | | \
646+
8 9 10 11
647+
一棵大小为N的完全二叉树的高度为[lgN]。
648+
堆的操作会首先进行一些简单的改动,打破堆的状态,然后再遍历堆并按照要求将堆的状态恢复(reheapifying)。
649+
实现:ca.mcmaster.chapter.two.queue.MaxPriorityQueueBinaryStack<T>
650+
public class MaxPriorityQueueBinaryStack<T extends Comparable<T>> implements MaxPriorityQueue<T> {
651+
private T[] pq;
652+
private int N = 0;
653+
public MaxPriorityQueueBinaryStack(int maxN){
654+
pq = (T[]) new Comparable[maxN + 1]; //构建一个存储数量+1长度的数组。
655+
}
656+
public void insert(T t) {
657+
pq[++N] = t; //将最新的数据插入尾部,再听过上分将数据放在正确的位置。
658+
swim(N);
659+
}
660+
public T max() {
661+
return pq[1];
662+
}
663+
public T delMax() {
664+
T max = pq[1]; //将最上方的文件保存,作为输出值。
665+
swap(1, N--); //将最后一个值和最开头的值进行交换,再让最开始的值进行下沉,使树有序。
666+
pq[N + 1] = null;
667+
sink(1);
668+
return max;
669+
}
670+
public Boolean isEmpty() { return N == 0; }
671+
public int size() { return N; }
672+
private Boolean less(int i, int j){ return pq[i].compareTo(pq[j]) < 0; }
673+
private void swap(int i, int j){ T temp = pq[i]; pq[i] = pq[j]; pq[j] = temp; }
674+
private void swim(int k){ //数据上浮,当前节点和父节点进行比较,如果大于父节点则和父节点进行交换
675+
while(k > 1 && less(k/2, k)){ //遍历知道首结点停止。
676+
swap(k, k/2);
677+
k = k/2;
678+
}
679+
}
680+
private void sink(int k){ //数据下沉,让上部的非有序数字下沉至正确位置。
681+
while(2 * k <= N){
682+
int j = 2 * k; //先将数字下沉一层
683+
if(j < N && less(j, j+1)) j++; //判断是否超过了树的总容量, 再比较a[j]和a[j++]的大小,要比大的那个小即可
684+
if(!less(k, j)) break; //如果当前值比大的那个还要大,已经可以退出循环了。
685+
swap(k, j); //如果小于下一层的大值,则和该值进行交换。
686+
k = j;
687+
}
688+
}
689+
}
690+
635691

636692

637693

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ca.mcmaster.chapter.two.queue;
2+
3+
public interface MaxPriorityQueue<T extends Comparable<T>> {
4+
/**
5+
* @Description: Insert an element into the priority queue
6+
* @param t
7+
*/
8+
public void insert(T t);
9+
10+
/**
11+
* @Description: Return the max value from the priority queue.
12+
* @return
13+
*/
14+
public T max();
15+
16+
/**
17+
* @Description: Return and delete the max value from the priority queue.
18+
* @return
19+
*/
20+
public T delMax();
21+
22+
/**
23+
* @Description: Check if current queue is empty.
24+
* @return
25+
*/
26+
public Boolean isEmpty();
27+
28+
/**
29+
* @Description: Return the number of the elements left in the queue.
30+
* @return
31+
*/
32+
public int size();
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package ca.mcmaster.chapter.two.queue;
2+
3+
public class MaxPriorityQueueBinaryStack<T extends Comparable<T>> implements MaxPriorityQueue<T> {
4+
private T[] pq;
5+
private int N = 0;
6+
public MaxPriorityQueueBinaryStack(int maxN){
7+
pq = (T[]) new Comparable[maxN + 1];
8+
}
9+
public void insert(T t) {
10+
pq[++N] = t;
11+
swim(N);
12+
}
13+
public T max() {
14+
return pq[1];
15+
}
16+
public T delMax() {
17+
T max = pq[1];
18+
swap(1, N--);
19+
pq[N + 1] = null;
20+
sink(1);
21+
return max;
22+
}
23+
public Boolean isEmpty() { return N == 0; }
24+
public int size() { return N; }
25+
private Boolean less(int i, int j){ return pq[i].compareTo(pq[j]) < 0; }
26+
private void swap(int i, int j){ T temp = pq[i]; pq[i] = pq[j]; pq[j] = temp; }
27+
private void swim(int k){
28+
while(k > 1 && less(k/2, k)){
29+
swap(k, k/2);
30+
k = k/2;
31+
}
32+
}
33+
private void sink(int k){
34+
while(2 * k <= N){
35+
int j = 2 * k;
36+
if(j < N && less(j, j+1)) j++;
37+
if(!less(k, j)) break;
38+
swap(k, j);
39+
k = j;
40+
}
41+
}
42+
public static void main(String[] args) {
43+
MaxPriorityQueueBinaryStack<Integer> stack = new MaxPriorityQueueBinaryStack<>(100);
44+
stack.insert(1);
45+
stack.insert(8);
46+
stack.insert(2);
47+
stack.insert(4);
48+
stack.insert(5);
49+
stack.insert(3);
50+
stack.insert(6);
51+
for (int i = 0; i < 7; i++) {
52+
System.out.println(stack.delMax());
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)