将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作:

1、定义树的结构体:

 typedef struct TreeNode{
int data;
struct TreeNode *left;
struct TreeNode *right;
}TreeNode;

2、创建根节点:

 TreeNode *creatRoot(){
TreeNode * root =(TreeNode *)malloc(sizeof(TreeNode));
if(NULL==root){
printf("CreatRoot is failing!\n");
return NULL;
}
root->left=NULL;
root->right=NULL;
printf("Input the data of root!\n");
scanf("%d",&root->data);
printf("Root created!\n");
return root;
}

3、申请了空间一定要记得释放空间,否则后果不堪设想

 void clearTree(TreeNode * root){
if(root){
clearTree(root->left);
clearTree(root->right);
free(root);
}
}

4、选择插入的节点是根节点的左孩子还是右孩子

 void addSubTreeNode(TreeNode* root,TreeNode*node,int select){
if(NULL==root){
printf("Root is NULL\n");
return ;
}
switch(select){
case :
if(root->left){
printf("The root has left subtree!\n");
return ;
}else{
root->left=node;
break;
}
case :
if(root->right){
printf("The root has right subtree!\n");
return ;
}else{
root->right=node;
break;
}
default:
printf("The input of select is wrong!\n");
}
}

5、二叉树的前序、中序、后序遍历

 void DLR(TreeNode * root){
if(NULL==root)
return;
printf("The First root input %d\n",root->data);
DLR(root->left);
DLR(root->right);
} void LDR(TreeNode * root){
if(NULL==root)
return;
LDR(root->left);
printf("The Middle root input %d\n",root->data);
LDR(root->right);
} void LRD(TreeNode * root){
if(NULL==root)
return;
LRD(root->left);
LRD(root->right);
printf("The Last root input %d\n",root->data);
}

6、求二叉树的深度

 int lengthTree(TreeNode *root){
int lenLeft,lenRight;
if(NULL==root)
return ;
else{
lenLeft=lengthTree(root->left);
lenRight=lengthTree(root->right);
if(lenLeft<lenRight)
return lenRight+;
else
return lenLeft+;
}
}

7、查找节点,这里是为了初始化构建二叉树时选择的函数,目的是找出你要插入节点的父亲节点

 TreeNode *treeFind(TreeNode *node,int number){
TreeNode *p;
if(NULL==node)
return NULL;
else{
if(node->data==number){
return node;
}else{
if(p=treeFind(node->left,number)){
printf("Parent found!\n");
return p;
} else if(p=treeFind(node->right,number)){
printf("Parent found!\n");
return p;
}
else
return NULL;
}
}
}

8、找到父亲节点后,选择是插入做孩子还是右孩子

 void addNode(TreeNode *root){
TreeNode *node,*parent;
int number,select;
printf("Please input the parent data!\n");
scanf("%d",&number);
parent=treeFind(root,number);
if(NULL==parent){
printf("parent is not found!\n");
return ;
}
printf("Please choice the operation: 0 Exit; 1 insert left subtree; 2 insert right subtree!\n");
scanf("%d",&select);
if(select==)
return ;
if(node=(TreeNode*)malloc(sizeof(TreeNode))){
printf("Please input the data of BinTreeData that you want to insert!\n");
scanf("%d",&node->data);
node->left=NULL;
node->right=NULL;
switch(select){
case :
addSubTreeNode(parent,node,);
break;
case :
addSubTreeNode(parent,node,);
break;
}
}
}

9、二叉树的层次遍历

 void levelFind(TreeNode * root){
TreeNode *node;
TreeNode *Queue[MAX];
int head,tail;
head=tail=;
if(root==NULL){
printf("Queue is empty!\n");
return ;
}
if((tail+)%MAX!=head)
Queue[tail++]=root;
else
printf("Queue is full!\n");
if(head!=tail)
node=Queue[head++];
else
printf("Queue is empty!\n");
printf("The bintree level find is: \n");
while(NULL!=node){
printf("%d ",node->data);
if(node->left!=NULL){
Queue[tail++]=node->left;
}
if(node->right!=NULL){
Queue[tail++]=node->right;
}
if(head==tail){
printf("Queue is empty!\n");
return;
}
node=Queue[head++];
}
}

