二叉树的遍历学习

对于一棵树:


树的遍历分为:

1.层序遍历: 一层一层的遍历i,相当于BFS:遍历顺序FCEADHGBM

在树已经构建出来的情况下,利用BFS输出层次遍历

2.前序遍历:先访问根节点,然后左子树,然后右子树,相当于DFS:FCADBEHGM

在树已经构建出来的情况下,输出代码如下:

void Print(int root)
{
   if(root == 0)
    return ;
   cout<<root<<endl;
   Print(tree[root].left);
   Print(tree[root].right);
   
}

3.中序遍历:先访问左子树,再访问根节点,再访问右子树:ACBDFHEMG

中序输出代码如下:

void Print(int root)
{
   if(root == 0)
    return ;
   Print(tree[root].left);
   cout<<root<<endl;
   Print(tree[root].right);
   
}

4.后序遍历:先访问左子树,再访问右子树,再访问根节点:ABDCHMGEF

后序代码如下:

void Print(int root)
{
   if(root == 0)
    return ;
   Print(tree[root].left);
   Print(tree[root].right);
   cout<<root<<endl;
}

一,已知后序和中序,求层次遍历;

团队程序设计天梯赛-练习集-L2-006 树的遍历


先利用后序和中序构建树,之后利用BFS输出层次遍历。

构树的原理:

因为后序数组的每一段的最后都是根节点,然后再中序数组之中找到这个数,然后继续进行分段,然后一直进行下去

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
struct Tree
{
    int left;
    int right;
};
Tree  tree[maxn];
int post[maxn];//后序数组
int in[maxn];//中序数组

int build(int post_low ,int post_high,int in_low,int  in_high)//利用后序遍历根节点在最后
{
    if(in_low >= in_high)
        return 0;
    int root = post[post_high - 1];
    int pos = in_low;
    while( in[pos] != root)
    {
        pos++;
    }
    int Count = pos - in_low;
    tree[root].left = build(post_low,post_low+Count , in_low,pos);
    tree[root].right = build(post_low+Count,post_high-1, pos+1,in_high);
    return root;
}
int n;
void bfs()//前序遍历就相当于bfs
{
    queue<int>q,ans;
    q.push(post[n- 1]);
    while(! q.empty())
    {
        int t = q.front();
        q.pop();
        if(t != 0)
            ans.push(t);
        if(tree[t].left != 0)
            q.push(tree[t].left);
        if(tree[t].right != 0)
            q.push(tree[t].right);
    }
    while(! ans.empty())
    {
        int t= ans.front();
        ans.pop();
        if(ans.empty())
            cout<<t<<endl;
        else cout<<t<<" ";
    }
    return ;
}

int main()
{
   while(~scanf("%d",&n) )
   {
       memset(tree,0,sizeof(tree));
       for(int i=0;i<n;i++)
        scanf("%d",&post[i]);
       for(int i =0 ; i<n;i++)
        scanf("%d",&in[i]);
       build(0,n,0,n);
       bfs();
   }
   return 0;
}

二,已知前序和中序,求层次;
团队程序设计天梯赛-练习集-L2-011 玩转二叉树


#include<bits/stdc++.h>
using namespace std;

const int maxn = 100000 + 10;
int n;
int pre[maxn];//前序数组
int in[maxn];//中序数组
struct Tree
{
    int left;
    int right;
};
Tree tree[maxn];
int Build(int pre_low,int pre_high,int in_low,int in_high)
{
    if(in_low >= in_high)
    {
        return 0;
    }
    int root = pre[pre_low];
    int pos = in_low;
    while( in[pos] != root)
    {
        pos ++;
    }
    int Count = pos -in_low;
    tree[root].left = Build(pre_low+1,pre_low+Count+1,in_low,pos);
    tree[root].right = Build(pre_low+Count+1,pre_high,pos+1,in_high);
    return root;
}
void bfs()
{
    queue<int>Q,ans;
    Q.push(pre[0]);
    while(! Q.empty())
    {
        int t = Q.front();
        Q.pop();
        if(t != 0)
            ans.push(t);
        if(tree[t].right != 0)
            Q.push(tree[t].right);
        if(tree[t].left != 0)
            Q.push(tree[t].left);
    }
    while(! ans.empty())
    {
        int t= ans.front();
        ans.pop();
        if(ans.empty())
            cout<<t<<endl;
        else cout<<t<<" ";
    }
}
int main()
{
   while(~scanf("%d",&n))
   {
       memset(tree,0,sizeof(tree));

      for(int i=0;i<n;i++)
            scanf("%d",&in[i]);
      for(int i=0;i<n;i++)
        scanf("%d",&pre[i]);
      Build(0,n,0,n);
      bfs();
   }
   return 0;
}


树的遍历


已知先序和中序,输出后序;

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100000 + 10;

struct Tree
{
    char left;
    char  right;
};
Tree tree[maxn];
char pre[maxn];//先序
char in[maxn];//中序
int len;
char Build(int pre_low,int pre_high,int in_low,int in_high)
{
    if(in_low >= in_high)
        return 0;
    char root = pre[pre_low];
    int pos = in_low;
    while( in[pos] != root)
    {
        pos ++;
    }
    int Count = pos - in_low;
    tree[root].left = Build(pre_low+1 ,pre_low+Count+1,in_low,pos);
    tree[root].right = Build(pre_low+Count+1,pre_high,pos+1,in_high);
    return root;
}
void print(char root)
{
    if(root == 0)
    return ;
    print(tree[root].left);
    print(tree[root].right);
    cout<<root;
}
int main()
{
   while(~ scanf("%s",pre))
   {
       scanf("%s",in);
       memset(tree,0,sizeof(tree));
       len = strlen(pre);
       Build(0,len,0,len);
       print(pre[0]);
       cout<<endl;
   }
   return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值