SBT Balanced binary tree, POJ 3481__insert

Source: Internet
Author: User

Reprint please indicate the source, thank http://blog.csdn.net/ACM_cxlove?viewmode=contents by---Cxlove


Balanced binary tree, adjustment includes left and right rotation, which have direct rotation and combination of rotation, not good drawing, specific SBT can be seen http://blog.csdn.net/acceptedxukai/article/details/6921334

Knock one, stay as a template, a little change can be POJ 3481

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #define N 1000005
using namespace Std;
	struct sbt{//Zoozi tree pointer, right subtree pointer, size, key value int left,right,size,key;
		void Init () {left=right=key=0;
	size=1;
}}t[n]; int Root,tot;
	The location of the root and the number of nodes//left rotation handles void Left_rot (int &x) {int k=t[x].right;
	T[x].right=t[k].left;
	T[k].left=x;
	T[k].size=t[x].size;
	t[x].size=t[t[x].left].size+t[t[x].right].size+1;
X=k;
	//Right rotation handles void Right_rot (int &x) {int k=t[x].left;
	T[x].left=t[k].right;
	T[k].right=x;
	T[k].size=t[x].size;
	t[x].size=t[t[x].left].size+t[t[x].right].size+1;
X=k;
			//Adjust processing void maintain (int &r,bool flag) {if (flag) {//Update right subtree if (t[t[t[r].right].right].size>t[t[r].left].size)
		Left_rot (R);
			else if (t[t[t[r].right].left].size>t[t[r].left].size) {Right_rot (t[r].right);
		Left_rot (R);
	else return;
		else{//Update in left subtree if (t[t[t[r].left].left].size>t[t[r].right].size) Right_rot (R); else if (t[t[t[r].left). right].size>t[t[r].right].size) {Left_rot (t[r].left);
		Right_rot (R);
	else return;
	//update subtree, then update root until balance maintain (t[r].left,false);
	Maintain (t[r].right,true);
	Maintain (R,FALSE);
Maintain (r,true);
		}//Insert new node void Insert (int &r,int k) {if (r==0) {r=++tot; T[R].
		Init ();
	T[r].key=k;
		} else{t[r].size++;
		if (K<t[r].key) Insert (t[r].left,k);
		else Insert (t[r].right,k);
	After inserting to adjust, guarantee balance maintain (r,k>=t[r].key);
	}///delete node, using the predecessor to replace int Remove (int &r,int k) {int d_key;
	if (!r) return 0;
	t[r].size--; The former description is the node to be deleted, and the latter indicates that there is no such node if (t[r].key==k| | (T[r].left==0&&k<t[r].key) | | (T[r].right==0&&k>t[r].key))
		{D_key=t[r].key;
		if (t[r].left&&t[r].right) t[r].key=remove (t[r].left,k+1);
	else R=t[r].left+t[r].right; else Remove (K<t[r].key?)
T[R].LEFT:T[R].RIGHT,K);
	//Get the maximum value, that is, to traverse to the right node int get_max (int r) {while (t[r].right) r=t[r].right;
return R; //Get the minimum value, that is, to traverse the leftmost node int get_min (int r) {while t[r]. left) R=t[r].left;
return R;
	//Get the precursor int get_pre (int &r,int y,int k) {if (r==0) return y;
	if (K>t[r].key) Get_pre (t[r].right,r,k);
else Get_pre (t[r].left,y,k);
	}//obtain subsequent int get_next (int &r,int y,int k) {if (r==0) return y;
	if (K<t[r].key) Get_next (t[r].left,r,k);
else Get_next (t[r].right,y,k);
	//Get a small number of K, note: Can not solve the number of duplicate int get_kth (int &r,int k) {int t=t[t[r].left].size+1;
	if (t==k) return t[r].key;
	if (t<k) return get_kth (T[R].RIGHT,K-R);
else return get_kth (t[r].left,k);
	///Get the node's rank int get_rank (int &r,int k) {if (K<t[r].key) return Get_rank (T[R].LEFT,K);
	else if (K>t[r].key) return Get_rank (t[r].right,k) +t[t[r].left].size+1;
else return t[t[r].left].size+1;
	}//Sort void inorder (int &r) {if (r==0) return;
	Inorder (T[r].left);
	printf ("%d\n", T[r].key);
Inorder (T[r].right); }


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.