# 帮我看一下非递归遍历二叉数的算法

hanzq 2004-05-07 08:04:47
struct btnode
{ char data;
struct btnode * lchild;
struct btonde * rchild;
};
struct node
{ struct btnode *addr;
struct node *link;
};
inorder(root)
struct btnode * root;
{
struct node *top,*p;
top=NULL;
while(root!=NULL||top!=NULL)
{
while(root!=NULL)
{
struct node * p=(struct node *)malloc(sizeof(struct node));
p->addr=root;
p->link=top;
top=p;
root=root->lchild;
}
if(top!=NULL)
{
root=top->addr;
printf("%c",root->data);
p=top;
top=top->link;
free(p);
root=root->rchild;
}
}return;
}

...全文
75 5 点赞 打赏 收藏 举报

5 条回复

hewittlee 2004-05-08
struct btonde * rchild; //小心点
• 打赏
• 举报

junnyfeng 2004-05-07
struct btnode
{ char data;
struct btnode * lchild;
struct btonde * rchild; //这里btnode写错了
};

• 打赏
• 举报

cngdzhang 2004-05-07

#include <stdio.h>
#include <stdlib.h>

struct btnode
{ char data;
struct btnode * lchild;
struct btnode * rchild;
};
struct node
{ struct btnode *addr;
struct node *link;
};

void inorder(struct btnode *root)
{
struct node *top,*p;
top=NULL;
while(root!=NULL||top!=NULL)
{
while(root!=NULL)
{
struct node * p=(struct node *)malloc(sizeof(struct node));
p->addr=root;
p->link=top;
top=p;
root=root->lchild;
}
if(top!=NULL)
{
root=top->addr;
printf("%c",root->data);
p=top;
top=top->link;
free(p);
root=root->rchild;
}
}return;
}

void main()
{
}
• 打赏
• 举报

zhouqingyuan 2004-05-07

• 打赏
• 举报

freefalcon 2004-05-07

struct btnode
{ char data;
struct btnode * lchild;
struct btnode * rchild; //btnode
};
• 打赏
• 举报

C语言

6.5w+

C语言相关问题讨论

2004-05-07 08:04