二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)
将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作:
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语言版)的更多相关文章
- java实现二叉树的相关操作
import java.util.ArrayDeque; import java.util.Queue; public class CreateTree { /** * @param args */ ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...
- 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树: SDUT 1489 Description 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input 输入数据有多组,第一行是一个整数t (t& ...
- 【C&数据结构】---关于链表结构的前序插入和后序插入
刷LeetCode题目,需要用到链表的知识,忽然发现自己对于链表的插入已经忘得差不多了,以前总觉得理解了记住了,但是发现真的好记性不如烂笔头,每一次得学习没有总结输出,基本等于没有学习.连复盘得机会都 ...
- 【11】-java递归和非递归二叉树前序中序后序遍历
二叉树的遍历 对于二叉树来讲最主要.最基本的运算是遍历. 遍历二叉树 是指以一定的次序访问二叉树中的每个结点.所谓 访问结点 是指对结点进行各种操作的简称.例如,查询结点数据域的内容,或输出它的值,或 ...
随机推荐
- Android ArryaList 笔记
Arraylist相当于动态数组,可以动态的添加或者删除其中的元素. 参考链接 http://beginnersbook.com/2013/12/java-arraylist/ package com ...
- 第三百零八节,Django框架,models.py模块,数据库操作——链表结构,一对多、一对一、多对多
第三百零八节,Django框架,models.py模块,数据库操作——链表结构,一对多.一对一.多对多 链表操作 链表,就是一张表的外键字段,连接另外一张表的主键字段 一对多 models.Forei ...
- 应当将指针变量用“==”或“!=”与 NULL 比较
应当将指针变量用“==”或“!=”与 NULL 比较. 指针变量的零值是“空”(记为 NULL). 尽管 NULL 的值与 0 相同,但是两者意义不 同. 假设指针变量的名字为 p,它与零值比较的标准 ...
- 【Java 线程的深入研究1】Java 提供了三种创建线程的方法
Java 提供了三种创建线程的方法: 通过实现 Runnable 接口: 通过继承 Thread 类本身: 通过 Callable 和 Future 创建线程. 1.通过实现 Runnable 接口来 ...
- ssm框架搭建并演示获取json字符串
为方便起见,使用oracle自带的scott账户中的emp表作为数据源. 预期效果: 1.输入URL:http://localhost:8888/ssm/getEmp?empno=7369 2.返回结 ...
- Oracle 11g 的bug?: aix 上,expdp 11.2.0.1 导出,impdp 11.2.0.3 导入,Interval 分区的 【Interval】 分区属性成了【N】
如题: Oracle 11g 的bug?: aix 上,expdp 11.2.0.1 导出,impdp 11.2.0.3 导入,Interval 分区的 [Interval] 分区属性成了[N] 谨记 ...
- windows 2008 r2 安装TabsStudio
windows 2008 r2 安装TabsStudio 办法如下: HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer.如果没有这个项,则新建这个项 ...
- Oracle中给用户赋予debug权限
通过可视化工具(如PL/SQL Developer.Oracle SQL Developer)调试Oracle的存储过程时,如果遇到如下错误信息:...ORA-01031: insufficient ...
- HDU2717BFS
/* WA了12发简直不能忍! . 题意非常简单.从正整数a变为b有三种方法: +1,-1.*2 特殊情况一:a与b相等不须要搜索 特殊情况二:a>b时.结果必定是a-b不需搜 特殊情况三:比較 ...
- Android 中如何从一个App启动另外一个App(如启动支付界面、启动地图界面、应用商场下载App等场景)
假定两个App,分别是A和B,当A运行某个功能需要启动B,一种是启动B应用,一种直接进入B的某个Activity.搜了很多资料,没有一个完整的.下面就A--Android5.1.1.B--Androi ...