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 ...
随机推荐
- Tomcat一个BUG造成CLOSE_WAIT
之前应该提过,我们线上架构整体重新架设了,应用层面使用的是Spring Boot,前段日子因为一些第三方的原因,略有些匆忙的提前开始线上的内测了.然后运维发现了个问题,服务器的HTTPS端口有大量的C ...
- Electron使用与学习--(页面间的通信)
目录结构: index.js是主进程js. const electron = require('electron') const app = electron.app const BrowserWin ...
- 窥探Vue.js 2.0 - Virtual DOM到底是个什么鬼?
引言 你可能听说在Vue.js 2.0已经发布,并且在其中新添加如了一些新功能.其中一个功能就是"Virtual DOM". Virtual DOM是什么 在之前,React和Em ...
- angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用
今天我们要讲的是ng2的路由系统. 例子
- Nginx如何处理一个请求
看了下nginx的官方文档,其中nginx如何处理一个请求讲解的很好,现在贴出来分享下.Nginx首先选定由哪一个虚拟主机来处理请求.让我们从一个简单的配置(其中全部3个虚拟主机都在端口*:80上监听 ...
- pt-online-schema-change中update触发器的bug
pt-online-schema-change在对表进行表结构变更时,会创建三个触发器. 如下文测试案例中的t2表,表结构如下: mysql> show create table t2\G . ...
- 通过AngularJS实现前端与后台的数据对接(一)——预备工作篇
最近,笔者在做一个项目:使用AngularJS,从而实现前端与后台的数据对接.笔者这是第一次做前端与后台的数据对接的工作,因此遇到了许多问题.笔者在这些问题中,总结了一些如何实现前端与后台的数据对接的 ...
- Flexible 弹性盒子模型之CSS flex-shrink 属性
实例 让第二个元素收缩到其他元素的三分之一: 效果预览 div:nth-of-type(2){flex-shrink:3;} 浏览器支持 表格中的数字表示支持该属性的第一个浏览器的版本号. 紧跟在 - ...
- 屌丝giser成长记-大学篇
作为一名屌丝giser的我,刚接触gis专业是2007年的大一,好悲催,当时gis这个专业是被调剂的,我压根都不知道gis为何物,那时候gis冷门的一逼,报名这个专业的寥寥无几.记得那时候得知被调剂到 ...
- Android代码分析工具lint学习
1 lint简介 1.1 概述 lint是随Android SDK自带的一个静态代码分析工具.它用来对Android工程的源文件进行检查,找出在正确性.安全.性能.可使用性.可访问性及国际化等方面可能 ...