Leftist Heap/Tree Introduction and code, leftistheap

Source: Internet
Author: User

Leftist Heap/Tree Introduction and code, leftistheap

The left-side tree is a common priority queue (HEAP) structure. Compared with the binary heap, the left-side tree can efficiently merge the two stacks.

The left-side tree is easy to implement, with low programming complexity and high efficiency.

One of its common applications is to work with the query set. Use the query set to determine whether two elements are in the same set, and use the left-side tree to determine the elements with the highest priority in a set.

 

1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 5 template <class T> 6 struct HeapNode 7 {8 typedef HeapNode <T> Node; 9 Node * lch; 10 Node * rch; 11 T val; 12 int dist; 13 14 HeapNode (const T & _ val): lch (0), rch (0 ), val (_ val), dist (0) {} 15 16 void clear () 17 {18 if (lch) lch-> clear (); 19 if (rch) rch-> clear (); 20 delete this; 21} 22}; 23 24 template <class T, class Comp> 25 struct Le FtistHeap 26 {27 typedef HeapNode <T> Node; 28 typedef LeftistHeap <T, Comp> Heap; 29 30 Node * root; 31 Comp cmp; 32 33 LeftistHeap (): root (0) {} 34 ~ LeftistHeap () 35 {36 clear (); 37} 38 39 void clear () 40 {41 if (root) root-> clear (); 42 root = 0; 43} 44 45 Node * merge (Node * A, Node * B) 46 {47 if (! A) return B; 48 if (! B) return A; 49 50 if (cmp (B-> val, A-> val) std: swap (A, B ); 51 A-> rch = merge (B, A-> rch); 52 53 if (! A-> lch | A-> rch-> dist> A-> lch-> dist) 54 std: swap (A-> lch, A-> rch ); 55 A-> dist = (A-> rch )? A-> rch-> dist + 1: 0; 56 57 return A; 58} 59 60 void push (const T & _ val) 61 {62 Node * nNode = new Node (_ val); 63 root = merge (root, nNode); 64} 65 66 Heap & operator <(const T & _ val) 67 {68 push (_ val); 69 return * this; 70} 71 72 T top () 73 {74 return root-> val; 75} 76 77 void pop () 78 {79 Node * temp = root; 80 root = merge (temp-> lch, temp-> rch); 81 delete temp; 82} 83 84 Heap & operator> (T & _ dest) 85 {86 _ dest = top (); 87 pop (); 88 return * this; 89} 90 91 void merge (Heap & _ other) 92 {93 this-> root = merge (this-> root, _ other. root); 94 _ other. root = 0; 95} 96 97 bool empty () 98 {99 return root = 0; 100} 101 };Leftist Heap

 

Define the "dist" of the left-side Tree node as the total length of the path from the left-side tree to the right-side. In particular, if a node has no right child, its dist value is 0.

Each node in the tree must satisfy the condition that the dist value of the left child is not smaller than that of the right child (if any.

Like most parallel operations, the core operation of the left tree is the Merge operation.

(The following pseudocode uses a small root heap as an example. The data field of a node is marked as val)

Function merge (Node * A, Node * B)

If (one of A and B is null) return another // criterion for Recursive termination

Exchange A and B (if needed) so that the val of A is smaller than the val of B

A-> rch = merge (B, A-> rch)

If (the dist of the left child of A is less than the dist of the right child or the left child of A does not exist) exchange the left and right children of

Update the dist of a according to the right child of.

Return

For implementation details, see the code.

With the merge operation, the other operations will follow the process:

Insert (push): Create a new node, and then regard it as a left-side tree, and merge it with the existing one.

Delete (pop): Delete the root node and merge the left and right children of the original root node.

 

Add a left-side tree + EXERCISE questions for the dataset:

1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 5 template <class T> 6 struct HeapNode 7 {8 typedef HeapNode <T> Node; 9 Node * lch; 10 Node * rch; 11 T val; 12 int dist; 13 14 HeapNode (const T & _ val): lch (0), rch (0 ), val (_ val), dist (0) {} 15 16 void clear () 17 {18 if (lch) lch-> clear (); 19 if (rch) rch-> clear (); 20 delete this; 21} 22}; 23 24 template <class T, class Comp> 25 struct Le FtistHeap 26 {27 typedef HeapNode <T> Node; 28 typedef LeftistHeap <T, Comp> Heap; 29 30 Node * root; 31 Comp cmp; 32 33 LeftistHeap (): root (0) {} 34 ~ LeftistHeap () 35 {36 clear (); 37} 38 39 void clear () 40 {41 if (root) root-> clear (); 42 root = 0; 43} 44 45 Node * merge (Node * A, Node * B) 46 {47 if (! A) return B; 48 if (! B) return A; 49 50 if (cmp (B-> val, A-> val) std: swap (A, B ); 51 A-> rch = merge (B, A-> rch); 52 53 if (! A-> lch | A-> rch-> dist> A-> lch-> dist) 54 std: swap (A-> lch, A-> rch ); 55 A-> dist = (A-> rch )? A-> rch-> dist + 1: 0; 56 57 return A; 58} 59 60 void push (const T & _ val) 61 {62 Node * nNode = new Node (_ val); 63 root = merge (root, nNode); 64} 65 66 Heap & operator <(const T & _ val) 67 {68 push (_ val); 69 return * this; 70} 71 72 T top () 73 {74 return root-> val; 75} 76 77 void pop () 78 {79 Node * temp = root; 80 root = merge (temp-> lch, temp-> rch); 81 delete temp; 82} 83 84 Heap & operator> (T & _ de St) 85 {86 _ dest = top (); 87 pop (); 88 return * this; 89} 90 91 void merge (Heap & _ other) 92 {93 this-> root = merge (this-> root, _ other. root); 94 _ other. root = 0; 95} 96 97 bool empty () 98 {99 return root = 0; 100} 101 }; 102 103 # include <functional> 104 105 const int maxN = 100005; 106 107 int N, M; 108 int idx [maxN]; 109 110 int father (int x) 111 {112 return idx [x] = x? X: idx [x] = father (idx [x]); 113} 114 LeftistHeap <int, std: greater <int> heap [maxN]; 116 117 void init () 118 {119 for (int I = 0; I <maxN; I ++) heap [I]. clear (); 120 for (int I = 0; I <maxN; I ++) idx [I] = I; 121} 122 bool solve () 124 {125 init (); 126 127 if (scanf ("% d", & N) = EOF) return false; 128 for (int I = 1; I <= N; I ++) 129 {130 int s; scanf ("% d", & s); 131 heap [I]. push (s); 132} 133 134 scanf ("% d \ n", & M); 135 while (M --) 136 {137 int mk1, mk2; 138 scanf ("% d", & mk1, & mk2); 139 140 int f1 = father (mk1); 141 int f2 = father (mk2 ); 142 if (f1 = f2) 143 {144 printf ("-1 \ n"); 145 continue; 146} 147 148 int s1, s2; 149 heap [f1]> s1; 150 heap [f2]> s2; 151 152 if (f1 <f2) 153 {154 idx [f2] = f1; 155 heap [f1]. merge (heap [f2]); 156 if (heap [f1]. empty () printf ("% d \ n", std: max (s1, s2)> 1); 157 else printf ("% d \ n", std:: max (heap [f1]. top (), std: max (s1, s2)> 1); 158 heap [f1] <(s1> 1) <(s2> 1); 159} 160 else161 {162 idx [f1] = f2; 163 heap [f2]. merge (heap [f1]); 164 if (heap [f2]. empty () printf ("% d \ n", std: max (s1, s2)> 1); 165 else printf ("% d \ n", std:: max (heap [f2]. top (), std: max (s1, s2)> 1); 166 heap [f2] <(s1> 1) <(s2> 1); 167} 168} 169 170 return true; 171} 172 173 int main () 174 {175 while (solve (); 176 return 0; 177}Problem: ZOJ P2334

 

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.