几乎报价http://blog.csdn.net/hopeyouknow/article/details/6740616。为了这细微的地方进行了修改。他能够执行。

bitree.h

typedef int Item;
typedef struct node
{
struct node *lchild;
struct node *rchild;
Item data;
}BiTNode, *BiTree; BiTree InitBiTree(BiTNode *root); BiTNode *MakeNode(Item item, BiTNode *lchild, BiTNode *rchild); void FreeNode(BiTNode *pnode); void ClearBiTree(BiTree tree); void DestroyBiTree(BiTree tree); int IsEmpty(BiTree tree); int GetDepth(BiTree tree); BiTree GetRoot(BiTree tree); Item GetItem(BiTNode *pnode); void SetItem(BiTNode *pnode, Item item); BiTree SetLChild(BiTree parent, BiTree lchild); BiTree SetRChild(BiTree parent, BiTree rchild); BiTree GetLchild(BiTree tree); BiTree GetRchild(BiTree tree); BiTree InsertChild(BiTree parent, int lr, BiTree child); void DeleteChild(BiTree parent, int lr); void PreOrderTraverse(BiTree tree, void (*visit)()); void InOrderTraverse(BiTree tree, void (*visit)()); void PostOrderTraverse(BiTree tree, void (*visit)());

bitree.c

#include "bitree.h"
#include <malloc.h>
#include <stdlib.h> BiTree InitBiTree(BiTNode *root)
{
BiTree tree = root;
return tree;
} BiTNode *MakeNode(Item item, BiTNode *lchild, BiTNode *rchild)
{
BiTNode *pnode = (BiTNode *)malloc(sizeof(BiTNode));
if(pnode){
pnode->data = item;
pnode->lchild = lchild;
pnode->rchild = rchild;
}
return pnode;
} void FreeNode(BiTNode *pnode)
{
if(pnode != NULL)
free(pnode);
} void ClearBiTree(BiTree tree)
{
BiTNode *pnode = tree;
if(pnode->lchild != NULL)
ClearBiTree(pnode->lchild); if(pnode->rchild != NULL)
ClearBiTree(pnode->rchild); FreeNode(pnode);
} void DestroyBiTree(BiTree tree)
{
if(tree)
ClearBiTree(tree);
} IsEmpty(BiTree tree)
{
if(tree == NULL)
return 0;
else
return 1;
} int GetDepth(BiTree tree)
{
int cd, ld, rd;
cd = ld = rd = 0;
if(tree){
ld = GetDepth(tree->lchild);
rd = GetDepth(tree->rchild);
cd = (ld > rd ? ld : rd);
return cd+1;
}else
return 0;
} BiTree GetRoor(BiTree tree)
{
return tree;
} Item GetItem(BiTNode *pnode)
{
return pnode->data;
} void SetItem(BiTNode *pnode, Item item)
{
pnode->data = item;
} BiTree SetLChild(BiTree parent, BiTree lchild)
{
parent->lchild = lchild;
return lchild;
} BiTree SetRChild(BiTree parent, BiTree rchild)
{
parent->rchild = rchild;
return rchild;
} BiTree GetLchild(BiTree tree)
{
if(tree)
return tree->lchild;
return NULL;
} BiTree GetRchild(BiTree tree)
{
if(tree)
return tree->rchild;
return NULL;
} BiTree InsertChild(BiTree parent, int lr, BiTree child)
{
if(parent){
if(lr == 0 && parent->lchild == NULL){
parent->lchild = child;
return child;
}
if(lr == 1 && parent->lchild == NULL){
parent->rchild = child;
return child;
}
}
return NULL;
} void DeleteChild(BiTree parent, int lr)
{
if(parent){
if(lr == 0 && parent->lchild != NULL){
parent->lchild = NULL;
FreeNode(parent->lchild);
}
if(lr == 1 && parent->rchild != NULL){
parent->rchild = NULL;
FreeNode(parent->rchild);
}
}
} void PreOrderTraverse(BiTree tree, void (*visit)())
{
BiTNode *pnode = tree;
if(pnode){
visit(pnode->data);
PreOrderTraverse(pnode->lchild, visit);
PreOrderTraverse(pnode->rchild, visit);
}
} void InOrderTraverse(BiTree tree, void (*visit)())
{
BiTNode *pnode = tree;
if(pnode){
InOrderTraverse(pnode->lchild, visit);
visit(pnode->data);
InOrderTraverse(pnode->lchild, visit);
}
} void PostOrderTraverse(BiTree tree, void (*visit)())
{
BiTNode *pnode = tree;
if(pnode){
PostOrderTraverse(pnode->lchild, visit);
PostOrderTraverse(pnode->lchild, visit);
visit(pnode->data);
}
}

