二叉排序树


#include <iostream>
using namespace std;

struct BiTreeNode{
	int data;
	BiTreeNode* left;
	BiTreeNode* right;
	BiTreeNode* parent;
};

void InOrderTraverse(BiTreeNode* Root){
	if(Root==NULL) return;
	InOrderTraverse(Root->left);
	cout<<Root->data<<' ';
	InOrderTraverse(Root->right);
}

BiTreeNode* Search(BiTreeNode* Root,int key){
	while(Root){
		if(Root->data==key)
			return Root;
		else if(Root->data>key)
			Root=Root->left;
		else
			Root=Root->right;
	}
	return NULL;
} 
BiTreeNode* SearchRecur(BiTreeNode* Root,int key){
	if(!Root) return NULL;
	if(Root->data==key)
		return Root;
	if(Root->data>key)
		return SearchRecur(Root->left,key);
	else
		return SearchRecur(Root->right,key);
}

BiTreeNode* Minimum(BiTreeNode* Root){
	while(Root->left)
		Root=Root->left;
	return Root;
}
BiTreeNode* Maximum(BiTreeNode* Root){
	while(Root->right)
		Root=Root->right;
	return Root;
}

BiTreeNode* Successor(BiTreeNode* pNode){
	if(pNode->right)
		return Minimum(pNode->right);
	BiTreeNode* pParentNode=pNode->parent;
	while(pParentNode && pNode==pParentNode->right){
		pNode=pParentNode;
		pParentNode=pNode->parent;
	}
	return pParentNode;
}
BiTreeNode* Predecessor(BiTreeNode* pNode){
	if(pNode->left)
		return Maximum(pNode->left);
	BiTreeNode* pParentNode=pNode->parent;
	while(pParentNode && pNode==pParentNode->left){
		pNode=pParentNode;
		pParentNode=pNode->parent;
	}
	return pParentNode;
}

void Insert(BiTreeNode** Root,int key){
	BiTreeNode* pNew=new BiTreeNode;
	pNew->data=key;
	pNew->parent=pNew->left=pNew->right=NULL;
	
	BiTreeNode* pParentNode=NULL;
	BiTreeNode* pNode=*Root;//important
	
	while(pNode){
		pParentNode=pNode;
		if(pNode->data>key)
			pNode=pNode->left;
		else
			pNode=pNode->right;
	}
	
	pNew->parent=pParentNode;
	
	if(pParentNode==NULL)
		*Root=pNew;
	else{
		if(pParentNode->data>key)
			pParentNode->left=pNew;
		else
			pParentNode->right=pNew;
	}
}
void Delete(BiTreeNode** Root,int key){
	//get the node whose data to be delete
	BiTreeNode* pNode=Search(*Root,key);
	//node to delete phisically
	BiTreeNode* pDel=NULL;
	if(!pNode->left || !pNode->right)
		pDel=pNode;
	else
		//delete the successor(predecessor ok too)
		pDel=Successor(pNode);
	
	BiTreeNode* pChild=NULL;
	if(pDel->left)
		pChild=pDel->left;
	else
		pChild=pDel->right;
	
	if(pChild)
		pChild->parent=pDel->parent;
	
	if(pDel->parent==NULL)
		*Root=pChild;
	else{
		if(pDel==pDel->parent->left)
			pDel->parent->left=pChild;
		else
			pDel->parent->right=pChild;	
	}
	
	if(pNode!=pDel)
		pNode->data=pDel->data;
	delete pDel;
	pDel=NULL;
}

int main(){
	//test
	BiTreeNode* Root=NULL;
	//insert and traverse
	Insert(&Root,4);
	Insert(&Root,2);
	Insert(&Root,6);
	Insert(&Root,3);
	Insert(&Root,7);
	Insert(&Root,5);
	Insert(&Root,1);
	InOrderTraverse(Root);
	cout<<endl;
	//max and min
	cout<<Maximum(Root)->data<<endl;
	cout<<Minimum(Root)->data<<endl;
	//predecessor and successor
	cout<<Successor(Minimum(Root))->data<<endl;
	cout<<Predecessor(Maximum(Root))->data<<endl;
	cout<<Successor(Search(Root,3))->data<<endl;
	cout<<Predecessor(Search(Root,5))->data<<endl;
	//delete and traverse
	Delete(&Root,6);
	InOrderTraverse(Root);
	cout<<Successor(Search(Root,5))->data<<endl;
	Delete(&Root,7);
	Delete(&Root,2);
	Delete(&Root,3);
	Delete(&Root,4);
	Delete(&Root,5);
	InOrderTraverse(Root);
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值