1.题目:二叉树遍历
前序遍历:根左右
中序遍历:左根右
后序遍历:左右根
层序遍历:从上往下、从左往右
迭代遍历:使用迭代方法实现递归函数,
与递归等价 morris遍历
2.算法:
迭代算法:
3.算法思想: (这东西讲不清 bibi 看视频!!!)
1.前序遍历: 首先在根开始向左遍历,到那个节点就打印数据,在递归左边节点,在递归右边节点,
2.中序遍历:首先在根开始遍历,遍历到最左边的时候才开始打印数据,因为是先打印左边啊!所以我们需要到最左边。然后递归右节点
3.后序遍历: 首先我们先遍历到最左边,然后遍历最右边,最后打印数据。
4.层序遍历: 首先我们,需要用到队列,的先进先出的特性来,实现。(开代码!!!不知道怎么说!!!)
代码:
/*************************************************
作者:She001
时间:2022/9/31
题目:二叉树遍历
前序遍历:根左右
中序遍历:左根右
后序遍历:左右根
层序遍历:从上往下、从左往右
递归遍历:使用递归方法遍历
迭代遍历:使用迭代方法实现递归函数,
与递归等价 morris遍历
***************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef struct student
{
int a;
struct student * left;
struct student * right;
int shendu;//树的深度
}node; //指针类型的
/*//二叉树模型
a1
a2 a3
a4 a5 a6 a7
a8
a9
*/
///
//迭代遍历的方法实现
/*
前序遍历:根左右
中序遍历:左根右
后序遍历:左右根
层序遍历:从上往下、从左往右
*/
void fangfa_2(node * t)//中序遍历
{
if(t ==NULL)
{
printf("空间开辟失败!\n");
return;
}
//准备一个栈
node* stack[100];
int stack_num=-1 ;
node * temp = t;
while(stack_num!=-1 || temp!=NULL )
{
while(temp!=NULL)
{
stack[++stack_num]=temp;
temp=temp->left;
}
if(stack_num!=-1) //注意打印过的数据,其指针不再进栈 数据思路还行
{
temp=stack[stack_num--];
cout<<temp->a<<" "; //先打左节点 再打本身
temp=temp->right;
}
}
}
void fangfa_3(node * t)//后序列
{
if(t ==NULL)
{
printf("空间开辟失败!\n");
return;
}
//准备一个栈
node *stack[100];
int stack_num=-1 ;
node* temp = t;
node* plastVisit=NULL; //访问标记
while(temp!=NULL )
{
stack[++stack_num]=temp;
temp=temp->left; //先移到 最左边
}
while(stack_num!=-1)
{
temp=stack[stack_num--];
if(temp->right==NULL || temp->right==plastVisit)
{
cout<<temp->a<<" "; //到了节点打印数据 或者右边的树已经打过
plastVisit=temp; //标记这个最右的节点已经走过
}
else
{
stack[++stack_num]=temp;
temp=temp->right;
while(temp!=NULL)
{
stack[++stack_num]=temp;
temp=temp->left;
}
}
}
}
void fangfa_1(node * t)//前序
{
if(t ==NULL)
{
printf("空间开辟失败!\n");
return;
}
//准备一个栈
node* stack[100];
int stack_num=-1 ;
node* temp = t;
while(stack_num!=-1 || temp!=NULL) //开始因为 节点不是空的所以可以进去
{
while(temp!=NULL)
{
cout<<temp->a<<" ";
stack[++stack_num] = temp; //走过的路需要记载 进栈
temp=temp->left;
}
if(stack_num!=-1)
{
temp=stack[stack_num];
stack_num--;
temp=temp->right; //看看这个节点的右节点
}
}
}
void fangfa_4(node* root)//cengxi
{
if(root==NULL)
{
return;
}
queue<node * >q;
q.push(root);
while(!q.empty())//不为空就执行
{
node* hh=q.front();//获取即将出队的数据
cout<<hh->a<<" ";//打印数据
q.pop();//出队
if(hh->left!=NULL)
{
q.push(hh->left);
}
if(hh->right!=NULL)
{
q.push(hh->right);
}
}
}
int main()
{
//数据初始化,建立树
struct student *a1 =new struct student;
struct student *a2 =new struct student;
struct student *a3 =new struct student;
struct student *a4 =new struct student;
struct student *a5 =new struct student;
struct student *a6 =new struct student;
struct student *a7 =new struct student;
struct student *a8 =new struct student;
struct student *a9 =new struct student;
//数值的赋值
a1->a=1;
a2->a=2;
a3->a=3;
a4->a=4;
a5->a=5;
a6->a=6;
a7->a=7;
a8->a=8;
a9->a=9;
//节点的连接
a1->left=a2;
a1->right=a3;
a2->left=a4;
a2->right=a5;
a3->left=a6;
a3->right=a7;
a6->left=a8;
a8->right=a9;
//节点为空的设置
a4->left=NULL;
a4->right=NULL;
a5->left=NULL;
a5->right=NULL;
a8->left=NULL;
a9->left=NULL;
a9->right=NULL;
a6->right=NULL;
a7->left=NULL;
a7->right=NULL;
//打印前序遍历
cout<<"前序遍历: ";
fangfa_1(a1);
cout<<endl;
//打印中序遍历
cout<<"中序遍历: ";
fangfa_2(a1);
cout<<endl;
//打印后序遍历
cout<<"后序遍历 : ";
fangfa_3(a1);
cout<<endl;
//打印层序遍历
cout<<"层序遍历 : ";
fangfa_4(a1);
cout<<endl;
//释放空间
delete a1;
delete a2;
delete a3;
delete a4;
delete a5;
delete a6;
delete a7;
delete a8;
delete a9;
return 0;
}