二叉搜索树

公众号:CppCoding

二叉树的性质:

若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;

若任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;

任意节点的左、右子树也分别为二叉查找树。

没有键值相等的节点。

在这里插入图片描述
二叉搜索树的时间复杂度为O(logN)

查找二叉树中的某个元素
  • 若b是空树,则搜索失败,否则:
  • 若x等于b的根节点的数据域之值,则查找成功;否则:
  • 若x小于b的根节点的数据域之值,则搜索左子树;否则:
  • 查找右子树。
插入某个元素
  • 若b是空树,则将s所指结点作为根节点插入,否则:
  • 若s->data等于b的根节点的数据域之值,则返回,否则:
  • 若s->data小于b的根节点的数据域之值,则把s所指节点插入到左子树中,否则:
  • 把s所指节点插入到右子树中。(新插入节点总是叶子节点)

在这里插入图片描述

删除某个元素
  • 如果结点z没有孩子节点,那么只需简单地将其删除,并修改父节点,用NULL来替换z;
  • 如果结点z只有一个孩子,那么将这个孩子节点提升到z的位置,并修改z的父节点,用z的孩子替换z;
  • 如果结点z有2个孩子,那么查找z的后继y,此外后继一定在z的右子树中,然后让y替换z。

在这里插入图片描述

结点类
class BinaryNode{
public:
	int element;
	BinaryNode* leftchild;
	BinaryNode* rightchild;
	BinaryNode(int theElement,BinaryNode* lt,BinaryNode * rt):element(theElement),leftchild(lt),rightchild(rt){};
	friend class BinarySearchTree;
	//其他函数成员略
};
二叉搜索树类
class BinarySearchTree{
public:
	BinaryNode* root;
	BinarySearchTree(BinaryNode* rt) {root=rt;}
	BinaryNode* findMax(BinaryNode* t) const;
	BinaryNode* findMin(BinaryNode* t) const;
	BinaryNode* find(int x,BinaryNode* t) const;
	void insert(int x,BinaryNode* &t);
	void remove(int x,BinaryNode * & t);
	void removeMin(BInary* & t);
	//其他函数省略
};
具体实现
//查找某一个数的结点
BinaryNode* BinarySearchtree::find(int x,BinaryNode* t)const{
	while(t)
		if(x<t->element)
			t=t->leftchild;
		else if(x>t->element)
			t=t->rightchild;
		else
			return t;
	return NULL;
}
//查找二叉搜索树的最大值
BinaryNode* BinarySearchTree::findMax(BinaryNode* t)const{
	if(t)
		while(t->rightchild!=NULL)
			t=t->rightchild;
	return t;
}
//查找最小的值
Binary* BinarySearchTree::findMin(BinaryNode* t) const{
	if(t)
		while(t->leftchild)
			t=t->leftchild;
	return t;
}
//插入一个元素
void BinarySearchTree::insert(int x,BinaryNode *& t){
	if(t==NULL)
		t=new BinaryNode(x,NULL,NULL);
	else if(x<t->element)
		insert(x,t->leftchild);
	else if(x>t->element)
		insert(x,t->rightchild);
	else{
		cout << "ERROR!!! "<< endl;
		return;
	}
}
//删除某一个结点
void BinarySearchTree::remove(int x,BinaryNode* & t){
	if(t==NULL){
		cout << "this node does not exist" << endl;
		return;
	}
	if(x<t->element)
		remove(x,t->leftchild);
	else if(x>t->element)
		remove(x,t->rightchild);
		//当函数判断要删除的结点有两个结点,它使用其右子树中关键码最小的结点来替换该结点,在调用removeMin函数将右子树中的原最小结点删除
	else if(t->leftchild!=NULL&&t->rightchild!=NULL){
		t->element=findMin(t->rightchild)->element;
		removeMin(t->rightchild);
	}else{
		BinaryNode* tmp=t;
		t=(t->leftchild!=NULL)?t->leftchild:t->rightchild;
		delete tmp;
	}
}

void BinarySearchTree::removeMin(BinaryNode* & t){
	if(t==NULL){
		cout << "empty tree" << endl;
		return;
	}
	else if(t->leftchild!=NULL){
		removeMin(t->leftchild);
	}else if(t->leftchild==NULL){
		BinaryNode* tmp=t;
		t=t->rightchild;
		delete tmp;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值