纪念逝去的岁月——C/C++排序二叉树
1、代码
2、运行结果
3、分析
1、代码
#include <stdio.h>
#include <stdlib.h> typedef struct _Node
{
int value;
struct _Node * pLeft;
struct _Node * pRight;
} Node; Node * getNewNode(int iValue)
{
Node * p = (Node *)malloc(sizeof(Node));
if(NULL != p)
{
p->value = iValue;
p->pLeft = NULL;
p->pRight = NULL;
} return p;
} void deleteNode(Node * p)
{
if(NULL != p)
{
free(p);
}
} int addElm(Node * p, int value)
{
Node * pX = p;
while(pX)
{
if(value < pX->value)
{
if(NULL == pX->pLeft)
{
pX->pLeft = getNewNode(value);
printf("add [%2d] to left of [%2d]\n", value, pX->value);
break;
}
else
{
pX = pX->pLeft;
}
}
else
{
if(NULL == pX->pRight)
{
pX->pRight = getNewNode(value);
printf("add [%2d] to right of [%2d]\n", value, pX->value);
break;
}
else
{
pX = pX->pRight;
}
}
} return ;
} Node * makeBinaryTree(int iList[], int iNum)
{
Node * p = getNewNode(iList[]);
int i = ;
for(i = ; i < iNum; i++)
{
addElm(p, iList[i]);
}
printf("\n"); return p;
} void destroyBinaryTree(Node * p)
{
if(NULL == p)
{
return;
}
destroyBinaryTree(p->pLeft);
destroyBinaryTree(p->pRight);
deleteNode(p);
} int preorderTraversal(Node * p)
{
if(NULL == p)
{
return -;
}
printf("%2d ", p->value);
preorderTraversal(p->pLeft);
preorderTraversal(p->pRight);
return ;
} int postorderTraversal(Node * p)
{
if(NULL == p)
{
return -;
}
postorderTraversal(p->pLeft);
postorderTraversal(p->pRight);
printf("%2d ", p->value);
return ;
} int inorderTraversal(Node * p)
{
if(NULL == p)
{
return -;
}
inorderTraversal(p->pLeft);
printf("%2d ", p->value);
inorderTraversal(p->pRight);
return ;
} void preTrvl(Node * p)
{
printf("pre : ");
preorderTraversal(p);
printf("\n");
} void postTrvl(Node * p)
{
printf("post : ");
postorderTraversal(p);
printf("\n");
} void inTrvl(Node * p)
{
printf("in : ");
inorderTraversal(p);
printf("\n");
} void printList(int iList[], int iNum)
{
for(int i = ; i < iNum; i++)
{
printf("%d ", iList[i]);
}
printf("\n");
} int main()
{
int iList[] = {, , , , , , , , , , , };
int iNum = ; printList(iList, iNum);
Node * p = makeBinaryTree(iList, iNum);
preTrvl(p);
postTrvl(p);
inTrvl(p);
destroyBinaryTree(p);
}
2、运行结果
$ ./binaryTree add [ ] to right of [ ]
add [ ] to left of [ ]
add [ ] to left of [ ]
add [ ] to right of [ ]
add [ ] to left of [ ]
add [ ] to left of [ ]
add [ ] to left of [ ]
add [] to right of [ ]
add [ ] to left of [ ]
add [] to left of []
add [] to right of [] pre :
post :
in :
3、分析
从运行结果的第三行开始,就是开始进行数据插入的地方,下面对运行结果中,每一行插入动作后二叉树的情况进行画图描述。
第03行:add [ 9] to right of [ 6] 第04行:add [ 8] to left of [ 9]

第05行:add [ 3] to left of [ 6] 第06行:add [ 5] to right of [ 3]

第07行:add [ 4] to left of [ 5] 第08行:add [ 7] to left of [ 8]

第09行:add [ 2] to left of [ 3] 第10行:add [12] to right of [ 9]

第11行:add [ 1] to left of [ 2]
第12行:add [10] to left of [12]

