数据结构:二叉树构建及其运算的递归设计

typedef char ElementType;
#define MAX_SIZE 20
typedef struct BookTreeNode
{
	ElementType data;
	struct BookTreeNode* left;
	struct BookTreeNode* right;
}BTNode;

括号表示法创建二叉树存储结构

我们想使用A(B,C(D,E))这样的方式创建一个树结构

  1. A:为树的根节点
  2. (:左括号,说明其具有左孩子,准备左插孩子
  3. ,:逗号吗,说明其具有右孩子,准备右插孩子
  4. ):右括号,说明其左右孩子均处理完毕,准备下一次插入孩子
void CreateBTree(BTNode*& bt, const char* arr)
{
	bt = nullptr;
	BTNode* St[MAX_SIZE]{ 0 };	//存储树结构的栈
	BTNode* pRoot = nullptr;
	int arr_index = 0;
	int top = 0;	//栈顶指针
	int choice = 1;	//choice=1:左插   choice=2:右插
	char ch;
	ch = arr[arr_index];

	while (ch!='\0')
	{
		switch (ch)
		{
			case '(':
			{
				top++;
				*(St+top) = pRoot;
				choice = 1;
				break;
			}
			case ')':
			{
				top--;
				break;
			}
			case ',':
			{
				choice = 2;
				break;
			}
			default:
			{
				pRoot = new BTNode;
				pRoot->data = ch;
				pRoot->left = pRoot->right = nullptr;
				if (bt == nullptr)
				{
					//当树为空的时候,创建根节点
					bt = pRoot;
				}
				else
				{
					//树不为空,根据k
					switch (choice)
					{
					case 1:
						St[top]->left = pRoot;
						break;
					default:
						St[top]->right = pRoot;
						break;
					}
				}
				break;
			}
		}
		arr_index++;
		ch = arr[arr_index];
	}
}

释放二叉树的空间

我们采用大问题转换为小问题的方式:

递归模型:

  1. 当树为空的时候:f(bt)=不做任何事情
  2. 树节点不为空:f(bt)=f(bt->left);f(bt)=f(bt->right);delete bt
void DestroyBTree(BTNode*& bt)
{
	if (bt != nullptr)
	{
		DestroyBTree(bt->left);
		DestroyBTree(bt->right);
		delete bt;
	}
}

求树的高度

两种方法: 个人感觉第一个比较好理解。

int BTHeight(BTNode*& bt)
{
#if 0
	int leftHeight, rightHeight;
	if (bt == nullptr)
	{
		//树为空
		return 0;
	}
	else
	{
		leftHeight = BTHeight(bt->left);	//求左子树高度
		rightHeight = BTHeight(bt->right);	//求右子树高度
		return (leftHeight > rightHeight) ? (leftHeight + 1) : (rightHeight + 1);
	}
#else
	if (bt == nullptr)
	{
		return 0;
	}
	else
	{
		return max(BTHeight(bt->left), BTHeight(bt->right)) + 1;
	}
#endif
}

求树的节点个数


int GetNodeCount(BTNode*& bt)
{
#if 0 
	int leftNumber, rightNumber;
	if (bt == nullptr)
	{
		return 0;
	}
	else
	{
		leftNumber = GetNodeCount(bt->left);
		rightNumber = GetNodeCount(bt->right);
		return (leftNumber + rightNumber + 1);
	}
	return 0;
#else
	if(bt == nullptr)
	{
		return 0;
	}
	else
	{
		return GetNodeCount(bt->left) + GetNodeCount(bt->right) + 1;
	}

#endif
}

求树的所有叶子节点个数

int GetLitCount(BTNode*& bt)
{
	int left, right;
	if (bt == nullptr)
	{		//bt为nullptr
		return 0;
	}
	else if (bt->left==nullptr && bt->right==nullptr)
	{		//bt为叶子节点
		return 1;
	}
	else
	{		
		left = GetLitCount(bt->left);//求左子树的叶子节点个数
		right = GetLitCount(bt->right);//求右子树叶子节点的个数
		return (left + right);	//返回和
	}
}

括号表示法输出树结构

使用的是根左右的前序遍历方式

void Display(BTNode*& bt)
{
	if (bt != nullptr)
	{
		cout.put(bt->data);
		if (bt->left != nullptr || bt->right != nullptr)
		{
			cout.put('(');
			Display(bt->left);
			if (bt->right != nullptr)
			{
				cout.put(',');
			}
			Display(bt->right);
			cout.put(')');
		}
	}
}

完整测试

#include "MyTree.h"

int main()
{
	BTNode* tree;
	CreateBTree(tree, "A(B(D,E(G,H)),C)");
	cout << GetLitCount(tree) << endl;
	cout << GetNodeCount(tree) << endl;
	cout << BTHeight(tree) << endl;
	Display(tree);
	DestroyBTree(tree)return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yuleo_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值