Ing binary heap

Source: Internet
Author: User

Shiniu


[Html]
Namespace MappingBinaryHeap {
/*
DS:
Datastructure to show the value
Heap:
1. Ds: value
2. idx: index
Pos:
The position for each index
Len:
The volum n of heap
Hh:
Heap
Push:
Insert an element
Pop:
Pop an element:
1. pop (pos []) pop the element index
2. pop (1) pop the 'Max' one
*/
Struct DS {
Int next;
DS (){}
DS (int x): next (x ){}
Bool operator <(const DS & A) const {
If (next =-1)
Return true;
If (A. next =-1)
Return false;
Return next> A. next;
}
Void init (){
Next = 0;
}
};
# Define maxn100005
Struct Heap {
Int idx;
DS val;
} Hh [maxn];
Int pos [maxn];
Int len;
Bool Prior (Heap a, Heap B ){
Return a. val <B. val;
}
Void Push (Heap s ){
Int I;
For (I = ++ len; I> 1 & Prior (s, hh [I/2]); I/= 2 ){
Hh [I] = hh [I/2];
Pos [hh [I]. idx] = I;
}
Hh [I] = s;
Pos [hh [I]. idx] = I;
}
Heap Pop (int idx ){
If (idx =-1)
Return hh [0];
Heap ret = hh [idx];
Heap last = hh [len --];
Int I, s;
For (I = idx; I * 2 <= len; I = s ){
S = I * 2;
If (s + 1 <= len & Prior (hh [s + 1], hh [s]) {
S ++;
}
If (Prior (hh [s], last )){
Hh [I] = hh [s];
Pos [hh [I]. idx] = I;
} Else {
Break;
}
}
Hh [I] = last;
Pos [hh [I]. idx] = I;
For (I = idx; I> 1 & Prior (hh [I], hh [I/2]); I/= 2 ){
Heap buf = hh [I];
Hh [I] = hh [I/2];
Hh [I/2] = buf;
Pos [hh [I]. idx] = I;
Pos [hh [I/2]. idx] = I/2;
}
Return ret;
}
Void init (){
Hh [0]. val. init ();
Len = 0;
}
};

Namespace MappingBinaryHeap {
/*
DS:
Datastructure to show the value
Heap:
1. Ds: value
2. idx: index
Pos:
The position for each index
Len:
The volum n of heap
Hh:
Heap
Push:
Insert an element
Pop:
Pop an element:
1. pop (pos []) pop the element index
2. pop (1) pop the 'Max' one
*/
Struct DS {
Int next;
DS (){}
DS (int x): next (x ){}
Bool operator <(const DS & A) const {
If (next =-1)
Return true;
If (A. next =-1)
Return false;
Return next> A. next;
}
Void init (){
Next = 0;
}
};
# Define maxn100005
Struct Heap {
Int idx;
DS val;
} Hh [maxn];
Int pos [maxn];
Int len;
Bool Prior (Heap a, Heap B ){
Return a. val <B. val;
}
Void Push (Heap s ){
Int I;
For (I = ++ len; I> 1 & Prior (s, hh [I/2]); I/= 2 ){
Hh [I] = hh [I/2];
Pos [hh [I]. idx] = I;
}
Hh [I] = s;
Pos [hh [I]. idx] = I;
}
Heap Pop (int idx ){
If (idx =-1)
Return hh [0];
Heap ret = hh [idx];
Heap last = hh [len --];
Int I, s;
For (I = idx; I * 2 <= len; I = s ){
S = I * 2;
If (s + 1 <= len & Prior (hh [s + 1], hh [s]) {
S ++;
}
If (Prior (hh [s], last )){
Hh [I] = hh [s];
Pos [hh [I]. idx] = I;
} Else {
Break;
}
}
Hh [I] = last;
Pos [hh [I]. idx] = I;
For (I = idx; I> 1 & Prior (hh [I], hh [I/2]); I/= 2 ){
Heap buf = hh [I];
Hh [I] = hh [I/2];
Hh [I/2] = buf;
Pos [hh [I]. idx] = I;
Pos [hh [I/2]. idx] = I/2;
}
Return ret;
}
Void init (){
Hh [0]. val. init ();
Len = 0;
}
};

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.