Skip to content

Commit

Permalink
TreeSet
Browse files Browse the repository at this point in the history
  • Loading branch information
wanwanpp committed Mar 9, 2018
1 parent fcc08d3 commit 749239c
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 111 deletions.
15 changes: 15 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/smartfox_info.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 28 additions & 8 deletions src/java/util/PriorityQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ private void initFromCollection(Collection<? extends E> c) {
*
* @param minCapacity the desired minimum capacity
*/
/**
* 若原长度小于64,就扩容大概为2倍,否则1.5倍。
*/
private void grow(int minCapacity) {
int oldCapacity = queue.length;
// Double size if small; else grow by 50%
Expand Down Expand Up @@ -315,6 +318,13 @@ public boolean add(E e) {
* according to the priority queue's ordering
* @throws NullPointerException if the specified element is null
*/
//添加元素

/**
* 1.检查是否需要扩容
* 2.若队列为空,就直接添加第一个元素到queue[0]
* 3.若不为空,就按照堆的方式,先添加到最后一个位置,然后自底向上调整。
*/
public boolean offer(E e) {
if (e == null)
throw new NullPointerException();
Expand All @@ -330,6 +340,7 @@ public boolean offer(E e) {
return true;
}

// 返回数组第一个元素
public E peek() {
if (size == 0)
return null;
Expand Down Expand Up @@ -566,15 +577,18 @@ public void clear() {
size = 0;
}

//删除头结点
//思想是把最后一个元素放到根节点,删掉最后一个元素,然后根节点自顶向下调整。
public E poll() {
if (size == 0)
return null;
int s = --size;
modCount++;
E result = (E) queue[0];
E x = (E) queue[s];
queue[s] = null;
E result = (E) queue[0]; //头结点,result记录方便后面返回
E x = (E) queue[s]; //最后一个元素
queue[s] = null; //最后一个元素置为null
if (s != 0)
//向下调整
siftDown(0, x);
return result;
}
Expand Down Expand Up @@ -622,6 +636,11 @@ private E removeAt(int i) {
* @param k the position to fill
* @param x the item to insert
*/
/**
* 自底向上调整
* 分为用comparator和comparable两种
* 若小于父节点,就交换位置,继续递归比较,指定大于或等于父节点
*/
private void siftUp(int k, E x) {
if (comparator != null)
siftUpUsingComparator(k, x);
Expand All @@ -641,14 +660,14 @@ private void siftUpComparable(int k, E x) {
}
queue[k] = key;
}

//若小于父节点,就交换位置,继续递归比较,指定大于父节点
private void siftUpUsingComparator(int k, E x) {
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = queue[parent];
if (comparator.compare(x, (E) e) >= 0)
break;
queue[k] = e;
queue[k] = e; //父节点元素移到当前元素位置
k = parent;
}
queue[k] = x;
Expand All @@ -669,15 +688,16 @@ private void siftDown(int k, E x) {
siftDownComparable(k, x);
}

//选出最小的孩子节点与当前节点比较,若小于孩子节点则不变,否则孩子节点上移,继续循环直到当前节点小于孩子节点。
private void siftDownComparable(int k, E x) {
Comparable<? super E> key = (Comparable<? super E>)x;
int half = size >>> 1; // loop while a non-leaf
while (k < half) {
while (k < half) { //表示k节点右孩子节点。
int child = (k << 1) + 1; // assume left child is least
Object c = queue[child];
int right = child + 1;
if (right < size &&
((Comparable<? super E>) c).compareTo((E) queue[right]) > 0)
//选择最小的孩子节点用于比较
if (right < size && ((Comparable<? super E>) c).compareTo((E) queue[right]) > 0)
c = queue[child = right];
if (key.compareTo((E) c) <= 0)
break;
Expand Down
5 changes: 2 additions & 3 deletions src/java/util/TreeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@ else if (cmp > 0)
parent.left = e;
else
parent.right = e;

fixAfterInsertion(e);//调整树的结构,使之符合红黑树的约束
//调整树的结构,使之符合红黑树的约束
fixAfterInsertion(e);
size++;
modCount++;
return null;
Expand Down Expand Up @@ -2311,7 +2311,6 @@ private void deleteEntry(Entry<K, V> p) {
// Start fixup at replacement node, if it exists.
Entry<K, V> replacement = (p.left != null ? p.left : p.right);//p的孩子节点


if (replacement != null) {//不为叶子节点
// Link replacement to parent
// 建立被删节点父节点与被删节点孩子节点的关系,被删节点只有一个孩子节点
Expand Down
Loading

0 comments on commit 749239c

Please sign in to comment.