算法导论 第十二章:二叉查找树(Binary Search Trees)

      二叉查找树具有如下性质:

       x是二叉查找树中的一个节点,如果y是x左子树中的一个节点,则y.key ≤ x.key ; 如果 y 是 x 右子树中的一个节点,则 x.key ≥ y.key.

        在二叉树上执行的基本操作的时间与树的高度成正比。当这棵树是完全二叉树时,这些操作的最坏情况运行时间为Θ(lgn);如果该树是含n个节点的线性链,则这些操作的最坏情况的运行时间为Θ(n)。我们可以通过随机构造二叉查找树(期望高度:E(h)=O(lgn)),从而使得在这种树上基本动态集操作的平均时间为Θ(lgn)。

基本操作:

查找关键字

最大值&最小值

查找某key的前驱后继

插入

删除


完整代码如下:

#include<iostream>
#include<cstdlib>
using namespace std;

typedef struct BSTNode{
	int key;           //key value
	//int data;          //satellite data
	BSTNode *parent; //parent node
	BSTNode *left;   //left child node
	BSTNode *right;  //right child node
	}BSTNode;

typedef struct BSTree
{
	BSTNode *root;
	}BSTree;

void BST_Insert(BSTree *T,int value)
{
	BSTNode *x=T->root;
	BSTNode *y=NULL;      //y is x's parent node

	BSTNode *z=new BSTNode();//new inserted node
	z->key=value;
	z->parent=NULL;
	z->left=NULL;
	z->right=NULL;
	
	//locate insert position
	while(x!=NULL)
	{ 
		y=x;
		if(z->key < x->key)
			x=x->left;
		else
			x=x->right;
 		 } 
	//link new node to its parent node
	z->parent = y;

	//link parent node to new node
	if(y==NULL)              //Treee is empty
		T->root=z;
	else if(z->key < y->key)
		y->left = z;
	else
		y->right = z;
	}
void BST_InorderWalk(BSTNode *x)
{
	if(x!=NULL)
	{ 
		BST_InorderWalk(x->left);
		cout<<x->key<<"   ";
		BST_InorderWalk(x->right);
	 	}
	}
BSTNode *BST_Search(BSTNode *x,int skey)
{
	if(x==NULL || skey==x->key)
		return x;
	if(skey < x->key)
		return BST_Search(x->left,skey);
	else
		return BST_Search(x->right,skey);
	}
BSTNode *BST_Minimum(BSTNode *x)
{
	while(x->left!=NULL)
		x=x->left;
	return x;
	}
BSTNode *BST_Maximum(BSTNode *x)
{
	while(x->right!=NULL)
		x=x->right;
	return x;
	}
BSTNode *BST_Successor(BSTNode *x)
{
	if(x->right!=NULL)
		return BST_Minimum(x->right);
	BSTNode *y=x->parent;
	while(y!=NULL && x==y->right)
	{ 
		x=y;
		y=y->parent;
		} 
	return y;
	}
BSTNode *BST_Predecessor(BSTNode *x)
{
	if(x->left !=NULL)
		return BST_Maximum(x->left);
	BSTNode *y=x->parent;
	while(y!=NULL && x==y->left)
	{ 
		x=y;
		y=y->parent;
		} 
	return y;
	}
void BST_Transplant(BSTree *T,BSTNode *u,BSTNode *v)
{
	if(u->parent==NULL)
		T->root=v;
	else if(u==u->parent->left)
		u->parent->left=v;
	else
		u->parent->right=v;
	if(v!=NULL)
		v->parent=u->parent;
	}
void BST_Delete(BSTree *T,BSTNode *z)
{
	if(z->left==NULL)                 //case 1 
		BST_Transplant(T,z,z->right);
	else if(z->right==NULL)           //case 2
		BST_Transplant(T,z,z->left);
 	else                             //case 3
 	{
		BSTNode *y=BST_Minimum(z->right);
		if(y->parent!=z) 
		{
			BST_Transplant(T,y,y->right);
			y->right=z->right;
			y->right->parent=y;
			}
		BST_Transplant(T,z,y);
		y->left=z->left;
		y->left->parent=y;
		}
	}
int main()
{
	int A[]={12,5,2,9,18,15,17,19};
	int n=sizeof(A)/sizeof(int);

	cout<<"/*----------------------Create BST-------------------------*/"<<endl;
	BSTree *T=new BSTree();
	T->root =NULL;
	for(int i=0;i<n;i++)      //Create Binary Search Tree
		BST_Insert(T,A[i]);

	cout<<"The Tree is(Inorder-walk-tree):"<<endl;
	BST_InorderWalk(T->root);
	cout<<endl;
	cout<<"/*---------------------------------------------------------*/"<<endl;

	cout<<"/*-----------------------Search BST------------------------*/"<<endl;
	int skey;
	BSTNode *snode=NULL;

	while(1)
	 {  
		cout<<"Please input the searching key(-1 denote exiting):";
		cin>>skey;
		if(skey==-1)
	  	{
			cout<<"Exiting..."<<endl;
			break;
			}
		snode=BST_Search(T->root,skey);
		if(snode!=NULL)
			cout<<"The node exists in the tee!"<<endl;
		else
			cout<<"The node doesn't exist."<<endl;
		}
	cout<<"/*----------------------------------------------------------*/"<<endl;
	
	cout<<"/*-------------------Minimum and Maximum--------------------*/"<<endl;
	BSTNode *minNode=NULL,*maxNode=NULL;
	minNode=BST_Minimum(T->root);
	maxNode=BST_Maximum(T->root);
	cout<<"The Minimum of the BST is:"<<minNode->key<<endl;
	cout<<"The Maximum of the BST is:"<<maxNode->key<<endl;
	cout<<"/*----------------------------------------------------------*/"<<endl;

	cout<<"/*-------------------Successor and Predecessor--------------*/"<<endl;
	cout<<"Please input the node's key:";
	cin>>skey;
	BSTNode *curNode=NULL;
	curNode=BST_Search(T->root,skey);
	if(curNode!=NULL)
	{
		BSTNode *sucNode=NULL,*preNode=NULL;
		sucNode=BST_Successor(curNode);
		preNode=BST_Predecessor(curNode);
		cout<<"The Successor of the current Node "<<curNode->key<<" is:"<< sucNode->key<<endl;
		cout<<"The Predecessor of the current Node "<<curNode->key<<" is:"<< preNode->key<<endl;
		}
	else
		cout<<"The node doen't exist."<<endl;
	cout<<"/*-----------------------------------------------------------*/"<<endl;

	cout<<"/*----------------------Insert-------------------------------*/"<<endl;
	int ikey;
	cout<<"Please input the iserting node's key:";
	cin>>ikey;
	BST_Insert(T,ikey);
	cout<<"After inserting ,the Tree is:"<<endl;
	BST_InorderWalk(T->root);
	cout<<endl;
	cout<<"/*-----------------------------------------------------------*/"<<endl;

	cout<<"/*------------------Deletion---------------------------------*/"<<endl;
	int dkey;
	cout<<"Please input the deleting node's key:";
	cin>>dkey;
	BSTNode *dNode=NULL;
	dNode=BST_Search(T->root,dkey);
	if(dNode==NULL)
		cout<<"The node doesn't exist in the treee."<<endl;
	else
	{ 
		BST_Delete(T,dNode);
		cout<<"After deleting ,the tree is:"<<endl;
		BST_InorderWalk(T->root);
		cout<<endl;
		}
	cout<<"/*-----------------------------------------------------------*/"<<endl;

	return 0;
	}
运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值