Stretching operation tree

Algorithm Description:
To complete the insert and extending lookup tree, first write the right-handed and left-most basic operations, since a number of the most recent operation of the rotation to put the root node.
L-zag: the right child of the current node to node, left the father, the father node to the right child of the current node.
That is: p-> lChild = q-> rChild ; p-> rChild = q;
dextrose zig: the left child and right child of the current node to the parent node, the parent node to the left child of the current node.
That is: p-> rChild = q-> lChild ; p-> lChild = q;

Insert operation: with different situations:
(1) node to be inserted is empty: directly assigned to the current node.
(2) to be inserted equals the value of the current node returns already exists.
(3) x is less than the current node becomes the left child of the current node.
1) the current node is empty: direct assignment to the current node.
2) the current node is equal to the value insertion: for right-handed, it becomes the root node, return already exists.
3) is less than the value of the current node is inserted: insertion operation (recursively) using this as a left child node of the root node, then the two right-handed (guaranteed operation at the root).
4) insert a value greater than the current node: as a root node insert (recursively) with the right child of the current node, then the first left, then left child becomes the root node, for dextrose.
(4) x is greater than the current node, the current node becomes the right child. Similar operation with the above-described operation, can be divided into four cases, but rotate in opposite directions. Not repeated here.

Find operation:
the search operation is similar to insert, when x == p-> element, do an insert, this value can find flipped to the top, with the insertion of the same principle. When greater than the right child to do recursive, less than, left the children to do recursive.

Source:

#include <iostream>
using namespace std;

enum ResultCode{Underflow,Overflow,Success,Duplicate,Fail,NotPresent};

struct BTNode{
	int element;
	BTNode *lChild,*rChild;
};

void LRot(BTNode *&p){
	BTNode *r=p->rChild;
	p->rChild=r->lChild;
	r->lChild=p;
	p=r;
}

void RRot(BTNode *&p){
	BTNode *r=p->lChild;
	p->lChild=r->rChild;
	r->rChild=p;
	p=r;
}

ResultCode Insert(BTNode *&p,int x){ 
	ResultCode result=Success;
	BTNode *r;
	if(p==NULL){
		p=new BTNode;
		p->element=x;p->lChild=NULL;p->rChild=NULL;
		return result;
	}
	if(x==p->element){
		result=Duplicate;
		return result;
	}
	if(x<p->element){
		r=p->lChild;
		if(r==NULL){
			r=new BTNode;
			r->element=x;r->lChild=NULL;r->rChild=NULL;
			r->rChild=p;p=r;
			return result;
		}
		else if(x==r->element){
			RRot(p);
			result=Duplicate;
			return result;
		}
		if(x<r->element){
			result=Insert(r->lChild,x);
			RRot(p);RRot(p);
		}
		else{
			result=Insert(r->rChild,x);
			LRot(r);
			p->lChild=r;
			RRot(p);
		}
	}
	else{
		r=p->rChild;
		if(r==NULL){
			r=new BTNode;
			r->element=x;r->lChild=NULL;r->rChild=NULL;
			r->lChild=p;p=r;
			return result;
		}
		else if(x==r->element){
			LRot(p);
			result=Duplicate;
			return result;
		}
		if(x>r->element){
			result=Insert(r->rChild,x);
			LRot(p);LRot(p);
		}
		else{
			result=Insert(r->lChild,x);
			RRot(r);
			p->rChild=r;
			LRot(p);
		}
	}
	return result;
}

void Find(BTNode *p,BTNode *&boot,int x){
	if(p==NULL)
		return ;
	if(x==p->element){
		Insert(boot,x);
		return ;
	}		
	if(x<p->element)
		Find(p->lChild,boot,x);
	else
		Find(p->rChild,boot,x);
}


void ShowSPTree(BTNode *p,int pos){
	int i;
	for(i=0;i<pos;i++)
		cout<<" ";
	if(p!=NULL){
		cout<<p->element<<endl;
		ShowSPTree(p->lChild,pos+5);
		ShowSPTree(p->rChild,pos+5);
	}
	else
		cout<<"--"<<endl;
}

int main(){
	int n,i,x;
	BTNode *boot=NULL;
	ResultCode result=Success;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>x;
		Insert(boot,x);
	}
	cin>>x;
	Find(boot,boot,x);
	ShowSPTree(boot,0);
	return 0;
}

Guess you like

Origin blog.csdn.net/jihome/article/details/94965026