C实现二叉树BTree基本操作

/**
* @file GM_BTree.h
* @brief 
* @author 
* @date 
* @version 
*/
#ifndef _GM_BTREE_H
#define _GM_BTREE_H

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

typedef struct BTree
{
    char value;
    struct BTree* lChild;
    struct BTree* rChild;
}BTree_Struct;

#ifdef __cplusplus
extern"C"
{
#endif /**< __cplusplus */

    /** 
    * @brief GM_BTree_Create 
    * 
    * 创建树.
    * @return BTree_Struct*  
    */
    BTree_Struct* GM_BTree_Create();

    /** 
    * @brief GM_BTree_PreOrder 
    * 
    * 先序遍历.
    * @param[in] tree 
    */
    void GM_BTree_PreOrder(BTree_Struct* tree);

    /** 
    * @brief GM_BTree_InOrder 
    * 
    * 中序遍历.
    * @param[in] tree 
    */
    void GM_BTree_InOrder(BTree_Struct* tree);

    /** 
    * @brief GM_BTree_PostOrder 
    * 
    * 后序遍历.
    * @param[in] tree 
    */
    void GM_BTree_PostOrder(BTree_Struct* tree);

    /** 
    * @brief GM_BTree_Search_PreOrder 
    * 
    * 先序查找.
    * @param[in] tree 
    * @param[in] value 
    */
    void GM_BTree_Search_PreOrder(BTree_Struct* tree, int value);

    /** 
    * @brief GM_BTree_Search_InOrder 
    * 
    * 中序查找.
    * @param[in] tree 
    * @param[in] value 
    */
    void GM_BTree_Search_InOrder(BTree_Struct* tree, int value);

    /** 
    * @brief GM_BTree_Search_PostOrder 
    * 
    * 后序查找.
    * @param[in] tree 
    * @param[in] value 
    */
    void GM_BTree_Search_PostOrder(BTree_Struct* tree, int value);

    /** 
    * @brief GM_BTree_Clear 
    * 
    * 清空.
    * @param[in] tree 
    */
    void GM_BTree_Clear(BTree_Struct* tree);
#ifdef __cplusplus
}

#endif /**< __cplusplus */

#endif /**< _GM_BTREE_H */

 

/**
* @file GM_BTree.c
* @brief 
* @author 
* @date 
* @version 
*/
#include "GM_BTree.h"

static BTree_Struct* pTree = NULL;

BTree_Struct* GM_BTree_Create()
{
    BTree_Struct* node = (BTree_Struct*)malloc(sizeof(BTree_Struct));

    if (NULL == node)
    {
        printf("Malloc memory error when creating tree");
        return NULL;
    }

    scanf("%c", &node->value);//输入先序ABD.F..EG..H..C..,其中.表示为NULL
    /*
    A
    / \
    B   C
    / \
    D   E
    \  / \
    F G  H
    */

    if(node->value=='.')
    {
        return NULL;
    }

    node->lChild = GM_BTree_Create();
    node->rChild = GM_BTree_Create();

    return node;
}

void GM_BTree_PreOrder(BTree_Struct* tree)
{
    if (NULL == tree)
    {
        return;
    }

    printf("%c\n", tree->value);
    GM_BTree_PreOrder(tree->lChild);
    GM_BTree_PreOrder(tree->rChild);
}

void GM_BTree_InOrder(BTree_Struct* tree)
{
    if (NULL == tree)
    {
        return;
    }
    GM_BTree_InOrder(tree->lChild);
    printf("%c\n", tree->value);
    GM_BTree_InOrder(tree->rChild);
}

void GM_BTree_PostOrder(BTree_Struct* tree)
{
    if (NULL == tree)
    {
        return;
    }

    GM_BTree_PostOrder(tree->lChild);
    GM_BTree_PostOrder(tree->rChild);
    printf("%c\n", tree->value);
}

void GM_BTree_Search_PreOrder(BTree_Struct* tree, int value)
{
    if (NULL == tree)
    {
        return;
    }

    if (value == tree->value)
    {
        printf("Find\n");
    }

    GM_BTree_Search_PreOrder(tree->lChild, value);
    GM_BTree_Search_PreOrder(tree->rChild, value);
}

void GM_BTree_Search_InOrder(BTree_Struct* tree, int value)
{
    if (NULL == tree)
    {
        return;
    }

    GM_BTree_Search_InOrder(tree->lChild, value);

    if (value == tree->value)
    {
        printf("Find\n");
    }

    GM_BTree_Search_InOrder(tree->rChild, value);
}

void GM_BTree_Search_PostOrder(BTree_Struct* tree, int value)
{
    if (NULL == tree)
    {
        return;
    }

    GM_BTree_Search_PostOrder(tree->lChild, value);
    GM_BTree_Search_PostOrder(tree->rChild, value);

    if (value == tree->value)
    {
        printf("Find\n");
    }
}

void GM_BTree_Clear(BTree_Struct* tree)
{
    if (NULL == tree)
    {
        return;
    }

    GM_BTree_Clear(tree->lChild);
    GM_BTree_Clear(tree->rChild);

    tree->lChild = NULL;
    tree->rChild = NULL;
    tree = NULL;
}

void main()
{
    pTree = GM_BTree_Create();

    GM_BTree_PreOrder(pTree);
    printf("\n");
    GM_BTree_InOrder(pTree);
    printf("\n");
    GM_BTree_PostOrder(pTree);
    printf("\n");

    GM_BTree_Search_PreOrder(pTree, 'F');
    GM_BTree_Search_PreOrder(pTree, 'J');
    GM_BTree_Search_InOrder(pTree, 'F');
    GM_BTree_Search_InOrder(pTree, 'J');
    GM_BTree_Search_PostOrder(pTree, 'F');
    GM_BTree_Search_PostOrder(pTree, 'J');

    GM_BTree_Clear(pTree);

    pTree = NULL;
}

转载于:https://my.oschina.net/kaixindewo/blog/30425

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值