[手撕数据结构] 二叉树及其基本操作

话不多说 直接上代码 

复制可直接运行

​
#include<math.h>
#include<stdlib.h>
#include<string>
#include<stdbool.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct TreeNode
{
	char data;
	struct TreeNode* left;
	struct TreeNode* right;
}Tree;
int EmptyTreeNULL(Tree* root)
{
	if (root->left == NULL && root->right == NULL)
	{
		return 1;
	}
	else
		return 0;
}
Tree* creatTeNode(char x)//创建结点
{
	Tree* newNode = (Tree*)malloc(sizeof(Tree));
	if (newNode == NULL)
	{
		cout << "创建结点失败" << endl;
		free(newNode);
		return 0;
	}
	newNode->data = x;
	newNode->left = NULL;
	newNode->right = NULL;
	return newNode;
}
void Preorder_Traversal(Tree* root)//先序遍历 递归
{
	if (root == NULL)
	{
		return;
	}
	cout << root->data << "     ";
	Preorder_Traversal(root->left);
	Preorder_Traversal(root->right);
}
void Inorder_Traversal(Tree* root)//中序遍历 递归
{
	if (root == NULL)
	{
		return;
	}
	Inorder_Traversal(root->left);
	cout << root->data << "     ";
	Inorder_Traversal(root->right);
}
void Postorder_Traversal(Tree* root)//后续遍历 递归
{
	if (root == NULL)
	{
		return;
	}
	Postorder_Traversal(root->left);
	Postorder_Traversal(root->right);
	cout << root->data << "     ";
}
void Preorder_Traversal_UseStack(Tree* root)//先序遍历 迭代
{
	Tree** stack = (Tree**)malloc(sizeof(Tree) * 100);
	if (stack == NULL)
	{
		cout << "储存栈建立失败" << endl;
		free(stack);
		return;
	}
	int top = 0;
	while (root != NULL || top != 0)
	{
		while (root != NULL)
		{
			cout << root->data << "     ";
			stack[top++] = root;
			root = root->left;
		}
		root = stack[--top];
		root = root->right;
	}
	free(stack);
	cout << endl;
}
void Inorder_Traversal_UseStack(Tree* root)//中序遍历 迭代
{
	Tree** stack = (Tree**)malloc(sizeof(Tree) * 100);
	if (stack == NULL)
	{
		cout << "储存栈建立失败" << endl;
		free(stack);
		return;
	}
	int top = 0;
	while (root != NULL || top != 0)
	{
		while (root != NULL)
		{
			stack[top++] = root;
			root = root->left;
		}
		root = stack[--top];
		cout << root->data << "     ";
		root = root->right;
	}
	free(stack);
	cout << endl;
}
void Postorder_Traversal_UseStack(Tree* root)//后序遍历 迭代
{
	Tree** stack = (Tree**)malloc(sizeof(Tree) * 100);
	if (stack == NULL)
	{
		cout << "储存栈建立失败" << endl;
		free(stack);
		return;
	}
	Tree* visitNode = root;//保存上一个访问(打印)过的结点
	int top = 0;
	while (root != NULL || top != 0)
	{
		while (root != NULL)
		{
			stack[top++] = root;
			root = root->left;//先访问左子树
		}
		root = stack[top - 1];//保存上一个结点
		if (root->right != NULL && root->right != visitNode)//如果最左边的叶子结点的上一个结点有右子树且没有被访问
		{
			root = root->right;//访问右子树
		}
		else//如果没有右子树或者已经被访问则输出
		{
			cout << root->data << "     ";
			visitNode = root;
			root = NULL;
			top--;//出栈
		}
	}
	//如果复习时候忘记流程  上B站look https://www.bilibili.com/video/BV18i4y1Z7am/?spm_id_from=333.788.recommend_more_video.0
}

void Sequence_Traversal(Tree* root)//层序遍历
{
	Tree* Queue[100];
	int head = 0;
	int tail = 0;
	Queue[tail++] = root;
	while (tail != head)
	{
		if (root->left != NULL)
			Queue[tail++] = root->left;
		if (root->right != NULL)
			Queue[tail++] = root->right;
		cout << root->data << "     ";
		root = Queue[++head];
	}
	cout << endl;
}

void Print_Leaf(Tree* root)//输出叶子结点
{
	cout << "叶子结点:";
	if (root == NULL)
	{
		cout << "空树" << endl;
		return;
	}
	Tree** stack = (Tree**)malloc(sizeof(Tree) * 100);
	if (stack == NULL)
	{
		cout << "储存栈建立失败" << endl;
		free(stack);
		return;
	}
	int top = 0;
	while (root != NULL || top != 0)
	{
		while (root != NULL)
		{
			if (root->left == NULL && root->right == NULL)
			{
				cout << root->data << "      ";
			}
			stack[top++] = root;
			root = root->left;
		}
		root = stack[--top];
		root = root->right;
	}
	free(stack);
	cout << endl;
}

int Get_Tree_high(Tree* root)//获取二叉树高度
{
	if (root == NULL)
		return 0;

	return max(1 + Get_Tree_high(root->left), 1 + Get_Tree_high(root->right));
}

void InsertTree(Tree* parents, Tree* left, Tree* right)//插入
{
	parents->left = left;
	parents->right = right;
}
int main()
{
	Tree* A = creatTeNode('A');
	Tree* B = creatTeNode('B');
	Tree* C = creatTeNode('C');
	Tree* D = creatTeNode('D');
	Tree* E = creatTeNode('E');
	Tree* F = creatTeNode('F');
	Tree* G = creatTeNode('G');
	InsertTree(A, B, C);
	InsertTree(B, D, E);
	InsertTree(C, F, G);
	cout << "先序遍历" << endl;
	Preorder_Traversal(A);
	cout << endl;
	Preorder_Traversal_UseStack(A);
	cout << "中序遍历" << endl;
	Inorder_Traversal(A);
	cout << endl;
	Inorder_Traversal_UseStack(A);
	cout << "后序遍历" << endl;
	Postorder_Traversal(A);
	cout << endl;
	Postorder_Traversal_UseStack(A);
	cout << endl;
	cout << "层序遍历" << endl;
	Sequence_Traversal(A);
	Print_Leaf(A);
	cout << "二叉树高度:" << Get_Tree_high(A)<<endl;
	system("pause"); 
	return 0;
}

​

                                                          效果如下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值