Two data structure and algorithm analysis of Queue ——— Second Edition (C)

Source: Internet
Author: User

    • Introduction

Zoo merges, inserts, deletes the minimum time complexity of O (Logn). The two-item queue is a data structure for further improvement of these results. With two queues, the worst time complexity for these three operations is O (LOGN), but the average insertion time complexity is O (1)

    • Two items queue

Two item queue is not a tree, it is a forest, a set of sequence of trees composed of deep forest, called the two queue.

The two-item queue has several properties that are more important

(a) Each tree is a constrained sequence tree, called a two-item tree

(b) k two-tree bk with a height of k by a root node and B0, B1, .... B (k-1) composition

(c) The number of nodes of the two tree with a height of k is 2^k

We can use a combination of two trees to represent a priority queue of any size. For example, a priority queue of size 13 can be represented by b3,b2,b0, with a binary representation of 1101. In this respect, I deeply doubt whether the two queues are generated by binary heuristics.

       

    • Two-item queue operation

Find the smallest item: You only need to find the root node of each two-item tree, so the time complexity is O (logn).

Merge: Complete by adding two queues together. Because of the O (Logn) tree, the time complexity of merging is also O (Logn).

Insert: Insertion is also a merge, except that the inserted node is treated as a B0. Although the sense of time complexity of the insertion is O (logn), it is actually O (1), since there is a certain probability that the two queues that are inserted are not B0.

Delete Minimum: Find the minimum value at the root node and then separate the tree with the minimum value into two queues, then merge the new two queue with the original two queue. The time complexity of each process is O (logn). So the time complexity added up is still O (Logn).

These operations are ultimately merging merge.

    • Code implementation for two-item queues

(1) Two queue declarations:

1typedefstructBinnode *Position; 2typedefstructBinnode *Bintree; 3typedefstructCollection *Binqueue; 4 structBinnode5 {  6 ElementType Element; 7 Position Leftchild; 8 Position Sibling; 9 }; Ten structCollection One {   A     intcurrentsize;  - Bintree Thetrees[maxtree];  - }   the  

The tree Binnode is defined first, and then the forest collection is defined.

Is thetrees, the array is a pointer to each of the two tree. And the representation of the two queues in the structure defined above. As you can see, the root node points only to a sub-node with the most subtree, which points to the individual sibling nodes, so access is necessarily a step-by-step visit.

(2) Merging trees:

Merging tree essence is the change of pointer. Change the two two trees, of course.

 1  2  bintree combinetree (bintree t1,bintree T2)  3  4   if  (T1->element>t2->element)  5  return   Combinetree (T2,T1);   6  t2->sibling = T1->leftchild;   7  t1->leftchild = T2;  8  return   T1;  9 }  

(3) merging of two priority queues (merge):

1 binqueue Merge (binqueue H1, Binqueue H2)2 {  3Bintree T1, T2, Carry =NULL; 4     inti,j; 5     if(h1->currentsize+h2->currentsize>capacity)6Error ("exceed the capacity"); 7H1->currentsize = h1->currentsize + h2->currentsize; //currentsize Meaning: 8      for(i=0, j=1; j2) //j: Used to abort the cycle condition 9     {  TenT1 = h1->Thetrees[i];  OneT2 = h2->Thetrees[i];  A         Switch(!! t1+2*!! t2+4*!!Carry) -         {   -              Case 0://No Trees the              Case 1://Only H1 -                  Break;  -              Case 2:    -H1->thetrees[i] =T2;  +H2->thetrees[i] =NULL;  -                  Break;  +              Case 4://Only Carry AH1->thetrees[i] =Carry;  atCarry =NULL;  -                  Break;  -              Case 3://T1,t2 -Carry =Combinetree (T1,T2);  -H1->thetrees[i] = h2->thetrees[i] =NULL;  -                  Break;  in              Case 5:   -Carry =Combinetree (T1,carry);  toH1->thetrees[i] =NULL;  +                  Break;  -              Case 6:   theCarry =Combinetree (T2,carry);  *H2->thetrees[i] =NULL;  $                  Break; Panax Notoginseng              Case 7:   -H1->thetrees[i] =Carry;  theCarry =Combinetree (T1,T2);  +H2->thetrees[i] =NULL;  A                  Break;  the         }   +     }   -     returnH1;  $} 

In this program, the addition of the switch statement is very good.

Another problem is: How to control the need for a few orders of two queue, which directly lead to the program to cycle several times. Here, the size of the two two-item queue is added, assuming 12, then it should be 4 order, because the 3-order size is 1+2+4 = 9<12, it should be four-step, which is also the way of circular control.

1  for (i=0, j=1; j2)  

(4) Delete minimum Value (deletemin):

1 ElementType deletemin (binqueue H)2 {  3     inti,j; 4     intMintree; 5 Binqueue Deletequeue; 6 Position Deletedtree, Oldroot; 7 ElementType Minitem; 8   9     if(IsEmpty (H))Ten     {   OneError ("Empty binqueue!!");  A         return-Infinity;  -     }   -     //Find the Minmum theMin =Infinity;  -      for(i=0; i<maxtree;i++)   -     {   -         if(H->thetrees[i] && h->thetrees[i]->element<Minitem) +         {   -             //Updata the Minmun +Miniitem = h->thetrees[i]->Element;  AMintree =i;  at         }   -     }   -     //Have found the DeleteTree -DeleteTree = h->Thetrees[mintree];  -Oldroot =DeleteTree;  -DeleteTree = oldroot->Leftchild;  in Free (oldroot);  -    to     //form the Deletequeue +Deletedqueue =Initialize ();  -Deletedqueue->currentsize = (1<<mintree)-1; //Left shift Mintree bit  the    *      for(j=mintree-1; j>=0; j--)   $     {  Panax NotoginsengDELETEDQUEUE-&GT;THETREE[J] =Deletedtree;  -Deletedtree = deletedtree->Sibling;  theDeletedqueue->thetree[j]->sibling =NULL;  +     }   AH->thetrees[minitree] =NULL;  theH->currentsize-= deletedqueue->currentsize+1;  +    - Merge (H,deletedqueue);  $     returnMinitem;  $    -}

Transferred from: http://blog.csdn.net/changyuanchn/article/details/14648463

Two data structure and algorithm analysis of Queue ——— Second Edition (C)

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.