二叉树中序遍历非递归的问题 弄了好久都没解决 望高手指点

johnny-huang 2011-12-07 05:24:00

tree.h为:
#include<stdio.h>
#define MAX 50
typedef struct node
{

char data;
node * Lsubtree;
node * Rsubtree;
}node ,*Tnode;

typedef node Elemtype;
typedef struct stack
{
Elemtype data[MAX];
int top;
} Stack;
void CreateTree(Tnode *tree )
{

char ch;
scanf("%c",&ch);
if(ch=='\n')
{
return ;
getchar();
}

else if(ch=='.')
{
*tree=NULL;

}
else
{
*tree=(node *)malloc(sizeof(node ));
(*tree)->data=ch;
CreateTree(&(*tree)->Lsubtree);
CreateTree(&(*tree)->Rsubtree);
}
return;
}

void PreOrder(Tnode tree)
{

if(tree!=NULL)
{
printf("%c",tree->data);
PreOrder(tree->Lsubtree);
PreOrder(tree->Rsubtree);
}


}


void InOrder(Tnode tree)
{

if(tree!=NULL)
{

InOrder(tree->Lsubtree);
printf("%c",tree->data);
InOrder(tree->Rsubtree);
}


}
void PostOrder(Tnode tree)
{

if(tree!=NULL)
{

PostOrder(tree->Lsubtree);
PostOrder(tree->Rsubtree);
printf("%c",tree->data);
}


}
void Access(char c)
{
printf("%c",c);
}

void InitStack(Stack *S)
{
S=(Stack *)malloc(sizeof(Stack));
S->top=-1;
}

bool EmptyStack(Stack * S)
{
if(S->top==-1)
return true;
else
return false;

}

void PushStack(Stack *S,Elemtype *tree)
{
if(S->top=MAX-1)
return;
S->top++;
S->data[S->top]=*tree;


}
void PoPStack(Stack *S,Elemtype *tree )
{
if(S->top==-1)
return;
*tree=S->data[S->top];
S->top--;

}

void N_INorder(Stack &S,Tnode &tree)
{


Tnode p=tree;
do
{

while(p!=NULL)
{

PushStack(S,p);
p=p->Lsubtree;

}

// if(S->top==-1)
// {
// printf("栈为空!!!\n");

// }
// else
// {
PoPStack(&S,p);
Access(p->data);
p=p->Rsubtree;
// }
}while(p!=NULL&&!(EmptyStack(&S)));
}

void Access(char c);
void CreateTree(Tnode *tree);
void PreOrder(Tnode tree);
void InOrder(Tnode tree);
void PostOrder(Tnode tree);
void PushStack(Stack *S,Elemtype tree);
void PoPStack(Stack *S,Elemtype *tree);
void N_INorder(Stack &S,Tnode &tree);
bool EmptyStack(Stack S);



///////////////////////////////////////////

主函数为:
#include<stdio.h>
#include<malloc.h>
#include"tree.h"

void main()
{
Tnode t;
Stack S;
InitStack(&S);
printf("创建一棵二叉树!\n");
printf("请输入要创建的二叉树('.'代表子树为空):");
CreateTree(&t);
printf("先根遍历递归算法得的二叉树为:\n");
PreOrder(t);
printf("\n");
printf("中根遍历递归算法得的二叉树为:\n");
InOrder( t);
printf("\n");
printf("后根遍历递归算法得的二叉树为:\n");
PostOrder(t);
printf("\n");
printf("非递归中根遍历算法的结果为:");
N_INorder(S,t);
printf("\n");
}




...全文
124 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
脱离语言 2012-07-05
  • 打赏
  • 举报
回复
已经给你说清楚了。第一个参数不能从结构体转为结构体指针,所以应该这么写PushStack(&S,p);
脱离语言 2012-07-05
  • 打赏
  • 举报
回复
已经给你说清楚了。第一个参数不能从结构体转为结构体指针,所以应该这么写PushStack(&S,p);
johnny-huang 2012-06-24
  • 打赏
  • 举报
回复
是的 呵呵呵 ,有劳啦!!!
脱离语言 2012-06-16
  • 打赏
  • 举报
回复
2011年的帖子啊...
脱离语言 2012-06-16
  • 打赏
  • 举报
回复
提示的很清楚了,cannot convert parameter 1 from 'struct stack' to 'struct stack *‘
第一个参数不能从结构体转为结构体指针,所以应该这么写PushStack(&S,p);

另外,main函数前面那些函数申明可以不写,因为main函数已经有那些函数定义的后面了。
johnny-huang 2011-12-07
  • 打赏
  • 举报
回复
补充错误为:
c:\users\administrator.yhwy-pc\desktop\二叉树\二叉树\tree.h(132) : error C2664: 'PushStack' : cannot convert parameter 1 from 'struct stack' to 'struct stack *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