程序实现二叉树遍历:先序/中序/后序/层次(递归/非递归)

问题描述:

按先序遍历的扩展序列建立二叉树的二叉链表存储结构,实现二叉树先序、中序、后序遍历的递归算法,实现二叉树中序遍历的非递归算法,实现二叉树层次遍历的非递归算法,求二叉树的结点个数,求二叉树的深度。

代码实现:

#include <stdio.h>
#include <stdlib.h>
#define MSIZE 100

typedef struct Node{
    char data;
    struct Node *lchild;
    struct Node *rchild;
}Node, *Tree;
//定义一个二叉树

void CreatTree(Tree *T){
    char ch;
    scanf("%c",&ch);
    if(ch == ' ')
        (*T) = NULL;
    else
    {
        (*T) = (Tree)malloc(sizeof(Node));
        (*T)->data = ch;
        CreatTree(&((*T)->lchild));
        CreatTree(&((*T)->rchild));
    }
}
//读入用户输入生成一个二叉树

void Front(Tree T){
    if(T)
    {
        printf("%c", T->data);
        Front(T->lchild);
        Front(T->rchild);
    }
}
//先序遍历的递归实现

void Middle(Tree T){
    if(T)
    {
        Middle(T->lchild);
        printf("%c", T->data);
        Middle(T->rchild);
    }
}
//中序遍历的递归实现

void Last(Tree T){
    if(T)
    {
        Last(T->lchild);
        Last(T->rchild);
        printf("%c", T->data);
    }
}
//后序遍历的递归实现

void Middle2(Tree T){
    Node *stack[MSIZE];//定义一个栈
    int top = -1;
    Node *p;//定义一个临时指针p
    p = T;
    while(p || top > -1)
    {
        if(p)
        {
            top++;
            stack[top] = p;
            p = p->lchild;//p不为空,遍历左子树
        }
        else
        {
            p = stack[top];
            printf("%c", p->data);
            top--;
            p = p->rchild;//遍历右子树
        }
    }
}
//利用栈实现非递归中序遍历

void level(Tree T){
    Node *queue[MSIZE];//定义队列
    int front = 0;
    int rear = 0;
    int count;
    Node *p;//定义临时指针p
    rear++;
    queue[rear] = T;//根节点进入队列
    while(front != rear)//队列不空
    {
        count = rear - front;
        while(count > 0)
        {
            count--;
            front++;
            p = queue[front];
            printf("%c", p->data);
            if(p->lchild)
            {
                rear++;
                queue[rear] = p->lchild;
            }
            if(p->rchild)
            {
                rear++;
                queue[rear] = p->rchild;
            }
        }
    }
}
//二叉树的层次遍历

int NodeCount(Tree T){
    if(!T) 
        return 0;
    else
        return (NodeCount(T->lchild) + NodeCount(T->rchild) + 1);
}

int TreeDepth(Tree T){
    int a, b;
    if(!T)
        return 0;
    else
    {
        a = TreeDepth(T->lchild);
        b = TreeDepth(T->rchild);
        if(a > b)
            return (a + 1);
        else
            return (b + 1);
    }
}

int main() {
    Tree T = NULL;
    printf("请输入数的先序扩展序列,输入空格表示该位置是空,按回车结束\n");
    CreatTree(&T);//生成树
    printf("前序遍历:");
    Front(T);
    printf("\n");
    printf("中序遍历:");
    Middle(T);
    printf("\n");
    printf("后序遍历:");
    Last(T);
    printf("\n");
    printf("非递归中序遍历:");
    Middle2(T);
    printf("\n");
    printf("非递归层次遍历:");
    level(T);
    printf("\n");
    printf("树的结点个数为:%d\n", NodeCount(T));
    printf("树的深度为:%d\n", TreeDepth(T));
    system("pause"); 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哆啦叮当

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值