zoukankan      html  css  js  c++  java
  • 序列建树(递归)


    1、前序+中序—>

    这个天勤《高分笔记》树那章的真题仿造上就有

    核心代码如下:

    S->lchild=CreateBT(pre,in,l1+i,l1+i-l2,l2,i-1);

    S->rchild=CreateBT(per,in,l1+i-l2+1,r1,i+1,r2);

    例:    

    前序:1 3 5 2 4 6 7

    中序:5 3 2 1 4 6 7

                i

    要注意的是:一开始S->lchild中的前序尾部r1会等于iS->rchild中前序首部l1会等于i+1。但是不能这样带人递归,因为一开始l1==l2,一次递归后两者就可能不相等了。

    1086. Tree Traversals Again
    http://www.patest.cn/contests/pat-a-practise/1086
     

    #include <stdio.h>

    #include <string.h>

    struct Node

    {

       Node* lchild;

       Node* rchild;

       int val;

    }Tree[31];

    int loc;

    Node* create()

    {

    Tree[loc].lchild=Tree[loc].rchild=NULL;

    return &Tree[loc++];

    }

    Node* build(int pre[],int in[],int l1,int r1,int l2,int r2)

    {

          if(l1<=r1)

      {

      Node* T=create();

      int i=l2;

      while(pre[l1]!=in[i])

      i++;

      T->val=in[i];

      T->lchild=build(pre,in,l1+1,l1+i-l2,l2,i-1);

      T->rchild=build(pre,in,l1+i-l2+1,r1,i+1,r2);

      return T;

      }

      else return NULL;

    }

    void postorder(int post[],Node* T)

    {

        if(T!=NULL)

    {

    postorder(post,T->lchild);

    postorder(post,T->rchild);

    post[loc++]=T->val;

    }

    }

    int main()

    {

       int n;

       while(scanf("%d",&n)!=EOF)

       {

           

       //其实这题就是给出 先序 和 中序 求 后序

       //push 的次序就是 先序,pop出来的次序 就是 中序

       int pre[31];

       int in[31];

       int post[31];

       int Stack[31];

       int top=0,loc1=0,loc2=0;

       int i;char s[10];int temp;

       for(i=0;i<2*n;i++)

       {

       getchar();

          scanf("%s",s);

      if(strcmp(s,"Push")==0)

      {

                  scanf("%d",&temp);

         pre[loc1++]=temp;

     Stack[top++]=temp;

      }

      else

      in[loc2++]=Stack[--top];

       }

     

       //建树

       int l1=0,l2=0,r1=n-1,r2=n-1;

       loc=0;

       Node* T=build(pre,in,l1,r1,l2,r2);

       loc=0;

       postorder(post,T);

       for(i=0;i<n;i++)

       printf("%d%c",post[i],i==n-1?' ':' ');

       }

    }


      

    2、后序+中序—>

    浙大 Graduate Entrance Exam 2011-2 上有一题目,就是给出后序 和 中序 遍历,求 层次遍历。

    可以先根据后序和前序遍历,生成树,返回根节点,在进行层次遍历。

    Tree Traversals (25)

    http://www.patest.cn/contests/pat-a-practise/1020 

    #include <iostream>

    using namespace std;

    struct LNode

    {

      LNode *lchild,*rchild;

      int data;

    };

    int in[31];

    int pos[31];

    LNode* fun(int pos[],int f1,int r1,int in[],int f2,int r2)//建树

    {

    if(f1>r1)  return NULL;

    LNode *p=(LNode*)malloc(sizeof(LNode));

    p->lchild=NULL;

    p->rchild=NULL;

    p->data=pos[r1];

    int i;

    for(i=f2;i<=r2;i++)

    if(in[i]==pos[r1])  break;

    p->lchild=fun(pos,f1,f1+i-f2-1,in,f2,i-1);

    p->rchild=fun(pos,f1+i-f2,r1-1,in,i+1,r2);

         return p;

    }

    int main()

    {

    int n;

    while(cin>>n)

    {

    int i;

        for(i=0;i<n;i++)

    cin>>pos[i];

    for(i=0;i<n;i++)

    cin>>in[i];

    LNode *q=(LNode*)malloc(sizeof(LNode));

            q=fun(pos,0,n-1,in,0,n-1);

    LNode* que[40];  //层次遍历

    int front=0;int rear=0;

    rear++;

    que[rear]=q;

            bool fir=true;

    while(front!=rear)

    {

       front++;

       if(fir)

       {cout<<que[front]->data;fir=false;}

       else cout<<" "<<que[front]->data;

       if(que[front]->lchild!=NULL)

         que[++rear]=que[front]->lchild;

       if(que[front]->rchild!=NULL)

         que[++rear]=que[front]->rchild;  

    }

    cout<<endl;

    }

       return 0;

    }

  • 相关阅读:
    web10 动态action的应用
    web09 struts2配置 struts2入门
    web 08 struts2入门 struts2配置 struts包
    web07-jdbcBookStore
    web06-PanduanLogin
    web05-CounterServlet
    web04-LoginServlet
    web03-OutputInfo
    web02-welcomeyou
    web01-helloworld
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/4255602.html
Copyright © 2011-2022 走看看