二叉树(数据结构)

本文详细介绍了二叉树的创建、多种遍历方法(包括前序、中序、后序、层序及非递归中序遍历)以及二叉树深度的计算。通过具体的代码实现,展示了如何使用队列和栈进行遍历,为读者提供了深入理解二叉树结构及其操作的实践指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <malloc.h>
#define TElemType char
#define SElemType char
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define MAXQSIZE 100
typedef  char QElemType;
typedef  int Status;
typedef int Status;
using namespace std;
typedef struct BiTNode
{
     TElemType data;
     struct  BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct {
    BiTree *base;
    char front;
    char rear;
}SqQueue;
typedef struct{
    BiTree *base;
    BiTree *top;
    int stacksize;
}SqStack;
Status InitQueue (SqQueue &Q)
{//构造一个空队列
    Q.base =new BiTree[MAXQSIZE];
    Q.front=Q.rear=0;
    return true;
}
Status EnQueue(SqQueue &Q,BiTree e)
{
     if((Q.rear+1)%MAXQSIZE==Q.front)  return false;
    Q.base[Q.rear]=e;
    Q.rear = (Q.rear+1) % MAXQSIZE;
     return true;
}
Status DeQueue (SqQueue &Q,BiTree &e)
{
   if(Q.front==Q.rear) return false;
   e=Q.base[Q.front];
   Q.front=(Q.front+1) % MAXQSIZE;
   return true;
}

Status InitStack( SqStack &S )
{
    S.base=new BiTree[STACK_INIT_SIZE];
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return true;
}
bool StackEmpty( SqStack S )
{
    if(S.base==S.top)
        return true;
    return false;
}
Status  Pop ( SqStack  &S, BiTree & e)
 {
     if ( S.top == S.base )   return false;
     e=*--S.top;
     return true;
}//Pop
Status GetTop( SqStack S, BiTree &e)
{	// 若栈不空,则用e返回S的栈顶元素

	if( S.top == S.base )
        return false;
 	e=*(S.top - 1 );
	return true;
}
Status  PushS ( SqStack  &S, BiTree  e)
{   //插入元素e为新的栈顶元素
     if ( S.top-S.base==S.stacksize ) {
               S.base=(BiTree*) realloc ( S.base,
                       (S.stacksize+STACKINCREMENT)*sizeof(SElemType));
           if (!S.base)exit(10);
           S.top=S.base + S.stacksize;
           S.stacksize+=STACKINCREMENT;
     }
     *S.top++ = e;
     return true;
}//Pus
int PreOrderTraverse(BiTree T){
  if(T==NULL) return true; //空二叉树
  else{
     cout<<T->data; //访问根结点
     PreOrderTraverse(T->lchild); //递归遍历左子树
     PreOrderTraverse(T->rchild); //递归遍历右子树
    }
}
void show_aim()
{
    cout<<"1.创建链式二叉树"<<endl;
    cout<<"2.前序遍历二叉树"<<endl;
    cout<<"3.中序遍历二叉树"<<endl;
    cout<<"4.后续遍历二叉树"<<endl;
    cout<<"5.层序遍历二叉树"<<endl;
    cout<<"6.二叉树的深度--"<<endl;
    cout<<"7.非递归中序遍历"<<endl;
    cout<<"操作错误"<<endl;
}
int Depth(BiTree T)
{
    int m,n;
   if(T==NULL)  return 0;
   else
    {
     m=Depth(T->lchild); n=Depth(T->rchild);
      if (m>=n)  return m+1;
      else   return n+1;
      }
}
void CreateBiTree(BiTree &T){
//按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
   char ch;
   cin >> ch;
   if(ch=='#')  T=NULL; //递归结束,建空树
   else
     {
          T=new BiTNode;
          T->data=ch;	//生成根结点
          CreateBiTree(T->lchild);	//递归创建左子树
          CreateBiTree(T->rchild);	//递归创建右子树
      }
}
Status InOrderTraverse(BiTree T){
  if(T==NULL) return true; //空二叉树
  else{
     InOrderTraverse(T->lchild); //递归遍历左子树
     cout<<T->data; //访问根结点
     InOrderTraverse(T->rchild); //递归遍历右子树
    }
}
Status PostOrderTraverse(BiTree T){
  if(T==NULL) return true; //空二叉树
  else{
     PostOrderTraverse(T->lchild); //递归遍历左子树
     PostOrderTraverse(T->rchild); //递归遍历右子树
     cout<<T->data; //访问根结点
    }
}
Status FInOrderTraverse (BiTree T)
{
    SqStack S;
    BiTree p;
     InitStack(S);PushS(S,T);   // 根指针进栈
     while ( !StackEmpty(S) )
    {
         while( GetTop(S, p) && p)    PushS( S, p->lchild );   // 向左走到尽头
          Pop( S, p ); // 空指针退栈
          if ( !StackEmpty(S) )
         {     // 访问结点,向右一步
             Pop( S, p );
             cout<<p->data;
             PushS ( S, p->rchild );   // 将右儿子入栈,则下次循环时打印右儿子
          } // if
     } //while
    return  true;
}
int EmQueue(SqQueue &Q){
    if(Q.front==Q.rear){
        return true;
    }
    return false;
}
void TevalTraverse(BiTree T){
        SqQueue Q;
        InitQueue(Q);
        if(T){
            EnQueue(Q,T);
            while(EmQueue(Q)==false){
                DeQueue(Q,T);
                cout<<T->data;
                if(T->lchild!=NULL)
                    EnQueue(Q,T->lchild);
                if(T->rchild!=NULL)
                    EnQueue(Q,T->rchild);
            }
        }
}
int main()
{
    show_aim();
    BiTNode* T;
    int oper;
    cout<<"请输入操作"<<endl;
    while(1){
    cin>>oper;
    if(oper==1)
    CreateBiTree(T);
    else if(oper==2){
        PreOrderTraverse(T);
        cout<<endl;
        }
    else if(oper==3){
        InOrderTraverse(T);
        cout<<endl;
        }
    else if(oper==4){
        PostOrderTraverse(T);
        cout<<endl;
        }
    else if(oper==5){
        TevalTraverse(T);
        cout<<endl;
        }
    else if(oper==6){
        cout<<Depth(T)<<endl;
    }
    else if(oper==7){
        FInOrderTraverse (T);
        cout<<endl;
    }
    }
    return 0;
}

 

本题的函数接口为: ```c++ Status push(Sqstack &S, SElemType x); // x 入栈 S Status pop(Sqstack &S, SElemType &e); // 从 S 栈出栈1次元素放入 e Status Compare(char s[]); // s 为表达式 ``` 函数 `Compare()` 用于比较给定的表达式 `s` 是否合法。函数中使用了栈 `S` 来辅助判断。在遍历表达式 `s` 的过程中,如果遇到左括号(包括圆括号、方括号和花括号),则将其入栈。如果遇到右括号,则将栈顶元素出栈,并与该右括号进行匹配。如果匹配成功,则继续遍历;否则,表达式不合法,返回 `FALSE`。遍历结束后,如果栈为空,则表达式合法,返回 `TRUE`;否则,表达式不合法,返回 `FALSE`。 以下是完整代码实现: ```c++ #define MAXSIZE 100 typedef char SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; } Sqstack; typedef enum { ERROR, TRUE, FALSE } Status; Status initStack(Sqstack &S) { S.base = new SElemType[MAXSIZE]; if (!S.base) return ERROR; S.top = S.base; S.stacksize = MAXSIZE; return TRUE; } Status push(Sqstack &S, SElemType x) { if (S.top - S.base == S.stacksize) return ERROR; *(S.top++) = x; return TRUE; } Status pop(Sqstack &S, SElemType &e) { if (S.top == S.base) return ERROR; e = *(--S.top); return TRUE; } Status Compare(char s[]) { Sqstack S; SElemType e; Status flag = TRUE; int i = 0; initStack(S); while (s[i] != '#' && flag == TRUE) { switch (s[i]) { case '(': case '[': case '{': push(S, s[i]); break; case ')': if (pop(S, e) == ERROR || e != '(') flag = FALSE; break; case ']': if (pop(S, e) == ERROR || e != '[') flag = FALSE; break; case '}': if (pop(S, e) == ERROR || e != '{') flag = FALSE; break; } i++; } if (flag == TRUE && s[i] == '#' && S.top == S.base) return TRUE; else return FALSE; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值