Various balance trees treap/sbt/avl/splay tree

Source: Internet
Author: User

Looking at the balance tree for so long, it is time to make a summary.

Take POJ 3481 For example, knocked four code, respectively treap, Size Balance Tree,avl tree,splay Tree.

The only less red and black trees t_t ...

In general, each of the balance tree has its own advantages:

Treap write up easy to get started also quickly if skilled words not to very kind can knock finish.

SBT fast ...

The AVL tree is highly balanced, but the actual effect is not satisfactory, it may be I realized the posture is not right/(ㄒoㄒ)/~~

Splay tree is more balanced in all aspects, especially in the maintenance of the sequence of different trees with the advantages of several other tree.

The treap is the exception of the persistent (because no comparison is not good to jump to conclusions).

In short, as long as the understanding of the rotation operation and achievements of the idea, each tree is very convenient to implement ...

Okay, that's a little ridiculous. The code is as follows:

Treap:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_N 110000#define Size (_) (_) = = Null? 0: (_)->size) typedef struct _TRP{INT Client, key, size, fix;struct _TRP *ch[2];} Treap, *treap;treap stack[max_n];int sz = 0;int run () {static int x = 1840828537;x + = (x << 2) | 1;return x;} void update (Treap x) {if (x = = NULL) return;x->size = size (x->ch[0]) + size (x->ch[1]) + 1;} void rotate (treap *x, int d) {treap k = (*x)->ch[!d];(*x)->ch[!d] = k->ch[d];k->ch[d] = *x;k->size = (*x)-&G T;size;update (*x); *x = k;} void Insert (treap *x, int client, int key) {if (*x = = NULL) {*x = &stack[sz++];(*x)->ch[0] = (*x)->ch[1] = NULL; x)->key = key, (*x)->size = 1, (*x)->client = client, (*x)->fix = run ();} else {int d = key > (*x)->key;insert (& ((*x)->ch[d]), client, key); update (*x); if (*x)->ch[d]->fix < (*x)->fix) rotate (x,!d);}} void _delete (treap *x, int key) {if (*x = = NULL) return;if ((*x)->key = =Key) {if (!) ( *x)->ch[0] | | ! (*x)->ch[1]) {*x = (*x)->ch[0]? (*x)->ch[0]: (*x)->ch[1];} else {int d = (*x)->ch[0]->fix < (*x)->ch[1]->fix;rotate (x, D); _delete (& ((*x)->ch[d]), key);}} else {_delete (& (*x)->ch[key> (*x)->key]), key);} if (*x! = NULL) update (*X);} Treap find_kth (treap x, int k) {int t = 0;for (; x! = NULL;) {t = size (x->ch[0]); if (k = = t + 1) break;else if (k <= t ) x = x->ch[0];else K-= t + 1, x = x->ch[1];} return x;} int main () {#ifdef localfreopen ("In.txt", "R", stdin), Freopen ("OUT.txt", "w+", stdout), #endifint N, a, B; Treap root = NULL, ret = Null;while (~scanf ("%d", &n) && n) {ret = null;if (2 = = N | | 3 = = n) {if (2 = = N && Amp Root) ret = find_kth (root, root->size), else if (3 = = n && root) ret = find_kth (root, 1); if (!ret | |!root) prin TF ("0\n"), Else printf ("%d\n", Ret->client), _delete (&root, Ret->key);} else {scanf ("%d%d", &a, &b); Insert (&root, A, b);}} return 0;}
SBT:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_N 110000#define Size (_) (_) = = Null? 0: (_)->size) typedef struct _SBT{INT client, key, Size;struct _SBT *ch[2];} Sbtnode, *SBT; Sbtnode stack[max_n];int sz = 0;void rotate (SBT *x, int d) {SBT k = (*x)->ch[!d];(*x)->ch[!d] = k->ch[d];k->ch[ D] = *x;k->size = (*x)->size; (*x)->size = Size ((*x)->ch[0]) + size ((*x)->ch[1]) + 1;*x = k;} void Maintain (SBT *x, int d) {if ((*x)->ch[d] = = NULL) return;if (Size ((*x)->ch[d]->ch[d]) > Size ((*x)->ch[ !D]) rotate (x,!d), else if (size (*x)->ch[d]->ch[!d]) > Size ((*x)->ch[!d]) Rotate (& ((*x)->ch[d]) , d), rotate (x,!d), and else return; Maintain (& (*X)->ch[d]), 0); Maintain (& (*X)->ch[!d]), 1); Maintain (x, 0), maintain (x, 1);} void Insert (SBT *x, int client, int key) {if (*x = = null) {*x = &stack[sz++];(*x)->ch[0] = (*x)->ch[1] = NULL; (*x) ->key = key, (*x)->size = 1, (*x)->client = client;} ElSe {(*x)->size++;insert (& ((*x)->ch[key > (*x)->key]), client, key); Maintain (x, key >= (*x)->key);}} void _delete (SBT *x, int key) {if (*x = = NULL) return, (*x)->size--;if ((*x)->key = key) {if (!) ( *x)->ch[0] | | ! (*x)->ch[1]) {*x = (*x)->ch[0]? (*x)->ch[0]: (*x)->ch[1];} else {SBT ret = (*x)->ch[1];for (; ret->ch[0]! = NULL; ret = ret->ch[0]); _delete (& ((*x)->ch[1]), (*x), key = Ret->key);}} else {_delete (& (*x)->ch[key > (*x)->key], key);}} SBT find_kth (SBT x, int k) {int t = 0;for (; x! = NULL;) {t = size (x->ch[0]), if (k = = t + 1) break;else if (k <= t) x = X->ch[0];else K-= t + 1, x = x->ch[1];} return x;} int main () {#ifdef localfreopen ("In.txt", "R", stdin), Freopen ("OUT.txt", "w+", stdout), #endifint N, a, B; SBT root = NULL, ret = Null;while (~scanf ("%d", &n) && n) {ret = null;if (2 = = N | | 3 = = n) {if (2 = = N &&am P Root) ret = find_kth (root, root->size); else if (3 = = n && RooT) ret = find_kth (root, 1), if (!ret | |!root) printf ("0\n"), Else printf ("%d\n", Ret->client), _delete (&root, RET-&G T;key);} else {scanf ("%d%d", &a, &b); Insert (&root, A, b);}} return 0;}
AVL:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_N 200000#define _max (b) (a) > (b)? (a):(B)) #define SIZE (_) ((_) ==null? 0: (_)->size) #define Height (_) (_) ==null?-1: (_)->height) typedef struct _A Vl{int client, key, size, height;struct _avl *ch[2];} Avltree, *avl; Avltree stack[max_n];int sz = 0;void Update (AVL x) {if (x = = NULL) return;x->size = size (x->ch[0]) + size (x->ch[1] ) + 1;x->height = _max (height (x->ch[0]), height (x->ch[1])) + 1;}  void rotate (AVL *x, int d) {AVL k = (*x)->ch[!d];(*x)->ch[!d] = k->ch[d];k->ch[d] = *x;update (*x); update (k); *x = k;} void Maintain (AVL *x, int d) {if (height ((*x)->ch[d])-Height ((*x)->ch[!d]) = = 2) {if (height ((*x)->ch[d]->ch [d])-height ((*x)->ch[d]->ch[!d]) = = 1) rotate (x,!d); else if (height ((*x)->ch[d]->ch[d])-Height ((*x)- >ch[d]->ch[!d]) = =-1) {Rotate (& ((*x)->ch[d]), D), rotate (x,!d);}} void Insert (AVL *x, int client, int key) {if (*x = =NULL) {*x = &stack[sz++];(*x)->ch[0] = (*x)->ch[1] = NULL; (*x)->key = key, (*x)->height = 0, (*x)->size = 1, (*x)->client = client;} else {int d = key > (*x)->key;insert (& ((*x)->ch[d]), client, key); update (*X); Maintain (x, d);}} void _delete (AVL *x, int key) {if (*x = = NULL) return;if ((*x)->key = = key) {if (!) ( *x)->ch[0] | | ! (*x)->ch[1]) {*x = (*x)->ch[0]? (*x)->ch[0]: (*x)->ch[1];} else {AVL ret = (*x)->ch[1];for (; ret->ch[0]! = NULL; ret = ret->ch[0]); _delete (& ((*x)->ch[1]), (*x), key = Ret->key);}} else {_delete (& (*x)->ch[key > (*x)->key]), key);} if (*x! = NULL) {update (*X); Maintain (x, 0), maintain (x, 1);}} AVL find_kth (AVL x, int k) {int t = 0;for (; x! = NULL;) {t = size (x->ch[0]); if (k = = t + 1) break;else if (k <= t) x = X->ch[0];else K-= t + 1, x = x->ch[1];} return x;} int main () {#ifdef localfreopen ("In.txt", "R", stdin), Freopen ("OUT.txt", "w+", stdout), #endifint N, a, B; AVL root = NULL, ret = Null;while (~scanf ("%d", &n) && n) {ret = null;if (2 = = N | | 3 = = n) {if (2 = = n && root) ret = Find_kth (Root, root->size), else if (3 = = n && root) ret = find_kth (root, 1), if (!ret | |!root) printf ("0\n"); E LSE printf ("%d\n", Ret->client), _delete (&root, Ret->key);} else {scanf ("%d%d", &a, &b); Insert (&root, A, b);}} return 0;}

