二叉查找树相关函数的实现

.h文件
typedef  int elemtype;
#include <stdio.h>
#include <iostream>
#include <tchar.h>
using namespace std;
struct BSTNode
{
	elemtype val;
	BSTNode *left_child, *right_child;
	BSTNode(const elemtype &_val) :val(_val), left_child(NULL), right_child(NULL){}
};

class BST_Class
{
public:
	BST_Class();
	BST_Class(const BST_Class &rhs);
	~BST_Class();

	struct BSTNode *create_tree(elemtype *ele, int  n);     //创造一个二叉排序树
	void  BST_Insert(BSTNode *pra,BSTNode *root,elemtype e); //插入一个元素,利用递归
	void midorder(const BSTNode *root) const;    //中序遍历,元素由小到大排列
	void Destroy_binarytree(BSTNode *root);     //销毁二叉树
	bool isEmpry(BSTNode *root);         //判断是否为空
	void Delete_BST(BSTNode *pra,BSTNode *root, elemtype e);    //删除元素
	
	BSTNode *Root;
};

.cpp文件

BST_Class::BST_Class()
{

}
BST_Class::BST_Class(const BST_Class &rhs)
{
	this->Root = rhs.Root;
}
BST_Class::~BST_Class()
{
	Destroy_binarytree(this->Root);
}
struct BSTNode* BST_Class::create_tree(elemtype *ele, int  n)
{
	BSTNode *root = NULL;
	root = new BSTNode(ele[0]);           //先建立根节点
	for (int i = 1; i < n; ++i)           //循环调用插入元素函数
	{
		BST_Insert(root,root, ele[i]);
	}		
	return root;
}
void BST_Class::BST_Insert(BSTNode *pra,BSTNode *root, elemtype e)
{

	if (!root)                               //root为空,创造新节点
	{
		BSTNode *newNode = new BSTNode(e);
		if (pra->val > e) pra->left_child = newNode;
		else pra->right_child = newNode;
		
	}
	else if (e<root->val)            //递归调用,找到插入位置
	{
		 BST_Insert(root, root->left_child, e);
	}
	else if (e>root->val)
	{
		 BST_Insert(root, root->right_child, e);
	}
}
void BST_Class::Destroy_binarytree(BSTNode *root)
{
	if (root)
{
	Destroy_binarytree(root->left_child);
	Destroy_binarytree(root->right_child);
	free(root);
	this->Root = NULL;
}
}
void BST_Class::midorder(const BSTNode *root) const
{
	if (root)
	{
		midorder(root->left_child);
		cout << root->val << endl;
		midorder(root->right_child);
	}
}
bool BST_Class::isEmpry(BSTNode *root)
{
	if (root) return true;
		return false;
}
void BST_Class::Delete_BST(BSTNode *pra,BSTNode *root, elemtype e)
{ 
	if (root)
	{
		if (e < root->val) Delete_BST(root,root->left_child, e);
		else if (e > root->val) Delete_BST(root,root->right_child, e);
		else
		{ 
			if (root->right_child&&root->right_child)        //有二个孩纸
			{

				BSTNode *p = root,*q=root->right_child;
	//找到待删除元素的右子树的最大的元素,q指向最大元素,p指向q的父节点
				while (q->right_child){ p = q; q = q->right_child; } 
	//最大元素没有右子树(否则不是最大元素),令其父节点(p)指向其左子树		
				p->right_child = q->left_child;  
         // 最大元素的左右孩纸指针指向待删除元素的左右孩纸指针
				q->left_child = root->left_child;
				q->right_child = root->right_child;
       //待删除元素的父节点指向最大元素q
				if (pra->val > e) pra->left_child = q;
				else pra->right_child = q;
			}
			else if (root->left_child) //只有左孩纸
			{
				if (pra->val > e) pra->left_child = root->left_child;
				else pra->right_child = root->left_child;
			}
			else if (root->right_child)//只有右孩纸
			{
				if (pra->val > e) pra->left_child = root->right_child;
				else pra->right_child = root->right_child;
			}
			else //没有孩纸
			{
			 	if (pra->val > e) pra->left_child = NULL;
				else pra->right_child = NULL;
			}
			free(root);//释放删除元素空间				
		}
	}

}


转载于:https://my.oschina.net/u/2420022/blog/481574

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值