注册 登录
编程论坛 数据结构与算法

递归算法交换左右子树显示有错误,求大神指点

无关一身轻 发布于 2013-11-28 19:19, 633 次点击
#include "stdio.h"
#include "stdlib.h"
#define maxsize 8
typedef struct node{
    char data;
    struct node *lchild,*rchild;
}BiTree;

BiTree *CREATREE()
{
    BiTree*Q[maxsize];
    int front=0,rear=0;
    char ch;
    BiTree *s,*root=NULL;
    while((ch=getchar())!='\n')
    {
        s=NULL;
        if(ch!='#')
        {
            s=(BiTree *)malloc(sizeof(BiTree));
            s->data=ch;
            s->lchild=s->rchild=NULL;
        }
        Q[++rear]=s;

        if(rear==1)
            root=s;
        else
        {
            if(Q[rear]&&Q[front])
                if(!(rear%2))
                    Q[front]->lchild=s;
                else
                    Q[front]->rchild=s;
        }
        if(rear%2==1) front++;
    }
    return root;
}

void SWAP(BiTree*t)
{   
    BiTree *temp = NULL;
    temp=t->lchild;
    t->lchild=t->rchild;
    t->rchild=temp;
    if(t->lchild)
    SWAP(t->lchild);
    if(t->rchild)
    SWAP(t->rchild);
}

void main()
{
    BiTree *T;
    T=BiTree *CREATREE();
    SWAP(T);
}


--------------------Configuration: SWAP - Win32 Debug--------------------
Compiling...
SWAP.CPP
e:\学习\microsoft visual studio\myprojects\swap交换左右子树\swap.cpp(56) : error C2275: 'BiTree' : illegal use of this type as an expression
        e:\学习\microsoft visual studio\myprojects\swap交换左右子树\swap.cpp(7) : see declaration of 'BiTree'
执行 cl.exe 时出错.

SWAP.exe - 1 error(s), 0 warning(s)
2 回复
#2
何时是月明2013-11-29 17:29
建议你先看看*CREATREE()当调用的时候是否可以输出构造的树
#3
无关一身轻2013-11-30 10:24
回复 2楼 何时是月明
??不是很懂!麻烦详细说一下
1