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

二叉树的非递归遍历请高手检查错误

qq8801103 发布于 2010-05-12 11:53, 1053 次点击
#include <stdio.h>
#define STACKINITSIZE 100
#define STACK 10
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef struct
{
   char *base;
   char *top;
   int stacksize;
}Sqstack;
typedef struct BiTNode
{
  char data;
  struct BiTNode  *lchild,*rchild;
}BiTNode,*BiTree;
char Initstack(Sqstack *S)
{
   S->base=(char *)malloc(STACKINITSIZE*sizeof(char));
   if(!S->base) exit(OVERFLOW);
   S->top=S->base;
   S->stacksize=STACKINITSIZE;
   return OK;
}
char GetTop(Sqstack S,char *e)
{
   if(S.top==S.base)  return ERROR;
   *e=*S.top-1;
   return *e;
}
char Push(Sqstack *S,char *e)
{
  if(S->top-S->base>=S->stacksize)
  {
    S->base=(char *)realloc(S->base,(S->stacksize+STACK)*sizeof(char));
    if(!S->base) exit(OVERFLOW);
    S->top=S->base+S->stacksize;
    S->stacksize+=STACK;
  }
  *S->top++=*e;
  return *e;
}
char Pop(Sqstack *S,char *e)
{
   if(S->top==S->base) return ERROR;
   *e=*--S->top;
   return *e;
}
print(char e)
{
   printf("%c",e);
   return 1;
}
char Inorder(BiTree T,int(* Visit)(char e))
{
  Sqstack S;BiTree p;
  Initstack(&S);   Push(&S,T);
  while(!stackempty(&S))
  {
     while(GetTop(S,p)&&p) Push(&S,p->lchild);
     Pop(&S,p);
     if(!stackempty(&S))
     {
    Pop(&S,p);
    if(!Visit(p->data)) return ERROR;
    Push(&S,p->rchild);
     }
  }
}
stackempty(Sqstack *S)
{
  if(S->top==S->base) return 1;
  else
    return 0;
}
char CreateBiTree(BiTree T)
{
  char ch;
  ch=getchar();
  if(ch=='#') T=NULL;
  else
  {
    if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
    T->data=ch;
    CreateBiTree(T->lchild);
    CreateBiTree(T->rchild);
  }
  return OK;
}
main()
{
BiTree S1;
CreateBiTree(&S1);
Inorder(&S1,print);
}
9 回复
#2
hzh5122010-05-12 14:21
你的程序很奇怪,让我很不明白的是 栈指针怎么会是char*类型,让我很不解,也无法给你改动。
因为一动数据结构,你的整个程序都要动,还不如另写一个呢!
#3
qq88011032010-05-13 22:17
那就请你给我写一个  谢谢
#4
鬼鬼千年2010-05-13 23:17
我写了一小半了,可是要停电了,明天接着写
#5
2010-05-14 10:10
其实栈就是一个比较特殊的数组,楼主何必写得那么麻烦……
#6
2010-05-14 10:11
其实栈就是一个比较特殊的数组,楼主何必写得那么麻烦……
#7
鬼鬼千年2010-05-14 13:16
声明:只是很粗的改了一遍,编译通过了,还没有调试
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define SIZE 20
#define STACK 10
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define elemtype   char
typedef struct
{
   elemtype data[SIZE];
   int top;
}Sqstack;

