算法導論 第十二章:二叉查找樹(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;
	}
運行結果:



發佈了42 篇原創文章 · 獲贊 13 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章