求二叉树后序遍历的循环算法

torresg 2003-12-11 10:23:39
ATT.
...全文
177 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZhangYv 2003-12-12
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2529/2529031.xml?temp=6.710231E-03
torresg 2003-12-12
  • 打赏
  • 举报
回复
楼上的兄弟,二叉树的遍历里只有递归算法和中序非递归算法才是基础,而后序遍历的非递归算法几乎都是不做要求的。我读书的时候这个问题就没有想出来,现在又想到这个问题,十分想把它弄懂!
19830711 2003-12-12
  • 打赏
  • 举报
回复

看书吧

这些基础性的东西书上都有呀
torresg 2003-12-12
  • 打赏
  • 举报
回复
还有就是我只需要算法,那些结构定义和stack的操作都可以不写,这样看起来清爽一点。谢谢!
torresg 2003-12-12
  • 打赏
  • 举报
回复
感谢layman2008和polly_110,递归算法我不需要,不过还是谢谢。abitz,你的访问语句放在哪里呢?
polly_110 2003-12-12
  • 打赏
  • 举报
回复
用递归后序遍历二叉树,
输入时按前序输入:
#include <stdio.h>
typedef struct node{
char data;
struct node *lchild, *rchild;
}*tree, bitnode;

tree create()
{
tree T;
char c;
printf("\nPlease input the data:");
scanf("%c",&c); getchar();

if(c=='#')
T=NULL;
else{
T=(tree)malloc(sizeof(bitnode));
T->data=c;
t->lchild=create();
t->rchild=create();
return (T);
}
}

main()
{
tree T;
T=create();
preoder(T);
}

preoder(bitnode *root)
{
if(root!=NULL){
preoder(root->lchild);
preoder(root->rchild);
printf("%4c",root->data);
}
}
这是我上学期做的一个题目,输入时按前序输入,如果某个结点没有孩子,则应以"#"做为它的孩子.
torresg 2003-12-12
  • 打赏
  • 举报
回复
谢谢ZhangYv和abitz。章鱼给的地址很好,收藏。
cutestar 2003-12-12
  • 打赏
  • 举报
回复
如果不用递归做后序遍历,难度有点大。
abitz 2003-12-12
  • 打赏
  • 举报
回复
void npostorder(BTree T,char mean) /*非递归后序遍历*/
{
BTree p;
STACK S;
int position=15;
enum flag flag;
S=INITSTACK();
do{
while(T!=NULL){
PUSH(T,S);
T=T->lchild;
} /*指向最左节点*/
p=NULL;
flag=Left;
while(!EMPTY(S)&&(flag==Left)){
T=TOP(S);
if(T->rchild==p){
visit(T); //here
POP(S);
p=T;
}
else{
T=T->rchild;
flag=Right;
}
}
}while(!EMPTY(S));
}
abitz 2003-12-11
  • 打赏
  • 举报
回复
#include <ctype.h>
#include <math.h>
#define MAXLEN 100 /* 定义栈的最大长度 */
#define NULL 0
#define OK 1
#define ERROR 0 /*定义出错信息 */

enum flag{Left,Right};

struct node{
char data;
struct node *lchild; /*指向左子树*/
struct node *rchild; /*指向右子树*/
};
typedef struct node *BTree;

typedef int Status;
typedef BTree elementtype;

enum boolean{FALSE,TRUE};

/* 一个简易栈 */

struct s{
int top;
elementtype element[MAXLEN];
}; /*定义栈的节点 */
typedef struct s *Stack;

Stack InitStack() /*建立栈*/
{
Stack p;
p=(Stack)malloc(sizeof(struct s));
p->top=0;
return(p);
}


enum boolean Empty(Stack s)
{
if(s->top==0)
return(TRUE);
return(FALSE);
} /*Empty*/


Status Push(elementtype x,Stack s)
{
if(s->top<MAXLEN){
s->top++;
s->element[s->top]=x;
return OK;
}
else return ERROR;
} /*Push*/


Status Pop(Stack s)
{
if(!Empty(s)){
s->top--;
return OK;
}
else return ERROR;
} /*pop*/


elementtype Top(Stack s)
{
if(!Empty(s))
return s->element[s->top];
else return NULL;
} /*Top*/

void DestroyStack(Stack s)
{
free(s);
}


void npostorder(BTree T,char mean) /*非递归后序遍历*/
{
BTree p;
Stack s;
enum flag flag;
s=InitStack();
do{
while(T!=NULL){
Push(T,s);
T=T->lchild;
} /*指向最左节点*/
p=NULL;
flag=Left;
while(!Empty(s)&&(flag==Left)){
T=Top(s);
if(T->rchild==p){
Pop(s);
p=T;
}
else{
T=T->rchild;
flag=Right;
}
}
}while(!Empty(s));
DestroyStack(Stack s);

}
layman2008 2003-12-11
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<string.h>
#include<iostream>

using namespace std;
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;


void CreateBiTree(BiTree &T) {

char ch;
cin >> ch;
if(ch=='#') T=NULL;
else {
T=new BiTNode;
T -> data=ch;
CreateBiTree(T ->lchild);
CreateBiTree(T ->rchild);
}
}

void PrintTree(BiTree &T) {

PrintTree(T ->lchild);
PrintTree(T ->rchild);
if (T==NULL) return; else cout<<T->data<<"\n";
}



int main() {
BiTree T;
CreateBiTree(T);
PrintTree(T);
system("PAUSE");
return(0);
}

69,373

社区成员

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

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