typedef struct BiTNode
{
  char data;
  struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

void  Initstack(Sqstack &s)//初始化堆栈
{
   
   s.top=-1;
}

char GetTop(Sqstack s,elemtype e)
{
   if(s.top==-1)  return ERROR;
   else
   {
       e=s.data[s.top];
       return e;
   }
}


void push(Sqstack s,elemtype e)
{
  if(s.top==SIZE-1)
       printf("zhan man");
  else
  {
      s.top++;
      s.data[s.top]=e;
  }
   
}
char pop(Sqstack s,elemtype &e)
{
   if(s.top==-1)  return ERROR;
   else
   {
       e=s.data[s.top];
       s.top--;
       return e;
   }
}
 void print(char e)
{
   printf("%c",e);
  
}
 
 int stackempty(Sqstack s)
{
  if(s.top==-1) return 1;
  else
    return 0;
}
void Inorder(BiTree T)
{
  Sqstack s;
   BiTree T1;
   T1=T;
   char p;
  p=T1->data;
  Initstack(s);   
  push(s,T1->data);
  while(T1||!stackempty(s))
  {
      if(T1)
      {
          push(s,T1->data);
          T1=T1->lchild;
      }
      else
      {
          pop(s,T1->data);
          print(T1->data);
          T1=T1->rchild;
      }
   
   }
  
}

char CreateBiTree(BiTree T)
{
  char ch;
  ch=getchar();
  if(ch=='#') T=NULL;
  else
  {
    if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
    T->data=ch;
    CreateBiTree(T->lchild);
    CreateBiTree(T->rchild);
  }
  return OK;
}
  int main()
{
BiTree S1;
CreateBiTree(S1);
Inorder(S1);
return 0;

}
#8
qq88011032010-05-14 19:24
7楼的我怎么编译不通过呢  能否用c  写个  写谢谢
#9
xichong2010-05-14 19:32
#define STACK_INIT_SIZE 100
#define STACKINCREMENT  10
#include <stdio.h>
#include <stdlib.h>
typedef char TElemType;

//////////////////////////////////////////////栈定义
typedef struct BiTNode
{
    TElemType data;
    struct BiTNode *lchild,*rchild;//
}BiTNode,*BiTree;
typedef struct
{
    BiTree *base;
    BiTree *top;
    int stacksize;
}SqStack;


//////////////////////////////////////////////队列定义
typedef struct QNode
{
    BiTree data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;


/////////////////////////////////////////////栈的基本操作
void initialStack(SqStack *s)
{
    s->base=(BiTree *)malloc(STACK_INIT_SIZE*sizeof(BiTree));
    if(!s->base) exit(0);
    s->top=s->base;
    s->stacksize=STACK_INIT_SIZE;
}
void Push(SqStack *s,BiTNode *e)
{
    if(s->top-s->base>=s->stacksize)
    {
        s->base=(BiTree *)realloc(s->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(BiTree));
        if(!s->base) exit(0);
        s->top=s->base+s->stacksize;
        s->stacksize+=STACKINCREMENT;
    }
    *(s->top)++=e;
}
void Pop(SqStack *s,BiTree *e)//////////////////
{
    if(s->top==s->base) exit(0);
    *e=*--(s->top);//
}
int GetTop(SqStack *s,BiTree *e)///////////////////
{
    if(s->top==s->base) return(0);
    *e=*(s->top-1);//
    return(1);
}
int StackEmpty(SqStack *s)
{
    if(s->base==s->top)
        return(1);
    else
        return(0);
}

BiTree CreatBiTree()//先序建立二叉链表
{
    TElemType ch;
    BiTree T;
    scanf("%c",&ch);
    if(ch=='@')
        T=NULL;
    else
    {
        T=(BiTree)malloc(sizeof(BiTNode));
        if(!T)
            exit(0);
        T->data=ch;
        T->lchild=CreatBiTree();
        T->rchild=CreatBiTree();
    }
    return T;//返回结点(T->data,T->lchild,T->rchild三个值已经确定)的指针的值
}

void Visit(BiTree p)//对数据元素进行操作的Visit函数
{
    printf("%c",p->data);
}
void InOrderTraverse(BiTree T)
{
    SqStack S;
    BiTree p;
    initialStack(&S);
    p=T;
    while(p||!StackEmpty(&S))
    {
        if(p)//根指针进栈,遍历左子树
        {
            Push(&S,p);
            p=p->lchild;
        }
        else//根指针退栈,访问根节点,遍历右子树
        {
            Pop(&S,&p);//Pop(&S,p) is incomptible
            Visit(p);
            p=p->rchild;
        }
    }
}

void AfterTraverse(BiTree T)//后序遍历
{
    SqStack S;
    BiTree p;
    initialStack(&S);
    do
    {
        while(T)//从左走到尽
        {
            Push(&S,T);
            T=T->lchild;
        }
        p=NULL;
        while(!StackEmpty(&S))
        {
            GetTop(&S,&T);
            if(T->rchild==p)//该结点的//右儿子\\为*空*或者*已经访问过*的结点,则访问根结点
            {
                Pop(&S,&T);
                Visit(T);
                p=T;//key point
            }
            else//否则,不要取出根结点,把它的右儿子的值找到,访问右子树
            {
                T=T->rchild;
                break;//退出内循环,进入下一层外循环
            }
        }
    }
    while(!StackEmpty(&S));
}
/////////////////////////////////
void main()
{
    BiTree t;
    printf("请按先序遍历序列输入二叉树中各个结点的值(字符),若为空树时输入空格键:\n");
    printf("<参考数据为:abc@@de@g@@f@@@,输完后再输入一个回车键.>\n");
    t=CreatBiTree();
    printf("[中序遍历]:\n");
    InOrderTraverse(t);
    printf("\n");
}
#10
2010-05-15 21:49
中序遍历:
#include <stdio.h>
#include <malloc.h>
#define maxsize 100
typedef char ElemType;
typedef struct node
{
    ElemType data;
    struct node *lchild,*rchild;
}BiNode,*BiTree;
BiTree CreatBiTree()
{
    BiTree T;
    ElemType x;
    scanf("%c",&x);
    if(x=='#') T=NULL;
    else
    {
        T=(BiTree)malloc(sizeof(BiNode));
        T->data=x;
        T->lchild=CreatBiTree();
        T->rchild=CreatBiTree();
    }
    return T;
}
void InOrder(BiTree T)
{
    BiTree array[maxsize];
    int j;
    BiTree p;
    j=0;
    while(j>0||p)
    {
        while(p)
        {
            array[++j]=p;
            p=p->lchild;
        }
        if(j>0)
        {
            p=array[j];j--;
            printf("%c ",p->data);
            p=p->rchild;
        }
    }
}
void main()
{
    BiTree T;
    T=CreatBiTree();
    InOrder(T);
}

先序遍历(进栈次数少的 ):
#include <stdio.h>
#include <malloc.h>
#define maxsize 100
typedef char ElemType;
typedef struct node
{
    ElemType data;
    struct node *lchild,*rchild;
}BiNode,*BiTree;
BiTree CreatBiTree()
{
    BiTree T;
    ElemType x;
    scanf("%c",&x);
    if(x=='#') T=NULL;
    else
    {
        T=(BiTree)malloc(sizeof(BiNode));
        T->data=x;
        T->lchild=CreatBiTree();
        T->rchild=CreatBiTree();
    }
    return T;
}
void Fast_Preorder(BiTree T)
{
    BiTree array[maxsize];
    BiTree p;int i;
    p=T;i=0;
    while(i>0||p)
    {
        while(p)
        {
            printf("%c ",p->data);
            if(p->rchild) array[++i]=p->rchild;
            p=p->lchild;
        }
        if(i>0) { p=array[i];i--; }
    }
}
void main()
{
    BiTree T;
    T=CreatBiTree();
    Fast_Preorder(T);
}


1