博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C实现二叉树BTree基本操作
阅读量:6904 次
发布时间:2019-06-27

本文共 4286 字,大约阅读时间需要 14 分钟。

hot3.png

/*** @file GM_BTree.h* @brief * @author * @date * @version */#ifndef _GM_BTREE_H#define _GM_BTREE_H#include 
#include
typedef struct BTree{ char value; struct BTree* lChild; struct BTree* rChild;}BTree_Struct;#ifdef __cplusplusextern"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

你可能感兴趣的文章
RPC框架性能基本比较测试
查看>>
git安装
查看>>
SEO黑页以及门页框架和JS跳转实现方法
查看>>
html5 Ajax 访问.net WebApi获取视频流
查看>>
[HNOI2008]玩具装箱TOY
查看>>
luogu P1801 黑匣子_NOI导刊2010提高(06)
查看>>
Java jdk环境变量配置
查看>>
Given Name.Family Name的区别
查看>>
读取Mysql的一种的方式
查看>>
信息安全--仿射密码
查看>>
深入浅出javascript(二)函数和this对象
查看>>
Form 对象
查看>>
Codeforces Round #533(Div. 2) C.Ayoub and Lost Array
查看>>
HDU - 3966-Aragorn' Story(树链剖分+线段树)
查看>>
Linux基础第五章 进程控制
查看>>
[转载]孤儿进程与僵尸进程[总结]
查看>>
jquery事件机制扩展,jquery鼠标右键事件。
查看>>
windows phone Image checkbox
查看>>
[pycharm]远程调试服务器项目
查看>>
7 Java NIO Selector-翻译
查看>>