Handwritten a node-size-balanced tree (SBT) template, reserved for

Source: Internet
Author: User

Look at the afternoon, feel some understanding, there should be no mistake, wrong hope treatise, thank

#include <stdio.h> #include <string.h>struct s{int key,left,right,size;} Tree[10010];int top;void left_rot (int &x)//l {int y=tree[x].right;tree[x].right=tree[y].left;tree[y].left=x; Tree[y].size=tree[x].size;tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;x=y;} void Right_rot (int &x)//right-handed {int y=tree[x].left;tree[x].left=tree[y].right;tree[y].right=x;tree[y].size=tree[x] . Size;tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size;x=y;} void maintain (int &x,bool flag)//maintain SBT status {if (Flag==false)//left {if (tree[tree[tree[x].left].left].size>tree[ tree[x].right].size)//left child of left child greater than right child Right_rot (x); ElseIf (Tree[tree[tree[x].left].right].size>tree[tree[x]. right].size)//left Child's right child is greater than right child {Left_rot (tree[x].left); Right_rot (x);} Elsereturn;} else//Right {if (tree[tree[tree[x].right].right].size>tree[tree[x].left].size)//Right child's right child is greater than left child Left_rot (x); ElseIf ( tree[tree[tree[x].right].left].size>tree[tree[x].left].size)//Right child's left child is greater than left child {Right_rot (tree[x].right); Left_ Rot (x);} ElseretUrn;} Maintian (Tree[x].left,false); Maintian (tree[x].right,true); Maintian (x,true); Maintian (X,false);} void Insert (int &x,int key)//Insert {if (x==0) {x=++top;tree[x].left=0;tree[x].right=0;tree[x].size=1;tree[x].key= Key;} Else{tree[x].size++;if (Key<tree[x].key) Insert (Tree[x].left,key); Elseinsert (tree[x].right,key);// The same element can be inserted right subtree maintain (x,key>=tree[x].key);}} int remove (int &x,int key)//Use subsequent delete {tree[x].size--;if (key>tree[x].key) remove (Tree[x].right,key); ElseIf (key <tree[x].key) Remove (Tree[x].left,key); ElseIf (tree[x].left!=0&&tree[x].right==0)//Left dial hand tree, no right subtree {int temp= X;x=tree[x].left;return temp;} ElseIf (!tree[x].left&&tree[x].right!=0)//have right subtree, no left subtree {int Temp=x;x=tree[x].right;return temp;} ElseIf (!tree[x].left&&!tree[x].right)//No Left and right subtree {int Temp=x;x=0;return temp;} There are {int temp=tree[x].right;while (tree[temp].left) Temp=tree[temp].left;tree[x].key=tree[temp].key in the else//subtree; Remove (Tree[x].right,tree[temp].key);}} int getmin (int x)//minimum value {while (tree[x].left) X=tree[x].left;returnTree[x].key;} int Getmax (int x)//MAX value {while (tree[x].right) X=tree[x].right;return Tree[x].key;} int pred (int &x,int y,int key)//precursor, y initial precursor, starting from 0, the final is to return the value of the key value {if (x==0) return y;if (Key>tree[x].key) return pred ( Tree[x].right,x,key); Elsereturn pred (Tree[x].left,y,key);} int succ (int &x,int y,int key)//successor, IBID. {if (x==0) return y;if (Key<tree[x].key) return succ (Tree[x].left,x,key); Elsereturn succ (Tree[x].right,y,key);} int select (int &x,int k)//Select K small number {int r=tree[tree[x].left].size+1;if (R==K) return Tree[x].key;elseif (r<k) Return select (tree[x].right,k-r); Elsereturn Select (Tree[x].left,k);} int rank (int &x,int key)//key row {if (Key<tree[x].key) {return rank (tree[x].left,key);} ElseIf (Key>tree[x].key) return rank (Tree[x].right,key) +tree[tree[x].left].size+1;elsereturn Tree[tree[x].left] . size+1;} void order (int &x) {if (x==0) Return;order (tree[x].left);p rintf ("%d\n", Tree[x].key); order (tree[x].right);} int main () {top=0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Handwritten a node-size-balanced tree (SBT) template, reserved for

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.