Splay Tree:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_N 200000#define Size (_) (_) = = Null? 0: (_)->size) typedef struct _SPT{INT client, key, Size;struct _spt *pre, *ch[2];} Splay;splay *null, *root, stack[max_n];int sz = 0;splay *_calloc (int client, int key) {splay *p = &stack[sz++];p->pr E = p->ch[0] = p->ch[1] = Null;p->size = 1, P->key = key, p->client = Client;return p;} void Push_up (splay *x) {if (x = = null) return;x->size = size (x->ch[0]) + size (x->ch[1]) + 1;} void rotate (splay *x, int d) {splay *y = x->pre;y->ch[!d] = x->ch[d];if (x->ch[d]! = null) x->ch[d]->pre = Y;x->pre = Y->pre;if (y->pre! = NULL) y->pre->ch[y->pre->ch[0]! = y] = x;x->ch[d] = Y;y->pre = X;push_up (y); if (y = = root) root = x;} void Splay_splay (splay *x, splay *f) {for (; X->pre! = f;) {if (X->pre->pre = = f) {rotate (x, x->pre->ch[0] = = x);} else {splay *y = x->pre, *z = Y->pre;if (Z->ch[0]= = y) {if (y->ch[0] = = x) rotate (y, 1), rotate (x, 1), Else rotate (x, 0), rotate (x, 1),} else {if (y->ch[1] = = x) rotate (y, 0), rotate (x, 0), else rotate (x, 1), rotate (x, 0);}} PUSH_UP (x);} void Insert (int client, int key) {splay *FP = null, *p = root;if (root = null) {root = _calloc (client, key); return;} for (; P! = null;) {FP = p;if (Key > P->key) p = p->ch[1];else p = p->ch[0];} p = _calloc (client, key), if (Fp->key > Key) fp->ch[0] = P;else fp->ch[1] = P;p->pre = Fp;splay_splay (P, nul L);p ush_up (p);} void _delete (int key) {splay *p = root, *rt = Null;while (P! = null && P->key! = key) p = p->ch[key > P-&G T;key];if (P = = null) return;splay_splay (p, NULL), RT = Root->ch[0];if (RT = = NULL) {RT = root->ch[1];} else {splay *tmp = rt->ch[1];while (tmp! = NULL && tmp->ch[1]! = NULL) TMP = TMP-&GT;CH[1];IF (tmp! = NULL) Splay_splay (TMP, root); RT = Root->ch[0];rt->ch[1] = Root->ch[1];root->ch[1]->pre = RT;} root = RT;ROOT-&GT;pre = null;if (root! = null) push_up (root);} void Initialize () {null = _calloc ( -1,-1); null->size = 0;root = null;}  Splay *find_kth (splay *x, int k) {int t = 0;for (; x! = null;) {t = size (x->ch[0]); if (k = = t + 1) break;else if (k <= T) x = x->ch[0];else K-= t + 1, x = x->ch[1];} if (x = = null) {return null;} else {splay_splay (x, null); return root;}} int main () {#ifdef localfreopen ("In.txt", "R", stdin), Freopen ("OUT.txt", "w+", stdout), #endifint N, A, b;initialize (); Splay *ret = Null;while (~scanf ("%d", &n) && n) {ret = null;if (2 = = N | | 3 = = n) {if (2 = = n && root) r ET = find_kth (Root, root->size), else if (3 = = n && root) ret = find_kth (root, 1); if (ret = = NULL | | root = = NUL L) printf ("0\n"), Else printf ("%d\n", Ret->client), _delete (Ret->key);} else {scanf ("%d%d", &a, &b); Insert (A, b);}} return 0;}

The final comparison results: From top to bottom, splay tree, Treap,sbt,avl.

In fact Konjac Konjac will only use C language will not C + +, but the time to submit the code selected C + + and GCC two O (╯-╰) o ...



Various balance trees treap/sbt/avl/splay tree

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.