10、主函数

 int main(){
TreeNode *root=NULL;
int n;
do{
printf("1 set root, 2 addNode, 3 the First root input, 4 the Middle root input, 5 the last root input, 6 according to level find, 7 caculate the depth, 0 exit!\n");
scanf("%d",&n);
switch(n){
case :
break;
case :
if(NULL==root)
root=creatRoot();
else
printf("The root is exist!\n");
break;
case :
addNode(root);
break;
case :
DLR(root);
break;
case :
LDR(root);
break;
case :
LRD(root);
break;
case :
levelFind(root);
break;
case :
printf("the depth of tree is %d\n",lengthTree(root));
break;
}
}while(n!=);
clearTree(root);
printf("Tree are cleared all!\n");
getch();
return ; }

二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)的更多相关文章

  1. java实现二叉树的相关操作

    import java.util.ArrayDeque; import java.util.Queue; public class CreateTree { /** * @param args */ ...

  2. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  4. SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...

  5. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

  6. 前序+中序->后序 中序+后序->前序

    前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...

  7. 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树

    已知 中序&后序  建立二叉树: SDUT 1489 Description  已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input  输入数据有多组,第一行是一个整数t (t& ...

  8. 【C&数据结构】---关于链表结构的前序插入和后序插入

    刷LeetCode题目,需要用到链表的知识,忽然发现自己对于链表的插入已经忘得差不多了,以前总觉得理解了记住了,但是发现真的好记性不如烂笔头,每一次得学习没有总结输出,基本等于没有学习.连复盘得机会都 ...

  9. 【11】-java递归和非递归二叉树前序中序后序遍历

    二叉树的遍历 对于二叉树来讲最主要.最基本的运算是遍历. 遍历二叉树 是指以一定的次序访问二叉树中的每个结点.所谓 访问结点 是指对结点进行各种操作的简称.例如,查询结点数据域的内容,或输出它的值,或 ...

随机推荐

  1. easyui datagrid列拖拽

    <script type="text/javascript"> var cols = [{ field: 'testName', title: '<span cl ...

  2. TopK的一个简单实现

    转自:http://rangerwolf.iteye.com/blog/2119096 题外话: <Hadoop in Action> 是一本非常不错的交Hadoop的入门书,而且建议看英 ...

  3. fanqiang_bak

  4. nginx反向代理压测问题记录

    使用nginx反向代理压测web程序,100个用户并发时,每隔一段时间loadrunner工具中就会报错,报错信息如下: Continuing after Error -26610: HTTP Sta ...

  5. samtools flags 的含义

    对于双端比对的数据,生成的BAM文件中,R1端序列和R2端序列的标识符是一样的,之前一直不知道如何根据bam文件区分哪条序列是R1端,哪条序列是R2端,昨天仔细研究了一下,原来代表R1端和R2端的信息 ...

  6. jquery获取html元素的绝对位置和相对位置的方法

    绝对位置坐标: 代码如下: $("#elem").offset().top$("#elem").offset().left 相对父元素的位置坐标: 代码如下: ...

  7. mysqldump工具,通过--where选项,导出指定表中指定数据?

    需求描述: 今天在使用mysqldump工具导出表的时候,考虑能不能导出满足条件的数据行,不要 将表都导出来,查找资料,通过--where选项,就可以实现目的,做个实验,在此记录下. 操作过程: 1. ...

  8. 视觉SLAM漫淡

    视觉SLAM漫谈 1.    前言 开始做SLAM(机器人同时定位与建图)研究已经近一年了.从一年级开始对这个方向产生兴趣,到现在为止,也算是对这个领域有了大致的了解.然而越了解,越觉得这个方向难度很 ...

  9. 将Spring源代码导入eclipse步骤

    深入学习spring.研读源代码是必须的~ 1.到https://github.com/spring-projects/spring-framework/releases去找自己须要的spring版本 ...

  10. WCF简单案例

    1,定义接口层,引用System.ServiceModel namespace Contracts { [ServiceContract(Name = "CalculatorService& ...