test.c

#include "bitree.h"
#include <stdio.h> void print(Item item)
{
printf("%d ", item);
} main()
{
BiTNode *n1 = MakeNode(10, NULL, NULL);
BiTNode *n2 = MakeNode(20, NULL, NULL);
BiTNode *n3 = MakeNode(30, n1, n2);
BiTNode *n4 = MakeNode(40, NULL, NULL);
BiTNode *n5 = MakeNode(50, NULL, NULL);
BiTNode *n6 = MakeNode(60, n4, n5);
BiTNode *n7 = MakeNode(70, NULL, NULL); BiTree tree = InitBiTree(n7);
SetLChild(tree, n3);
SetRChild(tree, n6); printf("the depth of the tree is %d.\n", GetDepth(tree)); printf("preorderTraverse:\n");
PreOrderTraverse(tree, print);
putchar('\n'); printf("inorderTraverse:\n");
InOrderTraverse(tree, print);
putchar('\n'); printf("postorderTraverse:\n");
PostOrderTraverse(tree, print);
putchar('\n'); DeleteChild(tree, 1);
printf("postorderTraverse:\n");
PostOrderTraverse(tree, print); DestroyBiTree(tree);
if(IsEmpty(tree))
printf("the tree is empty!\n"); return 0; }

编译:gcc test.c bitree.c

执行:./a.out

执行结果:

the depth of the tree is 3.
preorderTraverse:
70 30 10 20 60 40 50
inorderTraverse:
10 30 10 70 10 30 10
postorderTraverse:
10 10 30 10 10 30 70
postorderTraverse:
10 10 30 10 10 30 70
the tree is empty!

版权声明:本文博客原创文章,博客,未经同意,不得转载。

