C++二叉查找树增删改查功能实现

二叉查找树是二叉树中的一个特例,其中序遍历结果是从小到大的。

由于二叉树的特殊结构,其增删改查功能都是可以用递归实现的。

代码如下:

//tree.h
#include <iostream>
using namespace std;

//结构体定义
typedef struct BST
{
	int data;
	BST* leftChild;//左子节点
	BST* rightChild;//右子节点
};

BST* CreatBST(); //从键盘输入创建树
void DeleteTree(BST* tree);//删除树
void PreTraversal(BST* tree);//前序遍历
void PostTraversal(BST* tree);//后序遍历
void InorderTraversal(BST* tree);//中序遍历
void Insert(BST* &tree, int data);//插入元素
int FindMax(BST* tree);//寻找最大值
int FindMin(BST* tree);//寻找最小值
bool isExist(BST* tree, int data);//判断某个值是否存在
void Remove(BST* &tree, int data);//删除一个元素

//tree.cpp
#include <iomanip>
#include "tree.h"

BST* CreatBST()
{
	BST* tree;
	int data;
	cin >> data;
	if (data == -1) //若输入为-1,则判定该节点为空
	{
		tree = NULL;
	}
	else
	{
		tree = new BST;
		tree->data = data;
		tree->leftChild = CreatBST();
		tree->rightChild = CreatBST();
	}
	return tree;
}

void DeleteTree(BST* tree)
{
	if (tree != NULL)
	{
		DeleteTree(tree->leftChild);
		DeleteTree(tree->rightChild);
		delete tree;
		tree = NULL;
	}
}

void PreTraversal(BST* tree)
{
	if (tree != NULL)
	{
		cout << setw(4) << tree->data;
		PreTraversal(tree->leftChild);
		PreTraversal(tree->rightChild);
	}
}

void PostTraversal(BST* tree)
{
	if (tree != NULL)
	{
		PostTraversal(tree->leftChild);
		PostTraversal(tree->rightChild);
		cout << setw(4) << tree->data;
	}
}

void InorderTraversal(BST* tree)
{
	if (tree != NULL)
	{
		InorderTraversal(tree->leftChild);
		cout << setw(4) << tree->data;
		InorderTraversal(tree->rightChild);
	}
}

void Insert(BST* &tree, int data)
{
	if (tree == NULL)
	{
		tree = new BST;
		tree->data = data;
		tree->leftChild = NULL;
		tree->rightChild = NULL;
	}
	else 
	{
		if (data > tree->data)
		{
			Insert(tree->rightChild, data);
		}
		else if (data < tree->data)
		{
			Insert(tree->leftChild, data);
		}
		else
			cout << "Insert failed!" << endl;
	}
}
int FindMax(BST* tree)
{
	int maxVal;
	if (tree != NULL)
	{
		if (tree->rightChild != NULL)
			FindMax(tree->rightChild);
		else
			return tree->data;
	}
	else
	{
		cout << "Tree is NULL." << endl;
		return NULL;
	}
}
int FindMin(BST* tree)
{
	int minVal;
	if (tree != NULL)
	{
		if (tree->leftChild != NULL)
			FindMax(tree->leftChild);
		else
			return tree->data;
	}
	else
	{
		cout << "Tree is NULL." << endl;
		return NULL;
	}
}

bool isExist(BST* tree, int data)
{
	if (tree == NULL)
		return false;
	if (tree->data == data)
		return true;
	else if (tree->data > data)
		isExist(tree->leftChild, data);
	else
		isExist(tree->rightChild, data);
}

void Remove(BST* &tree, int data)
{
	if (tree != NULL)
	{
		if (data < tree->data)
			Remove(tree->leftChild, data);
		else if (data > tree->data)
			Remove(tree->rightChild, data);
		else if (tree->leftChild != NULL)
		{
			tree->data = FindMax(tree->leftChild);
			Remove(tree->leftChild, tree->data);
		}
		else if (tree->rightChild != NULL)
		{
			tree->data = FindMin(tree->rightChild);
			Remove(tree->rightChild, tree->data);
		}
		else
		{
			BST* node = tree;
			tree = tree->leftChild ? tree->leftChild : tree->rightChild;
			delete node;
			node = NULL;
		}

	}
}

int main()
{
	cout << "Create a Binary Search Tree by typing int numbers: " << endl;
	cout << endl;
	BST* tree = CreatBST();//6 2 1 -1 -1 4 3 -1 -1 -1 8 -1 -1

	cout << "In-order traversal: " << endl << endl;
	InorderTraversal(tree);
	cout << endl << endl;

	cout << "Post-order traversal: " << endl << endl;
	PostTraversal(tree);
	cout << endl << endl;

	cout << "Pre-order traversal: " << endl << endl;
	PreTraversal(tree);
	cout << endl << endl;

	cout << "Insert 5: " << endl;
	Insert(tree, 5);
	cout << "In-order traversal: " << endl << endl;
	InorderTraversal(tree);
	cout << endl << endl;

	cout << "Remove 6: " << endl;
	Remove(tree, 6);
	cout << "In-order traversal: " << endl;
	InorderTraversal(tree);
	cout << endl << endl;

	cout << "Check if a number is exist: " << endl;
	cout << "Please type a number: ";
	int num;
	cin >> num;
	cout << endl;
	if (isExist(tree, num))
		cout << num << " is exist" << endl;
	else
		cout << num << " is not exist" << endl;
	cout << endl;

	system("pause");
	return 0;
}

程序运行的结果如下图:
在这里插入图片描述输入的二叉树结构如下:
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值