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 ...
随机推荐
- C++对C的函数拓展
一,内联函数 1.内联函数的概念 C++中的const常量可以用来代替宏常数的定义,例如:用const int a = 10来替换# define a 10.那么C++中是否有什么解决方案来替代宏代码 ...
- 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo
有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...
- 【原】FMDB源码阅读(一)
[原]FMDB源码阅读(一) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 说实话,之前的SDWebImage和AFNetworking这两个组件我还是使用过的,但是对于 ...
- 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程
本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例 说明:1.本文暂未使用maven集成,jar包需要手动导入. 2.本文为基础教程,大神切勿见笑. 3.如果对您学习有帮助,欢迎各种转载,注 ...
- 挑子学习笔记:特征选择——基于假设检验的Filter方法
转载请标明出处: http://www.cnblogs.com/tiaozistudy/p/hypothesis_testing_based_feature_selection.html Filter ...
- 《JavaScript设计模式与开发实践》整理
最近在研读一本书<JavaScript设计模式与开发实践>,进阶用的. 一.高阶函数 高阶函数是指至少满足下列条件之一的函数. 1. 函数可以作为参数被传递. 2. 函数可以作为返回值输出 ...
- 【NLP】干货!Python NLTK结合stanford NLP工具包进行文本处理
干货!详述Python NLTK下如何使用stanford NLP工具包 作者:白宁超 2016年11月6日19:28:43 摘要:NLTK是由宾夕法尼亚大学计算机和信息科学使用python语言实现的 ...
- Javascript正则对象方法与字符串正则方法总结
正则对象 var reg = new Regexp('abc','gi') var reg = /abc/ig 正则方法 test方法(测试某个字符串是否匹配) var str = 'abc123'; ...
- bzoj3208--记忆化搜索
题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目. 我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...
- Node.js入门(一)
一.Node.js本质上是js的运行环境. 二.可以解析js代码(没有浏览器安全级的限制): 提供系统级的API:1.文件的读写 2.进程的管理 3.网络通信 三.可以关注的四个网站: 1.https ...