The computer we use every day can run multiple applications at the same time, without feeling stuck, the computer assigns a priority to events for each application, and mobile phones are usually the highest priority, whether we're watching a movie or texting as long as we have a phone call. At this point we need a reasonable data structure to delete the largest element and the insertion element, which we can call the priority queue. The most appropriate data structure for implementing this priority queue can be achieved through the classic two-fork heap, which is more efficient than other data structures.
Priority Queue
We need to understand a few concepts before starting the code:
1. When each node of a binary tree is greater than or equal to two of its child nodes, it is called a heap order.
2. The two-fork heap is a set of elements that can be sorted by a full binary tree in a heap order and stored in the array as a hierarchy.
In a heap, the position of the parent node of the node indexed as index is INDEX/2, the location of the child nodes is 2*index and 2*index+1, and the insertion of a complete binary tree with an array and the deletion of the largest element are very concise.
Core code:
-(void) Insert: (NSString *) value{[Self.datasource Addobject:value]; self.maxindex=self.maxindex+1; [Self swimUp:self.maxIndex];} -(void) deletemax{[self swap:1 secondindex:self.maxindex];//first node and last node for Exchange [Self.datasource removeobjectatindex:s elf.maxindex--];//Delete the last node [self sinkdown:1];//The order of the recovery heap}-(void) Swimup: (Nsinteger) index{//parent node is less than the current child node while (index& Gt;1&&[self LESSCOMPARE:INDEX/2 Secondindex:index]) {[self swap:index/2 secondindex:index]; INDEX=INDEX/2; }}-(void) Sinkdown: (Nsinteger) index{while (2*index<=self.maxindex) {Nsinteger i=2*index; The left and right node size is judged if (i<self.maxindex&&[self lesscompare:i secondindex:i+1]) {i++; } if (![ Self Lesscompare:index secondindex:i]) break; [Self swap:index secondindex:i]; Index=i; }}-(BOOL) Lesscompare: (Nsinteger) FirstIndex Secondindex: (nsinteger) secondindex{return [[Self.datasource Objectatindex:firstindex] IntegervaLue]<[[self.datasource Objectatindex:secondindex] integervalue];} -(void) Swap: (Nsinteger) FirstIndex Secondindex: (nsinteger) secondindex{nsstring *temp=[self.datasource Objectatindex:firstindex]; Self.datasource[firstindex]=[self.datasource Objectatindex:secondindex]; Self.datasource[secondindex]=temp;}
Insert element Call INSERT, delete maximum element call Deletemax, note that the array is starting from 1 in order to conveniently use the nature of the two fork tree, the first 0 bits are not used:
Priorityqueue *queue=[[priorityqueue Alloc]init]; Nsmutablearray *arr=[[nsmutablearray alloc]initwithcapacity:10]; [arr addobject:[nsnull null]]; [Arr addobject:@ "8"]; [Arr addobject:@ "5"]; [Arr addobject:@ "6"]; [Arr addobject:@ "2"]; [Arr addobject:@ "3"]; [Arr addobject:@ "4"]; Queue.datasource=arr; Queue.maxindex=[arr count]-1; [Queue insert:@ "7"];// [queue Deletemax]; for (Nsinteger I=1; I<[queue.datasource count]; i++) { NSLog (@ "Value:%@", Queue.datasource[i]); } NSLog (@ "iOS technology Group: 228407086"); NSLog (@ "Original address: Http://www.cnblogs.com/xiaofeixiang");
Test results:
Heap Order
The above demo is organized from the very beginning, but in many cases we are in a disorderly heap and need to re-construct the order by ourselves, divided into two steps:
1. First turn the heap into a large heap, which is the largest heap.
2. Delete the largest element repeatedly, the last element is swapped with the first one, and then the sink operation (as in the order of the elements in the heap is deleted).
-(void) sort{ nsinteger count=[self.datasource count]-1; For (Nsinteger K=COUNT/2, k>=1; k--) { [self sinksort:k count:count]; } while (count>1) { [self swap:1 secondindex:count--]; [Self sinksort:1 count:count];} } -(void) Sinksort: (nsinteger) Index count: (Nsinteger) count{while (2*index<=count) { Nsinteger i=2* index; The left and right node size is judged if (i<count&&[self lesscompare:i secondindex:i+1]) { i++; } if (![ Self Lesscompare:index secondindex:i]) break; [Self Swap:index secondindex:i]; index=i;} }
Test code:
Priorityqueue *queue=[[priorityqueue Alloc]init]; Nsmutablearray *arr=[[nsmutablearray alloc]initwithcapacity:10]; [arr addobject:[nsnull null]]; [Arr addobject:@ "1"]; [Arr addobject:@ "2"]; [Arr addobject:@ "3"]; [Arr addobject:@ "4"]; [Arr addobject:@ "5"]; [Arr addobject:@ "6"]; [Arr addobject:@ "7"]; Queue.datasource=arr; Queue.maxindex=[arr count]-1; [Queue sort]; for (Nsinteger I=1; I<[queue.datasource count]; i++) { NSLog (@ "Value:%@", Queue.datasource[i]); } NSLog (@ "iOS technology Group: 228407086"); NSLog (@ "Original address: Http://www.cnblogs.com/xiaofeixiang");
The above test is the worst case comparison, look at the effect:
Monday good mood, welcome to explore ~
Algorithm-priority queue and heap sequencing