二叉查找树的几个操作

这里是题目的图片

在这里插入图片描述

#include<iostream>
#include<string.h>
using namespace std;

class Node   // 结点类定义
{
public:
	int value;
	int ceng;
	Node *left;
	Node *right;
	Node(int value1)
	{
		ceng = 1;
		value = value1;
		left = NULL;
		right = NULL;
	}
};
class BST   //二叉查找树类定义
{
public:
	int t;
	int*p=new int[t];
	Node* root;
	BST(int t1, int* p1)  //析构函数
	{
		t = t1;
		p = p1;
		/*root = NULL;*/
		for (int i = 0; i < t; i++)
		{
			put(root,p[i]);
		}
	}
	void put(Node* &x,int value)   //加入结点
	{
		if (x == NULL)            //如果为空,就创建新节点
		{
			x = new Node(value);
		}
		else                      //如果不为空,判断和子树根节点value的大小,小则左,大则右
		{
			if (value < x->value)
				put(x->left, value);
			else
				put(x->right, value);
		}
	}
	void del(Node*& x, int valuee)
	{
		if (root == NULL)
		{
			cout << "真的不好意思,没有这个结点!" << endl;
			return;
		}
		if (x == NULL)            //如果为空就算了
		{
			
		}
		else                      //如果不为空,判断是否是当前value
		{
			if (x->value == valuee)
			{
				if (x->left != NULL && x->right == NULL)
				{
					x = x->left;
				}
				else
				{	
					if (x->right != NULL && x->left == NULL)
					{
						x = x->right;
					}
					if (x->right != NULL && x->left != NULL)
					{
						Node* y = x->right;
						while (y->left != NULL)
						{
							y = y->left;
						}
						y->left = x->left;
						x = x->right;
					}
					if (x->right == NULL && x->left == NULL)
					{
						x = NULL;
					}
				}
			}
			else
			{
				if (valuee < x->value)
					del(x->left, valuee);
				else
					del(x->right, valuee);
			}
		}
	}
	void printtree(Node*& x)     //中序遍历打印
	{
		if (root == NULL)
		{
			cout << "真的不好意思,这是一个空树" << endl;
			return;
		}
		if (x != NULL)
		{
			if (x->left != NULL)
			{
				printtree(x->left);
			}
			cout << x->value << " ";
			if (x->right != NULL)
			{
				printtree(x->right);
			}
		}
	}
	void findlen(Node*& x,int &sum,int n)    //n是层数,给一个1值
	{
		
		if (root == NULL)
		{
			sum = 0;
		}
		if (x != NULL)
		{
			sum += n;
			if (x->left != NULL)
			{
				findlen(x->left, sum, n+1);
			}
			if (x->right != NULL)
			{
				findlen(x->right, sum, n+1);
			}
		}
		
	}
};

int main()
{
	int t;  //数列长度
	cout << "请输入数列长度并依次输入数列各项的值" << endl;
	cin >> t;
	int* p = new int[t];
	for (int i = 0; i < t; i++)
	{
		cin >> p[i];
	}
	BST B(t, p);
	int q;
	while (1)
	{
		cout << "请输入您想进行的操作:" << endl << "1:中序遍历打印" << endl << "2:输出平均查找长度" << endl << "3:查找x并删除" << endl;
		cin >> q;
		if (q == 1)
		{
			B.printtree(B.root);
		}
		else
		{
			if (q == 2)
			{
				int s = 0;
				B.findlen(B.root, s, 1);
				cout << s << endl;
			}
			else
			{
				cout << "请输入要查找的字符" << endl;
				int w;
				cin >> w;
				B.del(B.root, w);
				B.printtree(B.root);
			}
		}
		cout << endl << endl;
	}
}

我这里使用链表实现,如有错误,请多多指正,感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值