7-1 树的遍历 (25 分)

在这里插入图片描述
欲哭无泪,我可能选择了最难走的一条路,我突发奇想,使用二维坐标的形式在电脑中描绘一棵树,使用结构体数组,结构体是{结点,x,y},摸索了很长时间,写是写出来了,但是忒麻烦了,光是处理后序、前序整理成我想要的树就写了150行代码,要了老命了,最后所有样例也只通过了一半,哭死,就先这样吧。

#include <stdio.h>
#include <math.h>

#define peerless -1000

int pot=0;


typedef struct Tree
{
    int x;
    int y;
    int node;
};

struct Tree tree[36];

void deal_tree(struct Tree *tree,int *pos_array,int *mid_array,int N);
void find_root(int root_pot,int num,struct Tree *tree,int N,int *pos_array,int root_loc,int *mid_array,int i,int j,int left,int right,int level);
void print_seq_tree(struct Tree *tree,int N);

int main()
{
    int N;
    int pos_array[36]={0};//后续遍历
    int mid_array[36]={0};//中序遍历
    int i = 0 ;

    scanf("%d",&N);
    while(i<N)
    {
        scanf("%d",&pos_array[i]);
        i++;
    }
    i=0;
    while(i<N)
    {
        scanf("%d",&mid_array[i]);
        i++;
    }

    deal_tree(tree,pos_array,mid_array,N);

    print_seq_tree(tree,N);
    return 0;
}


void print_seq_tree(struct Tree *tree,int N)
{
    int i=0,j=0;
    int x=0,y=0,node=0;
    while(i<N)
    {
        j=i+1;
        while(j<N)
        {
            if(tree[i].y<tree[j].y)
            {
                x=tree[i].x;
                y=tree[i].y;
                node=tree[i].node;

                tree[i].x=tree[j].x;
                tree[i].y=tree[j].y;
                tree[i].node=tree[j].node;

                tree[j].x=x;
                tree[j].y=y;
                tree[j].node=node;

            }
            j++;
        }
        i++;
    }

    i=0;
    j=0;
    while(i<N)
    {
        j=i+1;
        while(j<N)
        {
            if(tree[i].y==tree[j].y && tree[i].x>tree[j].x)
            {
                x=tree[i].x;
                y=tree[i].y;
                node=tree[i].node;

                tree[i].x=tree[j].x;
                tree[i].y=tree[j].y;
                tree[i].node=tree[j].node;

                tree[j].x=x;
                tree[j].y=y;
                tree[j].node=node;

            }
            j++;
        }
        i++;
    }

    i=0;
    while(i<N)
    {
        if(i==N-1)
        {
            printf("%d\n",tree[i].node);
            break;
        }
        printf("%d ",tree[i].node);
        i++;
    }
}

void deal_tree(struct Tree *tree,int *pos_array,int *mid_array,int N)
{
    int i=0,j=0;
    j=N-1;
    find_root(0,N,tree,N,pos_array,N-1,mid_array,i,j,0,0,0);
    //mid_array[i]即根
}

//使用(x,y)二维平面坐标的方式存储结点相对位置,root是(0,0)其左子树根(-1,-1),其右子树根(1,-1)
//(当前根的tree地址,当前检测的字串的个数,保存树的结构体数组,后序遍历序列,后序遍历序列中的根地址,中序遍历序列,中序遍历序列中待检测字串的起点,~终点,要检测的是左子树,要检测的是右子树,当前深度)
void find_root(int root_pot,int num,struct Tree *tree,int N,int *pos_array,int root_loc,int *mid_array,int i,int j,int left,int right,int level)
{
    int u=0;
    if(left==1)
    {
        u=i;
        //找根
        while(i<=j)
        {
            if(pos_array[root_loc]==mid_array[i])
            {
                break;
            }
            i++;
        }
        //存储该根结点
        tree[pot].node=mid_array[i];
        tree[pot].x=tree[root_pot].x-1;
        tree[pot].y=level;
        root_pot=pot;
        pot++;
        level--;
        if(num==1)//若只有num=1说明这是最后一个结点了,该结束了
        {
            u++;
        }
        else if(i==u)//检测到中序序列中的根地址刚好是检测字串的起点,说明该根结点只有右子树,则检测右子树
        {
            root_loc=j-1;
            num=j-u;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,u+1,j,0,1,level);
        }
        else if(i==j)//检测到中序序列中的根地址刚好是检测字串的终点,说明该根结点只有左子树,则检测左子树
        {
            root_loc=j-1;
            num=j-u;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,u,j-1,1,0,level);
        }
        else//否则该根左右都有则进行两次检测
        {
            //先左
            root_loc=i-1;
            num=i-u;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,u,i-1,1,0,level);
            //后右
            root_loc=j-1;
            num=j-i;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,i+1,j,0,1,level);
        }

    }
    else if(right==1)
    {
        u=i;
        while(i<=j)
        {
            if(pos_array[root_loc]==mid_array[i])
            {
                break;
            }
            i++;
        }
        tree[pot].node=mid_array[i];
        tree[pot].x=tree[root_pot].x+1;
        tree[pot].y=level;
        root_pot=pot;
        pot++;
        level--;
        if(num==1)
        {
            u++;
        }
        else if(i==u)
        {
            root_loc=j-1;
            num=j-u;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,u+1,j,0,1,level);
        }
        else if(i==j)
        {
            //因为后序与中序存在错位问题,所以有的-1有的-2
            root_loc=j-2;
            num=j-u;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,u,j-1,1,0,level);
        }
        else
        {
            root_loc=i-2;
            num=i-u;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,u,i-1,1,0,level);
            root_loc=j-2;
            num=j-i;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,i+1,j,0,1,level);
        }

    }
    else if(left==0 && right==0)//这里处理整个树的根
    {
        u=i;
        while(i<=j)
        {
            if(pos_array[root_loc]==mid_array[i])
            {
                break;
            }
            i++;
        }
        tree[pot].node=mid_array[i];
        tree[pot].x=0;
        tree[pot].y=level;
        root_pot=pot;
        pot++;
        level--;
        if(num==1)
        {
            u++;
        }
        else if(i==u)
        {
            root_loc=j-1;
            num=j-u;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,u+1,j,0,1,level);
        }
        else if(i==j)
        {
            root_loc=j-1;
            num=j-u;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,u,j-1,1,0,level);
        }
        else
        {
            root_loc=i-1;
            num=i-u;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,u,i-1,1,0,level);
            root_loc=j-1;
            num=j-i;
            find_root(root_pot,num,tree,N,pos_array,root_loc,mid_array,i+1,j,0,1,level);
        }

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值