码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-06-12 19:28:02      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:递归   树的遍历   

描述

树的遍历即给出一个指向树的指针,访问树中的每一个节点。树的遍历有三种基本遍历方式,分别是前序(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;
}

技术分享

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

标签:递归   树的遍历   

原文地址:http://blog.csdn.net/zwhlxl/article/details/44151695

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!