二叉树的基本操作(C语言实现)

CSeQueue.h文件

#include<malloc.h>

typedefcharDataType;

#define MAXSIZE 100

 

typedefstruct

{

    BiTree data[MAXSIZE];//队列存储区

    int front, rear;//队头队尾指针

}CSeQueue;

//置空队列

CSeQueue * InitSeQueue();

 

/*

入队

*/

void InSeQueue(CSeQueue *q, BiTreep);

 

 

/*

出队

*/

void OutSeQueue(CSeQueue *q, BiTree *p);

 

/*

判队空

*/

int EmptySeQueue(CSeQueue *q);

 

CSeQueue.c文件   

#include<malloc.h>

#include<stdio.h>

typedefcharDataType;

#define MAXSIZE 100

typedefstructNode

{

    DataType data;

    struct  Node *Lchild;

    struct  Node *Rchild;

 

}BiTNode, *BiTree;

typedefstruct

{

    BiTree data[MAXSIZE];//队列存储区

    int front, rear;//队头队尾指针

}CSeQueue;

//置空队列

CSeQueue * InitSeQueue()

{

    CSeQueue *q;

    q = (CSeQueue *)malloc(sizeof(CSeQueue));

    q->front = q->rear = MAXSIZE - 1;

    return q;

 }

/*

  入队

*/

void InSeQueue(CSeQueue *q, BiTreep)

{

    if ((q->rear + 1) % MAXSIZE == q->front)

    {

         printf("队满!\n");

    }

    else

    {

         q->rear = (q->rear + 1) % MAXSIZE;

         q->data[q->rear] = p;

 

    }

}

/*

   出队

*/

void OutSeQueue(CSeQueue *q, BiTree *p)

{

    if (q->front == q->rear)

    {

         printf("队空\n");

    }

    else

    {

         q->front = (q->front + 1) % MAXSIZE;

         *p = q->data[q->front];

    }

}

/*

  判队空

  */

int EmptySeQueue(CSeQueue *q)

{

    if (q->front == q->rear) return 0;

    else

    {

         return 1;

    }

}

SeqStack.h文件

#include<stdio.h>

#include<malloc.h>

#include<stdlib.h>

#defineMAXSIZE 100

 

 

typedefcharDataType;

typedefstructNode

{

    DataType data;

    struct  Node *Lchild;

    struct  Node *Rchild;

 

}BiTNode, *BiTree;

typedefstruct

{

    BiTree data[MAXSIZE];

    int  top;

 

}SeqStack;

/*

初始化栈,将栈顶置空

*/

SeqStack *Init_SeqStack();

 

/*判栈空*/

int Empty_SeqStack(SeqStack *s);

void push_SeqStack(SeqStack *s, BiTreex);

/*出栈*/

void Pop_SeqStack(SeqStack *s, BiTree *x);

 

/*

取得栈顶元素

*/

void Top_SeqStack(SeqStack *s, BiTree *x);

void print_SeqStack(SeqStack *s);

 

     

SeqStack.c文件

#include<stdio.h>

#include<malloc.h>

#include<stdlib.h>

#defineMAXSIZE 100

 

 

typedefcharDataType;

typedefstructNode

{

    DataType data;

    struct  Node *Lchild;

    struct  Node *Rchild;

 

}BiTNode, *BiTree;

typedefstruct

{

    BiTree data[MAXSIZE];

    int  top;

     

}SeqStack;

/*

  初始化栈,将栈顶置空

*/

SeqStack *Init_SeqStack()

{

    SeqStack *s;

    s = (SeqStack *)malloc(sizeof(SeqStack));

    s->top = -1;

    return s;

}

 

/*判栈空*/

int Empty_SeqStack(SeqStack *s)

{

    if (s->top == -1) return 1;

    elsereturn 0;

}

void push_SeqStack(SeqStack *s, BiTreex)

{

    if (s->top == MAXSIZE - 1) printf("对不起,栈已经达到最大容量了");

    else

    {

         s->top++;

         s->data[s->top] =x;

          

    }

}

 

/*出栈*/

void Pop_SeqStack(SeqStack *s, BiTree *p)

{

    if(Empty_SeqStack(s)) printf("栈空\n");

    else

    {

         *p = s->data[s->top];

         s->top--;

         //printf("%d出栈成功\n",x);

    }

   

}

 

/*

   取得栈顶元素

*/

void Top_SeqStack(SeqStack *s, BiTree *x)

{

    if(Empty_SeqStack(s)) return  ;

    else

        *x=s->data[s->top];

}

 

void print_SeqStack(SeqStack *s) {

   

    while (s->top != -1)

    {

          

         s->top--;

    }

}

Tree.c

#include<stdio.h>

#include<malloc.h>

#include"SeqStack.h"

#include"CSeQueue.h"

int  Count = 0;

void Visit(charch)

{

    printf(" %c",ch);

}

/*

先序遍历二叉树

*/

void PreOrder(BiTreeroot)

{

    if (root)

    {

         Visit(root->data);//访问根节点

         PreOrder(root->Lchild);//先序遍历左子树

         PreOrder(root->Rchild);//先序遍历右子树

    }

}

/*

中序遍历二叉树

*/

void InOrder(BiTreeroot)

{

    if (root)

    {

         InOrder(root->Lchild);//中序遍历左子树

         Visit(root->data);//访问根节点

         InOrder(root->Rchild);//中序遍历右子树

    }

 

 }

/*

后序遍历二叉树

*/

void PostOrder(BiTreeroot)

{

    if (root)

    {

         PostOrder(root->Lchild);//后续遍历左子树

         PostOrder(root->Rchild);//后续遍历右子树

         Visit(root->data);//访问根节点

    }

}

