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. hdu 4027 2011上海赛区网络赛G 线段树 成段平方根 ***

    不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了 #include<cstdio> #include<iostream> # ...

  2. [荐] jQuery取得select选择的文本与值

    csdn:http://blog.csdn.net/tiemufeng1122/article/details/44154571 jquery获取select选择的文本与值获取select :获取se ...

  3. codeforces733D. Kostya the Sculptor 偏序cmp排序,数据结构hash,代码简化

    对于n==100.1,1,2或者1,2,2大量重复的形状相同的数据,cmp函数最后一项如果表达式带等于,整个程序就会崩溃 还没有仔细分析std::sort的调用过程,所以这里不是很懂..,mark以后 ...

  4. python 简单的txt文件读写

    1 读取txt文件.跟c相比,python的文件读写简直是方便的可怕 首先是读取文件 首先获得文件名称,然后通过 open函数打开文件,通过for循环逐行读出文件内容 #!python file by ...

  5. Java和数据库时间格式化格式

    JAVA: yyyy-MM-dd HH-mm-ss(大小HH表示24小时制); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd&q ...

  6. 使用J2SE API读取Properties文件的六种方法

    1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedInputStream(new FileInputStream( ...

  7. css与js后边有?v=20160101

    原文地址http://blog.csdn.net/zanychou/article/details/8813076 <span style="font-size:14px;" ...

  8. cf429B dp递推

    Description Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to loo ...

  9. 《DSP using MATLAB》为什么要z变换?

    书中截图

  10. mybaties 的一些点

    resultMap resutType mybaties缓存 待续 mybaties对应关系是bean和数据库字段的对应. 1.mybaties 的返回值是对象的话定义为resultMap=" ...