二叉排序树的建立、插入、删除、查找、4种遍历 C++完整实现

#include<iostream>
#include<string>
#include<queue>
using namespace std;

typedef int KeyType;
#define NUM 13
class BinSTree;
class BinStreeNode
{
public:
	KeyType key;
	BinStreeNode *lchild;
	BinStreeNode *rchild;
	BinStreeNode()
	{
		lchild=NULL;
		rchild=NULL;
	}
};

class BinSTree
{
public:
	BinStreeNode *root;
	BinSTree()
	{
		root=NULL;
	}
	~BinSTree()
	{
		//delete root;
	}
	BinStreeNode *BSTreeSearch(/*BinStreeNode *bt,*/KeyType k,BinStreeNode *&p);//查找失败返回NULL,p记录待插入的父结点
	void BSTreeInsert(KeyType k);//bt用引用,插入结点为根结点,需要修改root
	int BSTreeDelete(KeyType k);
	void BSTreePreOrder(BinStreeNode *bt);
	void BSTreeInOrder(BinStreeNode *bt);
	void BSTreeAfOrder(BinStreeNode *bt);
	void BSTreeLevelTraverse(BinStreeNode *bt);//层次遍历
	bool IsEmpty()
	{
		return root==NULL;
	}
};
/*
* 二叉排序树查找算法
* 在根指针为bt的二叉排序树中查找元素k的节点,若查找成功,则返回指向该节点的指针
* 查找成功时,参数p指向查找到的结点;查找失败时,参数p指向k应插入的父节点
*/
BinStreeNode *BinSTree::BSTreeSearch(/*BinStreeNode *bt,*/KeyType k,BinStreeNode *&p)
{
	BinStreeNode *q=NULL;
	q=root;
	while(q)
	{
		p=q;
		if(q->key==k)
			return p;
		else if(q->key>k)
			q=q->lchild;
		else
			q=q->rchild;
	}
	return NULL;
}
//插入操作
void BinSTree::BSTreeInsert(KeyType k)
{
	BinStreeNode *p=NULL,*q;
	q=root;
	if(BSTreeSearch(k,p)==NULL)//查找失败时才插入
	{
		BinStreeNode *r=new BinStreeNode;
		r->key=k;
		if(q==NULL)//根结点为空
		{
			root=r;
			return ;
		}
		if(p&&k<p->key)
			p->lchild=r;
		else if(p&&k>p->key)
			p->rchild=r;
	}
}
//先序遍历
void BinSTree::BSTreePreOrder(BinStreeNode *bt)
{
	if(bt)
	{
		cout<<bt->key<<" ";
		BSTreePreOrder(bt->lchild);
		BSTreePreOrder(bt->rchild);
	}
}
//中序遍历
void BinSTree::BSTreeInOrder(BinStreeNode *bt) //时间复杂度O(N);
{
	if(bt)
	{
		
		BSTreeInOrder(bt->lchild);
		cout<<bt->key<<" ";
		BSTreeInOrder(bt->rchild);
	}
}
//后序遍历
void BinSTree::BSTreeAfOrder(BinStreeNode *bt)
{
	if(bt)
	{
		
		BSTreeAfOrder(bt->lchild);
		BSTreeAfOrder(bt->rchild);
		cout<<bt->key<<" ";
	}
}
//层次遍历
void BinSTree::BSTreeLevelTraverse(BinStreeNode *bt)
{
	queue<BinStreeNode*> q;
	if(bt)
		q.push(bt);
	while(!q.empty())
	{
		cout<<q.front()->key<<" ";//访问队头结点
		if(q.front()->lchild)
			q.push(q.front()->lchild);
		if(q.front()->rchild)
			q.push(q.front()->rchild);
		q.pop();
	}
	cout<<endl;
}
//二叉排序树删除操作,删除成功返回1,失败返回0.
int BinSTree::BSTreeDelete(KeyType k)
{
	BinStreeNode *f,*p,*q,*s;
	p=root;
	f=NULL;
	//查找关键字为k的结点,同时将此结点的双亲找出来
	while(p&&p->key!=k)
	{
		f=p; //f记录父结点
		if(p->key>k)
			p=p->lchild;
		else
			p=p->rchild;
	}
	if(!p)//找不到待删除结点时返回
		return 0;
	if(p->lchild==NULL) //待删除结点的左子树为空
	{
		if(f==NULL)//待删除结点为根结点
			root=p->rchild;
		else if(f->lchild==p)//待删除结点是其双亲结点的左节点
			f->lchild=p->rchild;
		else                //待删除结点是其双亲结点的右结点
			f->rchild=p->rchild;
		delete p;
	}
	else //待删除结点有左子树
	{
		q=p;
		s=p->lchild;
		while(s->rchild) //在待删除结点的左子树中查找最右下结点,即查找待删除结点的中序前驱结点
		{
			q=s;
			s=s->rchild;
		}
		if( q == p )  
            q->lchild = s->lchild;  
        else  
            q->rchild = s->lchild;  
        p->key = s->key;  //值替代法删除节点
        delete s; 
	}
	return 1;
}


int main()
{
	int a[NUM]={34, 18, 76, 13,12,11, 52, 82, 16, 67, 58, 73, 72 };
	BinSTree bst;
	BinStreeNode *pBt = NULL, *p = NULL, *pT = NULL;
	for(int i=0;i<NUM;i++)
	{
		bst.BSTreeInsert(a[i]);
	}
	pT = bst.BSTreeSearch(51, p ); //搜索排序二叉树 
	bst.BSTreeLevelTraverse(bst.root);
	if(pT)
	{
		cout<<pT->key<<endl;
	}
    bst.BSTreePreOrder(bst.root);  
    cout << endl;  
    bst.BSTreeDelete(13);   //删除无左孩子的情况  
    bst.BSTreePreOrder(bst.root);  
    cout << endl;  
    bst.BSTreeDelete( 76 );   //删除有左孩子的情况  
    bst.BSTreePreOrder(bst.root);  
    cout << endl;
	system("pause");
	return 0;
}

二叉排序树的建立、插入、删除、查找、4种遍历方式的C++完整实现版



    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/u010367506/article/details/22735533
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