二叉树C语言的更多相关文章

  1. 数据结构之线索二叉树——C语言实现

     线索二叉树操作 (1) 线索二叉树的表示:将每个节点中为空的做指针与右指针分别用于指针节点的前驱和后续,即可得到线索二叉树. (2) 分类:先序线索二叉树,中序线索二叉树,后续线索二叉树 (3) 增 ...

  2. c语言_二叉树的建立以及3种递归

    二叉树c语言的实现 二叉树的建立 二叉树的数据结构 typedef struct node{    int data;    struct node* left;    struct node* ri ...

  3. 小白专场-树的同构-c语言实现.md

    目录 一.题意理解 二.求解思路 2.1 二叉树表示 2.2 程序框架搭建 2.3 如何建二叉树 2.4 如何判别两二叉树同构 更新.更全的<数据结构与算法>的更新网站,更有python. ...

  4. 数据结构算法C语言实现(二十)--- 6.3.1遍历二叉树

    一.简述 二叉树的遍历主要是先序.中序.后序及对应的递归和非递归算法,共3x2=6种,其中后序非递归在实现上稍复杂一些.二叉树的遍历是理解和学习递归及体会栈的工作原理的绝佳工具! 此外,非递归所用的栈 ...

  5. C语言实现二叉树-02版

    ---恢复内容开始--- 昨天,提交完我们的二叉树项目后,今天早上项目经理早早给我打电话: 他说,小伙子干的不错.但是为什么你上面的insert是recusive的呢? 你难道不知道万一数据量大啦!那 ...

  6. C语言实现二叉树-利用二叉树统计单词数目

    昨天刚参加了腾讯2015年在线模拟考: 四道大题的第一题就是单词统计程序的设计思想: 为了记住这一天,我打算今天通过代码实现一下: 我将用到的核心数据结构是二叉树: (要是想了解简单二叉树的实现,可以 ...

  7. C语言实现二叉树

    二叉树的重要性就不用多说啦: 我以前也学习过,但是一直没有总结: 网上找到的例子,要么是理论一大堆,然后是伪代码实现: 要么是复杂的代码,没有什么解释: 最终,还是靠FQ找到一些好的文章,参考地址我会 ...

  8. C语言实现有序二叉树(1)

    在cpp中使用的C语言 头文件 /* 有序二叉树 BsTree */ #ifndef _BT_H #define _BT_H /*节点*/ typedef struct BsTreeNode { in ...

  9. C语言实现二叉树的基本操作

    二叉树是一种非常重要的数据结构.本文总结了二叉树的常见操作:二叉树的构建,查找,删除,二叉树的遍历(包括前序遍历.中序遍历.后序遍历.层次遍历),二叉搜索树的构造等. 1. 二叉树的构建 二叉树的基本 ...

随机推荐

  1. arm Linux 如何自动检测并mount SD卡,以及如何得知已经mount

    一.土八路做法: SD 卡一旦插入系统,内核会自动在/dev/下创建设备文件:sdcard. 但有时可能时用户在拨出卡前并没有umount的话,第二次插卡进去后系统创建的就不是sdcard设备文件了, ...

  2. Linux 如何查看一个进程的堆栈

    有两种方法: 第一种:pstack 进程ID 第二种,使用gdb 然后attach 进程ID,然后再使用命令 thread apply all bt 第三种:strace -f -p pid  该方法 ...

  3. BZOJ 1588 HNOI2002 营业额统计 裸Treap

    题目大意:...题目描写叙述不全看这里好了 给定一个序列 对于每一个元素我们定义该数的最小波动值为这个数与前面全部数的差中的最小值(第一个数的最小波动值为第一个数本身) 求最小波动值之和 找近期的数仅 ...

  4. ORACLE会话数、连接数配置

    ORACLE会话数.连接数配置 ORACLE会话数.连接数配置 ORACLE的会话数和连接数参数配置 以sysdba身份登录 sqlplus sys/xxxx as sysdba; 查看最大连接数: ...

  5. cocos2d-x win8下的环境配置和建立项目

    cocos2dx 跨平台.可是看网上说开发最合适还是在vs2010中,基本是编完后再移植到安卓. 1.去官网下载源代码2.2.3版本号的. 2.然后运行根文件夹下的build-win32.bat(须要 ...

  6. DisplayPageBoundaries 打开word后自动将页面间空白隐藏 (auto+定时器)

    每次打开文档都要鼠标点击页面间空白处,将其隐藏 尝试过在 AutoOpen, AutoExec等宏中添加 ActiveWindow.View.DisplayPageBoundaries = False ...

  7. 回顾Oracle几个用到的基本语句

    create table t_name(id number,name varchar2(10)); drop table t_name; select * from table_name where ...

  8. POJ 3714 Raid(平面近期点对)

    解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...

  9. iOS开发- Xcode插件- 规范凝视生成器VVDocumenter 自己的见解

    xcode升级  VVDocumenter 插件失效怎么办?? 首先给个完整的安装參考:http://www.th7.cn/Program/IOS/201405/212030.shtml  參考这个能 ...

  10. 如何在使Xcode打包iOS应用时自动增加编译号

    在红框标注的输入框中输入:真机调试编译成功增加 echo $CONFIGURATION if [ "Release" == "${CONFIGURATION}" ...