代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
比较树
作者:
false
/ 发布于
2012/3/6
/
720
#include <stdio.h></div> #include <string.h></div> #include <stdlib.h></div> <div></div> <div>define bool int</div> <div>define true 1</div> <div>define false 0</div> <div></div> <div>typedef struct Node *Tree;<span class="Apple-tab-span" style="white-space:pre"> </span>/*自定义树节点指针数据类型*/</div> <div></div> <div>Tree InsertX(int x,Tree T);</div> <div>/*****************************</div> <div>功能:用于向树中插入元素</div> <div>输入:待插入元素,树所在地址</div> <div>输出:插入后树的地址</div> <div>*****************************/</div> <div>bool Compare(Tree T, Tree X);</div> <div>/*****************************</div> <div>功能:比较两棵树是否完全相同</div> <div>输入:待比较的两棵树的头地址</div> <div>输出:不同时输出0,相同时输出1</div> <div>*****************************/</div> <div>Tree MakeEmpty(Tree T);</div> <div>/*****************************</div> <div>功能:清空树</div> <div>输入:待清空树的头地址</div> <div>输出:空树指针</div> <div>*****************************/</div> <div></div> <div>/*树节点结构体声明*/</div> <div>typedef struct Node</div> <div>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>int element;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Tree Left;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Tree Right;</div> <div>}TreeNode;</div> <div></div> <div>Tree InsertX(int x, Tree T)</div> <div>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if(T == NULL)<span class="Apple-tab-span" style="white-space:pre"> </span>/*开始构建树*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>T = (Tree)malloc(sizeof(TreeNode));</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if(T == NULL)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>printf("Out of space!");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>exit(0);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>else<span class="Apple-tab-span" style="white-space:pre"> </span>/*插入元素*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>T->element = x;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>T->Right = T->Left = NULL;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>else if(x < T->element)<span class="Apple-tab-span" style="white-space:pre"> </span>/*判断插入位置为左*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>T->Left = InsertX(x, T->Left);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>else if(x > T->element)<span class="Apple-tab-span" style="white-space:pre"> </span>/*判断插入位置为右*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>T->Right = InsertX(x, T->Right);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>return T;<span class="Apple-tab-span" style="white-space:pre"> </span>/*返回树的头指针*/</div> <div>} <div></div> <div>bool Compare(Tree T, Tree X)</div> <div>{</div> <div> if(T == NULL && X == NULL)<span class="Apple-tab-span" style="white-space:pre"> </span>/*两棵树都不存在或者迭代到对应树叶的位置*/</div> <div> {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> return true;</div> <div> } <div> else if(T->element != X->element)/*两棵树相同位置元素不同*/</div> <div> {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> return false;</div> <div> } <div> else<span class="Apple-tab-span" style="white-space:pre"> </span>if(Compare(T->Left, X->Left) && Compare(T->Right, X->Right)) /*迭代比较左子树,右子树*/</div> <div> {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return true;</div> <div> } <div>} <div></div> <div>Tree MakeEmpty(Tree T)</div> <div>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if(T != NULL)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>MakeEmpty(T->Left);<span class="Apple-tab-span" style="white-space:pre"> </span>/*迭代清空*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>MakeEmpty(T->Right);<span class="Apple-tab-span" style="white-space:pre"> </span>/*迭代清空*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>free(T);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>return NULL;</div> <div>} <div></div> <div>int main(void)</div> <div>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>int n, a, i, j, x;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>char* output[2]={"No","Yes"};<span class="Apple-tab-span" style="white-space:pre"> </span>/*输出结果*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Tree X = NULL,</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> T = NULL;<span class="Apple-tab-span" style="white-space:pre"> </span>/*设置初始值*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>while(1)<span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>T = MakeEmpty(T);<span class="Apple-tab-span" style="white-space:pre"> </span>/*清空树释放内存*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>X = MakeEmpty(X);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>scanf("%d", &n);<span class="Apple-tab-span" style="white-space:pre"> </span>/*输入n,n=0则退出*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if(n == 0)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>break;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>scanf("%d", &a);<span class="Apple-tab-span" style="white-space:pre"> </span>/*输入比较次数*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>for(i = 0; i < n; i++)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>scanf("%d", &x);<span class="Apple-tab-span" style="white-space:pre"> </span>/*输入待插入值*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>T = InsertX(x, T);<span class="Apple-tab-span" style="white-space:pre"> </span>/*向树中插入元素*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>for(j = 0; j < a; j++)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>X = MakeEmpty(X);<span class="Apple-tab-span" style="white-space:pre"> </span>/*清空树进行新一次测试*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>for(i = 0; i < n; i++)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>scanf("%d", &x);<span class="Apple-tab-span" style="white-space:pre"> </span>/*输入待插入值*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>X = InsertX(x, X);<span class="Apple-tab-span" style="white-space:pre"> </span>/*向树中插入元素*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>printf("%s\n", output[Compare(T, X)]);/*比较标本树和待比较树并输出结果*/</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>return 0;</div> <div>}
试试其它关键字
比较树
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
false
贡献的其它代码
(
2
)
.
比较树
.
使用ctypes调用libcaptcha生成验证码
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3