C++ 左倾红黑树

红黑树中有一种较为简单的查找插入实现,即这篇博文所提到的左倾红黑树 http://www.cnblogs.com/yangecnu/p/Introduce-Red-Black-Tree.html 所谓左倾红黑树,就是在红黑树原有的规则上再加了一条:红色节点向左倾斜,这样就只需要考虑左边,但插入删除的时候可能会有很多不必要的旋转,删除我就没写了,和红黑树一样,只不过还要让红节点全部保持在左方,删除部分参考上面那篇文章吧,有兴趣可以自己改


#include  
#include  
#include  
#include

using namespace std;  

int new_counter=0;
int del_counter=0; 

enum Color{
	RED,BLACK
};


struct Node{  
    Node* left;
	Node* right;
	Color c;
	int data;
};

void cwSpin(Node* node,Node*& parent){

	parent->left=node->right;
	node->right=parent;
	parent=node;
	
	node->left->c=BLACK;
	node->right->c=BLACK;
	node->c=RED;
}



void insert(Node*& node,Node*& parent,int data){
	
	if(node==0){
		Node* new_node=new Node;
		new_node->c=RED;
		new_node->left=0;
		new_node->right=0;
		new_node->data=data;
		node=new_node;
	}
	else if(datadata){
		insert(node->left,node,data);
	}
	else{
		insert(node->right,node,data);
	}
	

	if(node->c==BLACK){
		if(node->right!=0&&node->right->c==RED&&node->left!=0&&node->left->c==RED){
			node->left->c=BLACK;
			node->right->c=BLACK;
			node->c=RED;

		}
		else if(node->right!=0&&node->right->c==RED){
			Node* temp=node;
			node=node->right;
			temp->right=node->left;
			node->left=temp;
			
			node->c=BLACK;
			temp->c=RED;
		}
	}
	else{
		if(parent==0){
			return;
		}
		
		if(node->left!=0&&node->left->c==RED){
			cwSpin(node,parent);
		}
		else if(node->right!=0&&node->right->c==RED){
			Node* temp=node->right;
			node->right=node->right->left;
			temp->left=node;
			node=temp;
			cwSpin(node,parent);
		}
	}
}


int deep=0;

void InOrderTranversal(Node* root,int level=1){
	
	if(root==0){
		return;
	}
	InOrderTranversal(root->left,level+1);
	cout<data<<':'<deep){
		deep=level;
	}
	InOrderTranversal(root->right,level+1);
}


void release(Node* root){  
      
    if(root==0){  
        return;  
    }  
      
    release(root->left);  
    release(root->right);  
      
    delete root;  
          
}  
void del_proc(Node* ori,Node* root,int pos,int counter){
	
	
	
}


void Balance(Node*& node){
	
	
}

void Next(Node*& root,Node*& node){
	
	if(node->left==0){
		root->data=node->data;
		
		if(node->right!=0){
			Node* temp=node;
			node=node->right;
			node->c=BLACK;
			delete temp;
		}
		else{
			delete node;
			node=0;
		}
		
		return;
	}
	Next(root,node->left);
	
	Balance(node);
	
}


void del(Node*& root,Node*& parent,int data){
	if(root->data==data){
		if(root->left==0&&root->right==0){
			delete root;
			root=0;
		}
		else if(root->right==0){
			Node* temp=root;
			root=root->left;
			delete temp;
		}
		else{
			Next(root,root->right);
		}
	}
	else if(datadata){
		del(root->left,root,data);
	}
	else{
		del(root->right,root,data);
	}
	
	Balance(root);
}



int main(int argc,char* argv[]){
	srand(time(NULL));
	
	Node* root=new Node;
	root->c=BLACK;
	root->left=0;
	root->right=0;
	root->data=0;
	Node* parent=0;
	
	int num=100;
	if(argc>1){
		num=atoi(argv[1]);
	}
	clock_t t1=clock();
	for(int i=0;ic=BLACK;
    }
	clock_t t2=clock();
	cout<<(double)(t2-t1)<>num;
		del(root,parent,num);
		InOrderTranversal(root);
	}

	
	release(root);
}


你可能感兴趣的:(C/C++,C++,算法)