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 ...
随机推荐
- 谈谈JS中的函数节流
好吧,一直在秋招中,都没怎么写博客了...今天赶紧来补一补才行...我发现,在面试中,讲到函数节流好像可以加分,尽管这并不是特别高深的技术,下面就聊聊吧! ^_^ 备注:以下内容部分来自<Jav ...
- .Net语言 APP开发平台——Smobiler学习日志:如何快速在手机上实现ContextMenu
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 样式一 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的&qu ...
- Angular2 Hello World 之 2.0
最近angular2正式版发布了,现在就趁热接着记录一下2.0版的Hello World.据说由RC6升级到2.0代码改动不是很大,在写的时候也感觉改动不是很大,这次记录还是以asp.net core ...
- Android—应用程序开机自启
android开机时候会发送开机广播,我们想要收到广播知道手机开机,才能启动我们的应用程序. 首先要在配置文件中添加相应权限: <uses-permission android:name=&qu ...
- mysql 赋予用户权限
# 赋予权限MySQL> grant 权限参数 on 数据库名称.表名称 to 用户名@用户地址 identified by '用户密码'; # 立即生效权限MySQL> flush pr ...
- 使用apache自带日志分割模块rotatelogs,分割日志
rotatelogs 是 Apache 2.2 中自带的管道日志程序,参数如下(参见:http://lamp.linux.gov.cn/Apache/ApacheMenu/programs/rotat ...
- 怎样在Dos里切换盘符
一:在Dos里切换盘符 a:在电脑左下角右击显示图片;(我用的是win10系统,其他系统类似) b:点击运行,输入cmd; c:点击确定: d:输入盘符:(如f:) 或F: 只写字母,不写分号是不行的 ...
- photoshop:无法完成请求 因为暂存盘已满
今天photoshop打开一个问题,提醒:无法完成请求因为暂存盘已满 不用担心这个问题很好解决可能是你做的图比较大并不需要清理C盘空间 选择:编辑→首选项→暂存盘 设置第一暂存盘为D盘或E盘 总之 第 ...
- thinkphp-无限分类下根据任意部门获取顶级部门ID
根据所得到的部门编号获取顶级部门ID: 参数 - department_id 表格组织架构: tab_departments department_id parent_id name 1 1 顶级 2 ...
- 札记:Java异常处理
异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...