树的前序,中序,后序遍历(递归)

描述

树的遍历即给出一个指向树的指针,访问树中的每一个节点。树的遍历有三种基本遍历方式,分别是前序(preorder)、中序(inorder)、后序(postorder)。

递归实现

原理

  1. 前序(preorder):先访问节点,然后访问该节点的左子树和右子树;
  2. 中序(inorder) : 先访问该节点的左子树,然后访问该节点,再访问该节点的右子树;
  3. 后序( postorder) : 先访问该节点的左子树和右子树,然后访问该节点。

代码实现

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef char ElementType;
typedef int Status;

int index = 0;
char str[] = "ABDH#K###E##CFI###G#J##";

typedef struct TreeNode
{
    ElementType data;
    struct TreeNode *Left;
    struct TreeNode *Right;
}TreeNode, *pTree;

Status InitTree(pTree *T)
{
    *T = NULL;
    return OK;
}

Status Visit(pTree T)
{
    if(T == NULL)
        return ERROR;
    printf("%c ",T->data);
    return OK;
}

void DeleteTree(pTree *T)
{
    if(*T)  
    {
        if((*T)->Left)
            DeleteTree(&(*T)->Left);
        if((*T)->Right)
            DeleteTree(&(*T)->Right);
        free(*T);
        *T = NULL;
    }
}

void CreateTree(pTree *T)
{
    ElementType ch;
    ch = str[index++];
    if(ch == '#')
        *T = NULL;
    else
    {
        *T = (pTree)malloc(sizeof(TreeNode));
        if((*T) == NULL)
            exit(0);
        (*T)->data = ch;
        CreateTree(&(*T)->Left);
        CreateTree(&(*T)->Right);
    }

}

int TreeDepth(pTree T)
{
    int Ldepth, Rdepth;
    if(T == NULL)
        return -1;
    if(T->Left)
        Ldepth = TreeDepth(T->Left);
    else 
        Ldepth = 0;
    if(T->Right)
        Rdepth = TreeDepth(T->Right);
    else
        Rdepth = 0;

    return (Ldepth > Rdepth)? Ldepth + 1 : Rdepth + 1;
}

int TreeNodeCount(pTree T)
{
    if( T == NULL)
        return 0;
    return TreeNodeCount(T->Left) + TreeNodeCount(T->Right) + 1;
}
int TreeIsEmpty(pTree T)
{
    if(T)
        return FALSE;
    else 
        return TRUE;
}

void PreorderTraverse(pTree T, Status (*Visit)(pTree))
{
    if(T == NULL)
        return;
    (*Visit)(T);
    //printf("%c ",T->data);
    PreorderTraverse(T->Left,Visit);
    PreorderTraverse(T->Right,Visit);
}

void InorderTraverse(pTree T, Status (*Vistit)(pTree))
{
    if(T == NULL)
        return;
    InorderTraverse(T->Left,Visit);
    (*Visit)(T);
    InorderTraverse(T->Right,Visit);
}

void PostorderTraverse(pTree T, Status (*Visit)(pTree))
{
    if(T == NULL)
        return;
    PostorderTraverse(T->Left,Visit);
    PostorderTraverse(T->Right,Visit);
    (*Visit)(T);
}

int main()
{
    pTree Tree;

    InitTree(&Tree);

    CreateTree(&Tree);

    printf("Tree's Depth is %d\n",TreeDepth(Tree));
    printf("Tree's Node number is %d\n",TreeNodeCount(Tree));
    if(TreeIsEmpty(Tree))
    {
        printf("Tree is Empty\n");
    }

    printf("PreorderTraverse is :");
    PreorderTraverse(Tree,Visit);
    printf("\n");

    printf("InorderTraverse is :");
    InorderTraverse(Tree,Visit);
    printf("\n");

    printf("PostorderTraverse is :");
    PostorderTraverse(Tree,Visit);
    printf("\n");

    DeleteTree(&Tree);

    if(TreeIsEmpty(Tree))
    {
        printf("Tree is Delte and Empty\n");
    }
    return 0;
}

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值