C实现二叉树BTree基本操作

来源:互联网 发布:巨人网络回归概念股 编辑:程序博客网 时间:2024/05/16 06:46
 
/*** @file GM_BTree.h* @brief * @author Don Hao* @date 2011-8-22 21:51:35* @version * <pre><b>copyright: </b></pre>* <pre><b>email: </b>hao.limin@gmail.com</pre>* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>* <pre><b>All rights reserved.</b></pre>* <pre><b>modification:</b></pre>* <pre>Write modifications here.</pre>*/#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 __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 Don Hao* @date 2011-8-22 21:51:13* @version * <pre><b>copyright: </b></pre>* <pre><b>email: </b>hao.limin@gmail.com</pre>* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>* <pre><b>All rights reserved.</b></pre>* <pre><b>modification:</b></pre>* <pre>Write modifications here.</pre>*/#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;}
原创粉丝点击