C實現二叉樹BTree基本操作

來源:互聯網
上載者:User

 

/*** @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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.