二叉排序树 c++

//**********二叉排序树**********

#include<iostream>
using namespace std;

//定义树的节点
struct BiTNode
{
	int date;
	BiTNode* lchild;
	BiTNode* rchild;
};
typedef BiTNode* BiTree;

//树的类
class BinarySortTree
{
public:
	BinarySortTree(int a[], int n);												//构造创建一棵树
	~BinarySortTree() {}
	bool SearchBST( const BiTree&T, int key, BiTree f, BiTree& p);				//搜索key节点
	bool InsertBST(BiTree& T, int key);											//插入key节点
	bool DeleteBST(BiTree& T, int key);											//删除key节点
	void InOrderBST(BiTree T);													//中序遍历
	bool Delete(BiTree& p);														//删除算法
	BiTree T;																	//根节点T
};

BinarySortTree ::BinarySortTree(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		InsertBST(T, a[i]);
	}
	std::cout << "二叉排序树创建成功!!!" << std::endl;
}


//查找
//1.f指向T的双亲节点,初始调用值为NULL
//2.若查找成功,p指向被查找的节点,
//3.若查到失败,p指向查找路径上最后一个访问的节点(此时p==f)
//4.不需要对根节点进行修改,所以常量引用传递较好

bool BinarySortTree:: SearchBST( const BiTree &T, int key, BiTree f, BiTree& p)
{
	if (!T)
	{
		p = f;
		return false;
	}
	else if (T->date == key)
	{
		p = T;
		return true;
	}
	else if (key<T->date)
	{
		return SearchBST(T->lchild, key, T, p);
	}
	else
		return SearchBST(T->rchild, key, T, p);
}

//插入
//1.一定是在叶子节点下插入
//2.用引用的方式传T进去,是因为这棵树有可能为空的情况,所以可能会对根节点T修改
bool BinarySortTree::InsertBST(BiTree& T, int key)
{
	BiTree p, s;
	if (!SearchBST(T, key, NULL, p))
	{
		s = new BiTNode;
		s->date = key;
		s->lchild = NULL;
		s->rchild = NULL;
		if (!p)//树为是空的情况,因为树不空p不会空
			T = s;
		else if (key<p->date)//这里p是访问路径中最后一个节点
			p->lchild = s;
		else
			p->rchild = s;
		return true;
	}
	else
		return false;
}



//删除方法
//思路:
//	1.如果该节点只有左子树,让其双亲重连上其左子树,直接删除
//	2.如果该节点只有右子树,让其双亲重连上其右子树,直接删除
//	3.如果该节点既有左子树还有右子树
//	(1)先找其前驱节点(左子树中最靠右的节点)、前驱节点的双亲节点,并换值
//	(2)连接左右子树,需要判断前驱节点的双亲是否就是所删除的节点。
bool BinarySortTree:: Delete(BiTree& p)
{
	BiTree q = NULL;
	BiTree s = NULL;
	if (p->rchild == NULL)
	{
		q = p;
		p = p->lchild;
		delete q;
	}
	else if (p->lchild == NULL)
	{
		q = p;
		p = p->rchild;
		delete q;
	}
	else
	{
		q = p;
		s = p->lchild;
		while (s->rchild)
		{
			q = s;
			s = s->rchild;
		}
		p->date = s->date;
		if (q!=p)
			q->rchild = s->lchild;
		else
			q->lchild = s->lchild;
		delete s;
	}
	return true;
}

//删除节点
bool BinarySortTree::DeleteBST(BiTree& T, int key)
{
	if (!T)
		return false;
	else if (key == T->date)
		return	Delete(T);
	else if (key < T->date)
		return DeleteBST(T->lchild, key);
	else
		return DeleteBST(T->rchild, key);
}

//中序遍历
void BinarySortTree::InOrderBST(BiTree T)
{
	if (!T)
		return;
	InOrderBST(T->lchild);
	std::cout << T->date << " ";
	InOrderBST(T->rchild);
}

int main()
{
	int a[10] = { 62,34,45,66,78,87,76,98,89,55 };
	BinarySortTree mytree(a,10);
	cout << "中序遍历:";
	mytree.InOrderBST(mytree.T);
	mytree.DeleteBST(mytree.T, 78);
	cout << endl << "删除78后中序遍历:";
	mytree.InOrderBST(mytree.T);
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值