/*

  顺序创建中间节点,左子树右子树

*/

BiTree CreateTree()

{

    BiTNode *p;

    DataType ch; 

    scanf("%c",&ch);

    if (ch ==' ') //如果是叶子节点,接下来,左子树右子树都为空

    {

         p = NULL;

    }

    else

    {

         p = (BiTree)malloc(sizeof(BiTNode));

         p->data = ch;

         p->Lchild = CreateTree();

         p->Rchild = CreateTree();

    }

    return p;

}

/*

   先序非递归遍历二叉树

*/

 void preOrder(BiTreeroot)

{

    SeqStack *s;

    BiTree p;

    s=Init_SeqStack();//对栈进行初始化

    p = root;//获得树的根节点

    while(p!=NULL||!Empty_SeqStack(s))

    {

         while (p!= NULL)

         {

             Visit(p->data);

             push_SeqStack(s,p);

             p = p->Lchild;

         }

             if (!Empty_SeqStack(s))

             {

                  Pop_SeqStack(s,&p);

                  p = p->Rchild;

             }

         }

    }

 

 

 /*

  中序非递归遍历二叉树

 */

 void InPoder1(BiTreeroot)

 {

     SeqStack *s;

     BiTree p;

     s=Init_SeqStack();

     p = root;

     while (p != NULL || !Empty_SeqStack(s))

     {

          while (p != NULL)

          {

              push_SeqStack(s, p);

              p = p->Lchild;

          }

          if (!Empty_SeqStack(s))

          {

              Pop_SeqStack(s, &p);

              Visit(p->data);

              p = p->Rchild;

          }

     }

 }

 /*

 后续非递归遍历二叉树

 */

 void PostPOrder1(BiTreeroot)

 {

     SeqStack *s;

     BiTree p, q;

     s=Init_SeqStack();

     p = root;

     q = NULL;

     while (p!=NULL||!Empty_SeqStack(s))

     {

          while(p!=NULL)

          {

              push_SeqStack(s, p);

              p = p->Lchild;

          }

        

     if (!Empty_SeqStack(s))

     {

          Top_SeqStack(s,&p);

          if ((p->Rchild == NULL) || (p->Rchild == q))

          {

              Pop_SeqStack(s, &p);

              Visit(p->data);

              q = p;

              p = NULL;

          }

          else

              p = p->Rchild;

     }

     }

 }

 /*

   以先序遍历的方式统计二叉树中的节点数

 */

 void  CountNode(BiTreeroot)

 {

     if (root)

     {

          Count++;

          CountNode(root->Lchild);

          CountNode(root->Rchild);

     }

 }

 /*

    使用后序遍历求二叉树的高度

 */

 int PostTreeDepth(BiTreeroot)

 {

     int hl, hr, h;

     if (root == NULL) return 0;

     else {

          hl=PostTreeDepth(root->Lchild);

          hr=PostTreeDepth(root->Rchild);

          h = (hl > hr ? hl : hr) + 1;

          return h;

     }

     

 }

 /*

    按树状打印二叉树

 */

 void PrintTree(BiTreeroot, inth)

 {

     int i = 0;

     if (root)

     {

          PrintTree(root->Rchild, h + 2);

          for (; i < h; i++) printf("");

          printf("%c\n",root->data);

          PrintTree(root->Lchild, h + 2);

     }

 }

 

 /*

   二叉树的层次遍历

 */

 void LevelOrder(BiTreeroot)

 {

     CSeQueue  *Q ;

     BiTree p;

      Q=InitSeQueue();

      InSeQueue(Q,root);

     while (EmptySeQueue(Q))

     {

         OutSeQueue(Q, &p);

         Visit(p->data);

         if (p->Lchild!=NULL )

         InSeQueue(Q, p->Lchild);

         if (p->Rchild!=NULL)

         InSeQueue(Q, p->Rchild); 

     }

 }

 /*

 输出二叉树的叶子节点

 */

 void InOrde(BiTreeroot)

 {

     if (root)

     {

          InOrde(root->Lchild);

          if (root->Lchild == NULL&&root->Rchild == NULL)

              printf("%c ",root->data);

           InOrde(root->Rchild);

     }

 }

 /*

  后序遍历统计叶子节点数目

 */

 int leaf(BiTreeroot)

 {

     int nl, nr;

     if (root == NULL)return 0;

     if ((root->Lchild == NULL) && (root->Rchild == NULL)) return 1;

     nl =leaf(root->Lchild);

     nr =leaf(root->Rchild);

     return (nl+nr);

 }

int main(void)

 {

    BiTNode *root = NULL;

    printf("请输入树的结构:");

    root = CreateTree();

    printf("二叉树创建成功:\n");

    printf("先序递归遍历结果:\n");

    PreOrder(root);

    printf("\n中序递归遍历结果:\n");

    InOrder(root);

    printf("\n后续递归遍历结果:\n");

    PostOrder(root);

    printf("\n先序非递归遍历结果:\n");

    preOrder(root);

    printf("\n中序非递归遍历结果:\n");

    InPoder1(root);

    printf("\n后续非递归遍历结果:\n");

    PostPOrder1(root);

    printf("\n");

    CountNode(root);

    printf("二叉树的节点有%d\n", Count);

    printf( "二叉树的高度为:%d\n",PostTreeDepth(root) );

    printf("二叉树的层次遍历的结果如下:\n");

    LevelOrder(root);

    printf("\n");

    printf("二叉树的树形打印结果如下:\n");

    PrintTree(root,PostTreeDepth(root));

    printf("二叉树的叶子节点有:\n");

    InOrde(root);

    printf("\n二叉树的叶子节点数有%d:",leaf(root));

}

结果如下:


  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值