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++排序二叉树的更多相关文章

  1. 纪念逝去的岁月——C/C++选择排序

    选择排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  2. 纪念逝去的岁月——C++实现一个队列(使用类模板)

    1.代码 2.运行结果 1.代码 #include <stdio.h> #include <string.h> template <typename T> clas ...

  3. 纪念逝去的岁月——C++实现一个栈(使用类模板)

    这个版本是上个版本的加强版,上个版本的代码:http://www.cnblogs.com/fengbohello/p/4542912.html 目录 1.代码 2.运行结果 1.代码 1.1 调试信息 ...

  4. 纪念逝去的岁月——C++实现一个栈

    1.代码 2.运行结果 1.代码 stack.cpp #include <stdio.h> #include <string.h> class ClsStack { priva ...

  5. 纪念逝去的岁月——C/C++二分查找

    代码 #include <stdio.h> int binarySearch(int iList[], int iNum, int iX, int * pPos) { if(NULL == ...

  6. 纪念逝去的岁月——C/C++快速排序

    快速排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  7. 纪念逝去的岁月——C/C++交换排序

    交换排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  8. 纪念逝去的岁月——C/C++冒泡排序

    冒泡排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  9. 纪念逝去的岁月——C/C++字符串回文

    判断字符串是否是回文: 1. 输入:hello world dlrow olleh 输出:1 2. 输入:nihao hello 输出:0 代码 #include <stdio.h> #i ...

随机推荐

  1. scala的tcp通信

    client: object ActorClient extends App { import actors.Actor, actors.remote.Node, actors.remote.Remo ...

  2. VS2015 Xamarin for iOS

    VS2015环境配置 VS2015安装不多说.其实Xamarin 和微软感觉并不是什么好基友,Xamarin以前一直像个可怜的娃,以插件的形式寄生于VS中.现在只不过形势稍微好点了,VS2015 在明 ...

  3. PS 零基础训练1

    背景色:Alt + Del 前景色:Ctrl + Del 快捷键: 更换工具栏里的第二项:Shift + W or Shift + C  ... 缩放:Ctrl + = or Ctrl + - 工具笔 ...

  4. WPF之MVVM(Step3)——使用Prism(1)

    使用WPF-MVVM开发时,自己实现通知接口.DelegateCommand相对来说还是用的较少,我们更多的是使用第三方的MVVM框架,其中微软自身团队提供的就有Prism框架,此框架功能较多,本人现 ...

  5. [Liferay6.2]Connect to ajax.googleapis.com …… timed out

    启动liferay 6.2 tomcat之后,后台会报一大段的异常信息,主要异常信息如下: -- :: org.apache.shindig.gadgets.http.BasicHttpFetcher ...

  6. NuGet学习笔记(2) 使用图形化界面打包自己的类库

    上文NuGet学习笔记(1) 初识NuGet及快速安装使用说到NuGet相对于我们最重要的功能是能够搭建自己的NuGet服务器,实现公司内部类库的轻松共享更新.在安装好NuGet扩展后,我们已经能够通 ...

  7. Arduino101学习笔记(十二)—— 101定时器中断

    一.API 1.开定时器中断 //*********************************************************************************** ...

  8. Hibernate入门案例 增删改

    一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...

  9. JVM参数调优

    JVM参数调优 JVM参数调优是一个很头痛的问题,可能和应用有关系,下面是本人一些调优的实践经验,希望对读者能有帮助,环境LinuxAS4,resin2.1.17,JDK6.0,2CPU,4G内存,d ...

  10. 3、利用SuperObject 循环处理Json深层次的值

    //遍历对象 procedure TForm1.Button5Click(Sender: TObject); var item,jo: ISuperObject; ja,JA_TYPE,JA_MAC: ...