【树】二叉排序树

本文详细介绍了二叉排序树的基本概念、插入、删除、查找等操作,并通过实例展示了如何使用二叉排序树实现有序输出。代码示例涵盖了树节点结构定义、二叉排序树类实现以及关键操作的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BinarySortTree.h

#ifndef BINARYSORTTREE_H
#define BINARYSORTTREE_H
#include<iostream>
//binary sort tree node
struct binary_sort_tree_node{
	int data;
	binary_sort_tree_node* lchild;
	binary_sort_tree_node* rchild;
	binary_sort_tree_node() :lchild(NULL), rchild(NULL){}
	binary_sort_tree_node(int n) :data(n), lchild(NULL), rchild(NULL){}
};
//BinarySortTree
class BinarySortTree{
public:
	typedef binary_sort_tree_node node_value;
	typedef binary_sort_tree_node* node_pointer;
	BinarySortTree();
	~BinarySortTree();
	node_pointer get_root();
	bool insert_bst(node_pointer* p, int key);
	bool delete_bst(node_pointer* p, int key);
	bool del(node_pointer* p);
	bool search(node_pointer p, int key, node_pointer f, node_pointer* q);
	void order(node_pointer p);
private:
	void destroy(node_pointer p);
private:
	node_pointer root;
};
BinarySortTree::BinarySortTree() :root(NULL){}
BinarySortTree::~BinarySortTree(){
	destroy(root);
	root = NULL;
}
BinarySortTree::node_pointer BinarySortTree::get_root(){
	return root;
}
/*  当二叉排序树p中不存在关键字等于key的数据元素时,
插入key并返回true,否则返回false */
bool BinarySortTree::insert_bst(node_pointer* p, int key){
	node_pointer q, s;
	if (!search(*p, key, NULL, &q)){ //查找不成功 
		s = new node_value(key);
		if (q == NULL)
			*p = s; //插入s为新的根结点 
		else if (key < q->data)
			q->lchild = s; //插入s为左孩子 
		else
			q->rchild = s; //插入s为右孩子 
		return true;
	}
	else
		return false;
}
/* 若二叉排序树p中存在关键字等于key的数据元素时,则删除该数据元素结点,
并返回true;否则返回false */
bool BinarySortTree::delete_bst(node_pointer* p, int key){
	if (*p == NULL)//不存在关键字等于key的数据元素 
		return false;
	else{
		if (key == (*p)->data)//找到关键字等于key的数据元素
			return del(p);
		else if (key < (*p)->data)
			return delete_bst(&(*p)->lchild, key);
		else
			return delete_bst(&(*p)->rchild, key);
	}
}
/* 从二叉排序树中删除结点p,并重接它的左或右子树。 */
bool BinarySortTree::del(node_pointer* p){
	node_pointer q, s;
	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)->data = s->data;
		if (q != *p)//重接q的右子树
			q->rchild = s->rchild;
		else//重接q的左子树
			q->lchild = s->lchild;
		delete s;
	}
	return true;
}
/* 递归查找二叉排序树p中是否存在key,
指针f指向p的双亲,其初始调用值为NULL
若查找成功,则指针q指向该数据元素结点,并返回true
否则指针q指向查找路径上访问的最后一个结点并返回false */
bool BinarySortTree::search(node_pointer p, int key, node_pointer f, node_pointer* q){
	if (p == NULL){
		*q = f;
		return false;
	}
	else if (key == p->data){
		*q = p;
		return true;
	}
	else if (key < p->data)
		return search(p->lchild, key, p, q);
	else
		return search(p->rchild, key, p, q);
}
void BinarySortTree::destroy(node_pointer p){
	if (p == NULL)
		return;
	destroy(p->lchild);
	destroy(p->rchild);
	delete p;
}
/*  中序遍历就能有序输出  */
void BinarySortTree::order(node_pointer p){
	if (p){
		order(p->lchild);
		std::cout << p->data << " ";
		order(p->rchild);
	}
}

#endif
main.cpp

#include"BinarySortTree.h"
using namespace std;

void menu(){
	cout << "----------------------------------------------------------------" << endl;
	cout << "1:建立二叉排序树" << endl;
	cout << "2:二叉排序树的插入操作" << endl;
	cout << "3:二叉排序树有序输出" << endl;
	cout << "4:二叉排序树的删除操作" << endl;
	cout << "0:退出菜单" << endl;
	cout << "----------------------------------------------------------------" << endl;
}

int main(){
	int choose;
	int num;
	bool flag = true;
	int a[10] = { 45, 56, 24, 89, 67, 84, 56, 39, 64, 55 };
	BinarySortTree binary_sort_tree;
	binary_sort_tree_node* root = binary_sort_tree.get_root();
	while (flag){
		menu();
		cout << "请选择要进行的操作:" << endl;
		cin >> choose;
		switch (choose){
		case 1:
			int i;
			for (int i = 0; i < 10; i++)
				binary_sort_tree.insert_bst(&root, a[i]);
			break;
		case 2:
			cout << "请输入要插入的数:" << endl;
			cin >> num;
			if (binary_sort_tree.insert_bst(&root, num))
				cout << "插入数据成功" << endl;
			else
				cout << "要插入的数据已经存在" << endl;;
			break;
		case 3:
			binary_sort_tree.order(root);
			cout << endl;
			break;
		case 4:
			cout << "请输入删除的数:" << endl;;
			cin >> num;
			if (binary_sort_tree.delete_bst(&root, num))
				cout << "删除成功" << endl;
			else
				cout << "不存在关键字等于key的数据元素" << endl;
			break;
		case 0:
			flag = false;
			break;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值