课程设计4--二叉树遍历

// BitTraverse.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "BiTree.h"

void  main(int argc, char* argv[])
{
 bool isContinue = true ;//标志是否继续对另一二叉树进行遍历
 char key ; //标志用户的选择
 BiTree T;
    AboutAuthor();
 while(isContinue)
 {
  cout << "/n/n/t***************************二叉树的遍历*************************/n/n/n";
  T = CreateBiTree();
  PreOrderTraverse(T);
  cout << "/n";
  InOrderTraverse(T); 
  cout << "/n";
  PosTraverse(T);

  cout << "/n/t是否继续对另一二叉树进行遍历--是(Y),否(N):"<<endl;
  cout << "Select:";
  cin >> key;

  if(key == 'n'||key == 'N' )
  {
   isContinue = false;
  }
  else
  {//对BiTree.h中的标志进行初始化
   first = true;//第一次构造二叉树
   L_or_R  = true;//表示对左孩子操作
  }
 }
}

 #include <malloc.h>
#include <iostream.h>

#define OK 1
#define ERROR 0
#define NULL 0

#define INIT_SIZE 100 //存储空间初始分配量
#define INCREMENT 10  //存储空间分配增量

typedef struct BiTNode{
 int data;
 int tag;     //0:遍历左子树前的现场保护,1:遍历右子树前的现场保护
 struct BiTNode *lchild, *rchild;//左右孩子指针
}BiTNode, *BiTree;//二叉树的二叉链表存储表示

typedef struct{
 BiTree *top, *base; //栈顶指针和栈底指针
 int stacksize;   //当前已分配的存储空间,以元素为单位
}SqStack;


bool first = true;//是否第一次构造二叉树
bool L_or_R  = true;//表示左孩子还是有孩子

BiTree CreateBiTree( )
{ //按先序次序输入二叉树中结点的值
 char e;//用于记录用户的选择
 BiTree tmp ;
    int innum;//记录用户的输入值
 if(first)
  cout << "/n/t根节点为空则输入N,非空则输入Y:";
 else
  if(L_or_R)
   cout <<  "/t若左孩子为空则输入N,非空则输入Y:";
  else
   cout <<  "/t若右孩子为空则输入N,非空则输入Y:";
 cout << "/n/tSelect:";
 cin >> e;
 
 if( (e =='n')||(e =='N') )
 {
  tmp = NULL;
  L_or_R = false;
 }
 else
 {
  if(first)
  {
   cout << "/n/t输入根节点值(整形):/n/tfoot=:";
   first = false;
  }
  else
  {
   if(L_or_R)
    cout << "/n/t输入左孩子(整形):";
   else
   {
    cout << "/n/t输入右孩子(整形):"; 
    L_or_R = true;
   }   
  }
  cin >> innum;//innum全局静态变量
  
  tmp=(BiTree)malloc(sizeof(BiTNode));
  if(!tmp) return NULL;

  tmp->data=innum;

  tmp->lchild = CreateBiTree();
  tmp->rchild = CreateBiTree();
 }
 return tmp;
}


void AboutAuthor()
{
 cout << "/n/t/t/t/t/t/t/t/tBy:江江(宋保江)"<<endl;
 cout << "/t/t/t/t/t/t/t/tAdreess:计本043"<<endl;
 cout << "/t/t/t/t/t/t/t/t山东建筑大学"<<endl;
 cout << "/t/t/t/t/t/t/t/tQQ:282719081"<<endl;
 cout << "/t/t/t/t/t/t/t/tTel:13793141761";
}


int InitStack(SqStack *S) //创建一个空栈
{
 S->base=(BiTree *)malloc( INIT_SIZE * sizeof(BiTree) );
 if(!S->base) //空间分配失败
  return ERROR;
 //空间分配成功
 S->top=S->base;//置栈顶指针
 S->stacksize=INIT_SIZE;//栈大小
 return OK;
}

int Push(SqStack *S, BiTree T) //插入栈顶
{
 if( (int)(S->top - S->base) >= S->stacksize)
 {//栈满,追加存储空间
  S->base=(BiTree *)realloc(S->base, (S->stacksize+INCREMENT)*sizeof(BiTree) );
 if(!S->base)//分配失败,返回1
  return OK;
 //分配成功
 S->top = S->base + S->stacksize;//置栈顶指针
 S->stacksize += INCREMENT;//栈大小
 }
 *S->top++ = T;//接收输入后,S->top指向栈顶元素的下一个位置

 return ERROR;
}

int Pop (SqStack *S, BiTree &e)
{
     // 若栈不空,则删除S的栈顶元素,
     // 用 e 返回其值,并返回OK;
     // 否则返回ERROR
    if (S->top == S->base) return ERROR;
     e = *--S->top;
    return OK;
}

bool EmptyStack(SqStack S)
{
 return S.top==S.base;
}
void DestroyStack(SqStack S) {

 S.base = NULL;

}
int GetTop(SqStack &S,BiTree &p)
{//取栈顶
 if(S.top==S.base)return ERROR;
 p=*(S.top-1);
 return OK;
}


//中序遍历
bool InOrderTraverse(BiTree bt)
{
 SqStack S;
 BiTree p = bt;

 if( !InitStack(&S) )
  return ERROR;

 cout << "中序遍历结果:/n/t";
 while( p||(S.base!=S.top) ){
   if(p){
    if( Push(&S, p) )//根指针进栈,遍历左子树
   return OK;
    p = p->lchild;
   }
   //根指针退栈,访问根结点,遍历右子树
   else{
    Pop(&S,p);
    cout << "--->";  
    cout <<p->data;
    p = p->rchild;
   }
 }

 cout << "/n";
 DestroyStack(S);
 return ERROR;
}

//先序遍历
bool  PreOrderTraverse(BiTree t)
{
  SqStack s;
  BiTree p;
  if(!InitStack(&s))
   return ERROR;
  p=t;
  cout << "/n先序遍历结果:/n/t";
  while (p || !EmptyStack(s))
  {
    while (p!=NULL)         //遍历左子树
    {
        cout << "--->";  
     cout <<p->data;

        Push(&s,p);
        p=p->lchild;    
    }
   
    if (!EmptyStack(s))       //通过下一次循环中的内嵌while实现右子树遍历
    {
        Pop(&s,p);
        p=p->rchild;    
    }
         
  }
  cout << "/n";
  DestroyStack(s);
  return OK;
}
//后续遍历
bool PosTraverse(BiTree T)
{
 SqStack S;
 BiTree p;
 p=T;
    if(!InitStack(&S))
    return ERROR;
    cout << "/n后序遍历结果:/n/t";
    Push(&S,p);
    if(!p) return ERROR;
 while(!EmptyStack(S))
 {
  GetTop(S,p);
  while((p->lchild)&&(p->lchild->tag!=1))
  {
   p=p->lchild;
   Push(&S,p);
  }//找到结点最左孩子
   GetTop(S,p);
   if((p->rchild==NULL)||p->rchild->tag==1)
   {
    Pop(&S,p);
    cout << "--->";  
    cout <<p->data;
    p->tag=1;
   }//无右孩子或右孩子已访问则访问自身结点
   else
   {
    p=p->rchild;
    Push(&S,p);
   }//有右孩子则右孩子入栈
  }
    cout << "/n";
    DestroyStack(S);
 return OK;
}//后序非递归遍历

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值