c 二叉树的使用
简单的通过一个寻找嫌疑人的小程序 来演示二叉树的使用
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /**
* 数据结构 - 二叉树 - 节点
*/
typedef struct node {
char *querstion;
struct node *no;
struct node *yes;
}node; /**
* 方法 输入和输入
* 根据打印,询问答案是否正确,y是正确
*/ int yes_no(char *question) {
printf("%s ? (y/n)",question);
char answer[];
fgets(answer, , stdin);
return answer[] == 'y';
} /**
* 根据问题创建节点
*
* @param question 问题描述
*
* @return 返回一个node
*/
node *create(char *question) {
node *n = malloc(sizeof(node));
n->querstion = strdup(question);
n->no = NULL;
n->yes = NULL;
return n;
} /**
* 释放内存
*
* @param n 节点
*/
void release(node *n) {
if (n) {
if (n -> yes) {
free(n->yes);
}
if (n -> no) {
free(n->no);
}
if (n -> querstion) {
free(n->querstion);
}
free(n);
}
} int main(int argc, const char * argv[]) { // 初始化...
char question[];
char suspect[]; // 初始化第一个数据
// 嫌疑犯如果有胡子就是张三,否则是李四
node *start = create("嫌疑犯有胡子");
start->no = create("李四");
start->yes = create("张三"); node *current;
do { current = start;
// 循环的访问节点
while () {
if (yes_no(current->querstion)) { //y
if (current->yes) {
current = current->yes;
}else {
printf("找到了嫌疑犯!\n");
break;
}
}else if (current->no) { // 跳到no 分支
current = current->no;
}else{ // 添加更多信息 // 添加嫌疑犯
printf("谁是嫌疑犯?");
fgets(suspect, , stdin);
node *yes_node = create(suspect);
current->yes = yes_node; // n
node *no_node = create(current->querstion);
current->no = no_node; // question
printf("请输入一个问题,如果正确嫌疑犯是:%s,否则嫌疑犯是:%s",suspect,current->querstion);
fgets(question, , stdin);
current->querstion = strdup(question);
break;
}
} } while (yes_no("再来一次")); release(start); return ;
}
运行程序,我们来查看打印信息

然而,表面上看这段代码没什么问题,其实有一部分存储器没事释放的,下边我们使用Valgrind工具来看一下
Valgrind 可以在这里下载http://valgrind.org/downloads/current.html#current
Valgind 是一款内存调试,内存泄露检测 和性能调优的 开源软件
使用方法是:
下载并解压好文件后 -》 cd 到文件目录 然后依次运行下边的命令
#./configure --prefix=/usr/local/webserver/valgrind
#make
#make install
可能会报这样的错误,我使用的环境是mac
make[]: *** No rule to make target `/usr/include/mach/mach_vm.defs', needed by `m_mach/mach_vmUser.c'. Stop.
make[]: *** [install-recursive] Error
make: *** [install] Error
运行下边的命令
xcode-select --install
等待一段时间后,安装完成后
./configure --disable-tls --enable-only64bit --build=amd64-darwin
make
sudo make install
ok 成功安装,接下来让我们使用Valgrind
gcc -g suspect.c -o sus
valgrind --leak-check=full ./sus
之后就能看到检测后的信息了
c 二叉树的使用的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 二叉树的递归实现(java)
这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...
- Java 二叉树遍历右视图-LeetCode199
题目如下: 题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样. 换成通俗的意思:按层遍历二叉树,输出每层的最右端结点. 这就明白时一道二叉树层序遍历的问题,用一个队 ...
- 数据结构:二叉树 基于list实现(python版)
基于python的list实现二叉树 #!/usr/bin/env python # -*- coding:utf-8 -*- class BinTreeValueError(ValueError): ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
随机推荐
- SQL Server常见数据类型介绍
数据表是由多个列组成,创建表时必须明确每个列的数据类型,以下列举SQL Server常见数据类型的使用规则,方便查阅. 1.整数类型 int 存储范围是-2,147,483,648到2,147,483 ...
- JS与APP原生控件交互
"热更新"."热部署"相信对于混合式开发的童鞋一定不陌生,那么APP怎么避免每次升级都要在APP应用商店发布呢?这里就用到了混合式开发的概念,对于电商网站尤其显 ...
- Hawk 5.1 数据导入和导出
除了一般的数据库导入导出,Hawk还支持从文件导入和导出,支持的文件类型包括: Excel CSV(逗号分割文本文件) TXT (制表符分割文本文件) Json xml Excel 目前来看,Exce ...
- Java FtpClient 实现文件上传服务
一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...
- C#项目中文件的具体含义
1.Bin 目录 用来存放编译的结果,bin是二进制binary的英文缩写,因为最初C编译的程序文件都是二进制文件,它有Debug和Release两个版本,分别对应的文件夹为bin/Debug和bin ...
- Hibernate中事务的隔离级别设置
Hibernate中事务的隔离级别,如下方法分别为1/2/4/8. 在Hibernate配置文件中设置,设置代码如下
- 真假4K电视验证:一张图足矣
国庆期间笔者逛了一下电视卖场,考虑到国内电视台以及宽带的情况,1080P至少还能用十年,所以只想要个2k电视就够了.然而事与愿违,卖场中八成的都是4k电视,清一色的4k电视让人眼花缭乱.难道4k面板技 ...
- Unable to create the selected property page. An error occurred while automatically activating bundle net.sourceforge.pmd
解决方案: 在命令行到eclipse目录下使用 eclipse.exe -clean
- StatePattern(状态模式)
/** * 状态模式 * @author TMAC-J * 状态模式和策略模式很像,其实仔细研究发现完全不一样 * 策略模式各策略之间没有任何关系,独立的 * 状态模式各状态之间接口方法都是一样的 * ...
- NodeJs支付宝移动支付签名及验签
非常感谢 :http://www.jianshu.com/p/8513e995ff3a?utm_campaign=hugo&utm_medium=reader_share&utm_co ...