话不多说 直接上代码
复制可直接运行
#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;
}