数据结构 二叉排序树的创建,查找,插入,删除

Description

实现二叉排序树的创建、查找、插入、删除操作。

Input

10

62 88 58 47 35 73 51 99 37 93

59

58

Output

输入二叉树的节点个数

开始建树BST,请输入10个节点

二叉排序树BST创建完成,

接下来进行中序遍历

35 37 47 51 58 62 73 88 93 99

接下来进行二叉排序树的查找,请输入关键值key

关键字key=59不在二叉排序树中

那么插入该关键值key

插入59后的中序遍历为:

35 37 47 51 58 59 62 73 88 93 99

现在进行二叉排序树的关键字删除,输入想删除的key值

删除key=58

删除后的中序遍历:

35 37 47 51 59 62 73 88 93 99

输出结果:

《数据结构 二叉排序树的创建,查找,插入,删除》

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

/*
 * 名称: 二叉排序树的查找
 * 方法: 先创建一个BST,然后对其进行查找,插入,删除,
 * 专业: 软件工程
 *  by : mazicwong
 */

/* 测试数据s
10
62 88 58 47 35 73 51 99 37 93
59
58
*/
typedef struct BiTNode {
	int data;
	struct BiTNode *leftchild, *rightchild;
}BiTnode,*BiTree;

/* BST查找
   f指向T的父母
   查找成功,则p指向该元素节点,返回true
   否则,p指向查找路径的最后一个节点,返回false
*/
bool searchBST(BiTree T,int key,BiTree f,BiTree &p)
{
	if (!T)
	{
		p = f;
		return false;
	}
	else if (key == T->data)
	{
		p = T;
		return true;
	}
	else if (key < T->data)
		return searchBST(T->leftchild, key, T, p);
	else
		return searchBST(T->rightchild, key, T, p);
}

//BST插入
bool insertBST(BiTree &T, int key)
{
	BiTree p, s;
	//因为search过程中有进行比较,所以search完T就指向该插入位置的父亲
	if (!searchBST(T, key, NULL, p))
	{
		s = (BiTree)malloc(sizeof(BiTNode));
		s->data = key;
		s->leftchild = s->rightchild = NULL;
		if (!p)  T = s;//p是空的说明树是空的,则插入在根节点
		else if(key < p->data) p->leftchild = s;
		else     p->rightchild = s;
		return true;
	}
	else
		return false;
}

/* BST删除
   删掉一个节点
   右子树空,则重接左子树
   左子树空,则重接右子树
   两个子树都是非空的,则找左子树一直向右的节点,替换到删掉节点那里,
*/
bool Delete(BiTree &p)
{
	BiTree q, s;
	if (p->rightchild == NULL) //右子树空,则重接左子树
	{
		q = p; p = p->leftchild; free(q);
		//p = p->leftchild; 不能直接这样,因为要释放内存
	}
	else if (p->leftchild == NULL)  //左子树空,则重接右子树
	{
		q = p; p = p->rightchild; free(q);
	}
	else //两个子树都是非空的
	{
		q = p; s = p->leftchild;
		while (s->rightchild)
		{
			q = s;  //q指向左子树的最右孩子的老爸
			s = s->rightchild;  //s指向左子树的最右孩子
		}
		p->data = s->data;  
		if (q != p) //左子树的最右孩子有可能有左孩子,所以还要留下
			q->rightchild = s->leftchild;  //重接q的右子树
		else  //即左子树,没有右孩子,直接移上来(q==p),即p->left=s->left
			q->leftchild = s->leftchild;  //重接q的左子树
		free(s);
	}
	return true;
}

/* BST删除
   若BST存在与key相等的节点,则删除该节点,返回true
*/
bool deleteBST(BiTree &T,int key)
{
	if (!T)
		return false;
	else
	{
		if (key == T->data)
			return Delete(T);
		else if (key < T->data)
			return deleteBST(T->leftchild, key);
		else
			return deleteBST(T->rightchild, key);
	}
}

void InOrderTraverse(BiTree T)
{
	if (T)
	{
		InOrderTraverse(T->leftchild);
		printf("%d ", T->data);
		InOrderTraverse(T->rightchild);
	}
}
int main()
{
	while (1)
	{
		//二叉树的创建
		int n;
		printf("输入二叉树的节点个数\n");
		scanf("%d", &n);
		if (n == 0) break;
		BiTree T = NULL;
		printf("开始建树BST,请输入%d个节点\n", n);
		for (int i = 0; i < n; i++)
		{
			int a;
			scanf("%d", &a);
			insertBST(T, a);
		}
		printf("二叉排序树BST创建完成,\n接下来进行中序遍历\n");
		InOrderTraverse(T);
		puts("");

		//二叉树的查找与插入
		BiTree f = NULL;
		BiTree p;
		printf("接下来进行二叉排序树的查找,请输入关键值key\n");
		int key;
		scanf("%d", &key);
		if (searchBST(T, key, f, p))
			printf("关键字key=%d在二叉排序树中\n", key);
		else
		{
			printf("关键字key=%d不在二叉排序树中\n", key);
			printf("那么插入该关键值key\n");
			insertBST(T, key);
			printf("插入%d后的中序遍历为: \n", key);
			InOrderTraverse(T);
			puts("");
		}

		//二叉树的删除
		printf("现在进行二叉排序树的关键字删除,输入想删除的key值\n");
		scanf("%d", &key);
		if (searchBST(T, key, f, p))
		{
			printf("删除key=%d\n", key);
			deleteBST(T, key);
			printf("删除后的中序遍历: \n");
			InOrderTraverse(T);
			printf("\n\n");
		}
		else
			printf("关键字%d不在二叉排序树中\n", key);
	}
	return 0;
}
    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/mazicwong/article/details/53320905
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