C实现二叉树BTree基本操作
2011-08-23 16:30
489 查看
/** * @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 __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 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; }
相关文章推荐
- 二叉树基本操作实现(二叉树的链式存储)
- 二叉树学习(二):二叉树的基本操作及代码实现
- C++实现二叉树的基本操作(递归+非递归)
- c++模板实现二叉树,线索化,线索化遍历,非递归遍历及一些基本操作
- 算法与数据结构-二叉树的基本操作C语言实现
- 【代码】C++实现二叉树基本操作及测试用例
- 实现基本二叉树操作
- 二叉树的基本操作的c++实现
- 二叉树基本操作--java实现
- 二叉树基本操作的程序实现
- 数据结构第四次作业(二叉树的基本操作实现)
- 二叉树实现及基本操作
- 二叉树的基本操作实现(数据结构实验)
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- 八.二叉树各种操作的C语言实现 树的一些基本的操作,包括,树的建立,树的深度,
- 二叉树基本操作的程序实现
- 数据结构.二叉树的基本操作(C语言实现)
- Java实现二叉树基本操作
- 实验四 二叉树基本操作的实现
- 二叉树的基本操作 C++代码实现