第13行:add [11] to right of [10]

纪念逝去的岁月——C/C++排序二叉树的更多相关文章
- 纪念逝去的岁月——C/C++选择排序
选择排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
- 纪念逝去的岁月——C++实现一个队列(使用类模板)
1.代码 2.运行结果 1.代码 #include <stdio.h> #include <string.h> template <typename T> clas ...
- 纪念逝去的岁月——C++实现一个栈(使用类模板)
这个版本是上个版本的加强版,上个版本的代码:http://www.cnblogs.com/fengbohello/p/4542912.html 目录 1.代码 2.运行结果 1.代码 1.1 调试信息 ...
- 纪念逝去的岁月——C++实现一个栈
1.代码 2.运行结果 1.代码 stack.cpp #include <stdio.h> #include <string.h> class ClsStack { priva ...
- 纪念逝去的岁月——C/C++二分查找
代码 #include <stdio.h> int binarySearch(int iList[], int iNum, int iX, int * pPos) { if(NULL == ...
- 纪念逝去的岁月——C/C++快速排序
快速排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
- 纪念逝去的岁月——C/C++交换排序
交换排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
- 纪念逝去的岁月——C/C++冒泡排序
冒泡排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
- 纪念逝去的岁月——C/C++字符串回文
判断字符串是否是回文: 1. 输入:hello world dlrow olleh 输出:1 2. 输入:nihao hello 输出:0 代码 #include <stdio.h> #i ...
随机推荐
- 攻城狮在路上(壹) Hibernate(四)--- 对象标识符(OID)生成机制
Hibernate使用对象标识符(OID)来建立内存中对象和数据库表中记录的对应关系,对象的OID和数据库的主键对应.为了保证OID的唯一性和不可变性,应该让Hibernate来为OID赋值.Hibe ...
- android 入门-控件 测量状态栏高度
private ViewTreeObserver viewTreeObserver; /** 获取可見区域高度 **/ WindowManager manager = getWindowManager ...
- JQuery.Ajax()的data参数类型
假如现在有这样一个表单,是添加元素用的. <form id='addForm' action='UserAdd.action' type='post'> <label for='un ...
- .net Session 详解
(一) 描述当用户在 Web 应用程序中导航 ASP.NET 页时,ASP.NET 会话状态使您能够存储和检索用户的值.HTTP 是一种无状态协议.这意味着 Web 服务器会将针对页面的每个 HTTP ...
- C#复习、面向对象阶段开始
C#复习:在控制台程序中使用结构体.集合,完成下列要求项目要求:一.连续输入5个学生的信息,每个学生都有以下4个内容:1.序号 - 根据输入的顺序自动生成,不需要手动填写,如输入第一个学生的序号是1, ...
- 【jQuery 使用】 利用jQuery.prop("outerHTML")获取包含自身在内的HTML元素的HTML代码
jQuery.html() 是获取当前节点下的html代码,并不包含当前节点本身的代码,然而我们有时候的确需要,可以通过jQuery.prop("outerHTML")的方式设置. ...
- SoapUI之webservice接口测试(一)
1.新建soap project 添加后出现接口内容 2.为了方便后续的测试,以防某些参数删除错了,这边需要新建测试集 3.点开新建的测试集可以发现,里面的内容跟原始测试集内容是一样的 然后就可以在这 ...
- HTTP 请求方式: GET和POST的比较(转)
GET和POST是HTTP的两个常用方法. 什么是HTTP? 超文本传输协议(HyperText Transfer Protocol -- HTTP)是一个设计来使客户端和服务器顺利进行通讯的协议 ...
- Angular JS 学习之过滤器
1.过滤器可以使用一个管道字符(|)添加到表达式和指令中: 2.AngularJS过滤器可用于转换数据: **currency:格式化数字为货币格式: **filter:从数组项中选择一个子集: ** ...
- css3 总结02
盒子尺寸 .border-box{ -webkit-box-sizing: border-box; -moz-box-sizing: border-box; border-sizing: border ...