二叉树的基本操作和遍历
2015-05-20 21:03
176 查看
下面将介绍二叉树的基本操作和先序中序后序遍历
先看看二叉树基本定义:
![](https://img-blog.csdn.net/20150520205945841?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWFhYWF3YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20150520210454441?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWFhYWF3YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
然后测试
先看看二叉树基本定义:
#include "stdio.h" #include "stdlib.h" #define Max 50 typedef char DATA;//定义元素类型 typedef struct ChainTree//定义二叉树结点类型 { DATA data;//元素数据 struct ChainTree *left;//左子树结点指针 struct ChainTree *right;//右子树结点指针 }ChainBinTree; //初始化二叉树 ChainBinTree *ChainBinTreeInit(ChainBinTree *node) { if(node!=NULL) return node; else return NULL; } int ChainBinTreeAddNode(ChainBinTree *bt,ChainBinTree*node,int n) //添加数据到二叉树 //bt为父结点,node为子结点,n=1表示添加左子树,n=2表示添加右子树 { if(bt==NULL) { printf("父结点不存在,请先设置"); return 0; } switch(n) { case 1://添加到左结点 if(bt->left)//左子树不为空 { printf("左子树不为空!\n"); return 0; } else { bt->left = node; break; } case 2://添加到右结点 if(bt->right)//右子树不为空 { printf("右子树不为空!\n"); return 0; } else { bt->right = node; break; } default: printf("参数错误"); return 0; } } //返回左结点 ChainBinTree *ChainBinTreeLeft(ChainBinTree *bt) { if(bt) { return bt->left; } else return NULL; } //返回右结点 ChainBinTree *ChainBinTreeRight(ChainBinTree *bt) { if(bt) { return bt->right; } else return NULL; } //检查二叉树是否为空,为空则返回1,否者返回0; int ChainBinTreeEmpty(ChainBinTree *bt) { if(bt) { return 0; } else return 1; } //求二叉树深度 int ChainBinTreeDepth(ChainBinTree *bt) { int dep1,dep2; if(bt==NULL) { return 0;//空树为0 } else { dep1 = ChainBinTreeDepth(bt->left); dep2 = ChainBinTreeDepth(bt->right); if(dep1>dep2) return dep1+1; else return dep2+1; } } //在二叉树中查找值为data的结点 ChainBinTree *ChainBinTreeFind(ChainBinTree *bt,DATA data) { ChainBinTree *p; if(bt==NULL) { return NULL; } else { if(bt->data==data) return bt; else { if(p=ChainBinTreeFind(bt->left,data)) return p; else if(p=ChainBinTreeFind(bt->right,data)) return p; else return NULL; } } } //清空二叉树 void ChainBinTreeClear(ChainBinTree *bt) { if(bt) { ChainBinTreeClear(bt->left); ChainBinTreeClear(bt->right); free(bt); bt=NULL; } return ; } //先序遍历 void ChainBinTree_DLR(ChainBinTree *bt,void(*oper)(ChainBinTree *p)) { if(bt)//树不为空执行 { oper(bt); ChainBinTree_DLR(bt->left,oper); ChainBinTree_DLR(bt->right,oper); } } //中序遍历 void ChainBinTree_DLR1(ChainBinTree *bt,void(*oper)(ChainBinTree *p)) { if(bt)//树不为空执行 { ChainBinTree_DLR(bt->left,oper); oper(bt); ChainBinTree_DLR(bt->right,oper); } } //后序遍历 void ChainBinTree_DLR2(ChainBinTree *bt,void(*oper)(ChainBinTree *p)) { if(bt)//树不为空执行 { ChainBinTree_DLR(bt->left,oper); ChainBinTree_DLR(bt->right,oper); oper(bt); } }
然后测试
#include "stdio.h" #include "ChainBinTree.c" void oper(ChainBinTree *p) { printf("%c ",p->data); return ; } ChainBinTree *InitRoot()//初始化二叉树的根 { ChainBinTree *node; if(node = (ChainBinTree *)malloc(sizeof(ChainBinTree))) { printf("\n输入根结点数据"); scanf("%s",&node->data); node->left=NULL; node->right=NULL; return ChainBinTreeInit(node); } return NULL; } void AddNode(ChainBinTree *bt) { ChainBinTree *node ,*parent; DATA data; char select; if(node = (ChainBinTree *)malloc(sizeof(ChainBinTree))) { printf("\n输入二叉树结点数据"); fflush(stdin); scanf("%s",&node->data); node->left=NULL; node->right=NULL; printf("输入父结点数据:"); fflush(stdin); scanf("%s",&data); parent = ChainBinTreeFind(bt,data);//查找制定数据节点 if(!parent) { printf("未找到父结点"); free(node); return; } printf("1,添加到左子树\n2.添加到右子树"); do{ select = getch(); select-= '0'; if(select==1 ||select==2) { ChainBinTreeAddNode(parent,node,select);//添加结点到二叉树 } } while(select!=1 &&select!=2) ; }return; } int main() { ChainBinTree *root = NULL; char select; void (*oper1)();//指向函数的指针 oper1=oper; do{ printf("\n1.设置二叉树元素 2.添加二叉树结点" ); printf("\n3.先序遍历 4.中序遍历" ); printf("\n5.后序遍历 6.二叉树深度" ); printf("\n0.退出 \n "); select=getch(); switch(select) { case '1': root = InitRoot(); break; case '2': AddNode(root); break; case '3': printf("先序"); ChainBinTree_DLR(root,oper1); printf("\n"); break; case '4': printf("中序"); ChainBinTree_DLR1(root,oper1); printf("\n"); break; case '5': printf("后序"); ChainBinTree_DLR2(root,oper1); printf("\n"); break; case '6': printf("\n深度:%d\n",ChainBinTreeDepth(root)); break; case '0': break; } }while(select!=0); ChainBinTreeClear(root); root = NULL; while(1); return 0 ; }按下图的顺序依次插入结点最后达到目的:
相关文章推荐
- 二叉树的基本操作(一)——二叉树的遍历
- c++模板实现二叉树,线索化,线索化遍历,非递归遍历及一些基本操作
- 二叉树基本操作--创建,三种遍历,叶子节点
- 二叉树学习总结:二叉树的基本操作、遍历二叉树、中序线索化二叉树、中序遍历线索二叉树
- 二叉树的基本操作(创建、递归和非递归遍历、求深度、求叶子数)
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- 二叉树的基本操作(定义、遍历、高度、生成)【数据结构】
- 二叉树的基本操作及遍历
- 二叉树的构建,遍历等基本操作
- 二叉树的基本操作精集(创建、遍历、求深度结点以及叶子结点个数)
- 二叉树的基本操作,前序遍历,后续遍历,中序遍历
- 数据结构(十二) 二叉树的基本操作 --- 创建一个二叉树 前中后序遍历二叉树
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- 二叉树的建立(非递归建立与定义建立)与基本操作(广度和深度遍历,求叶子树高)实现
- 二叉树的基本操作,遍历,子结构,镜像,构建
- 二叉树与其基本递归操作:创建、遍历、特征量计算等
- 查找二叉树的基本操作以及层次遍历
- 二叉树的基本操作(含遍历算法非递归实现全收录)
- javascript实现数据结构: 树和二叉树,二叉树的遍历和基本操作
- c语言描述的二叉树的基本操作(层序遍历,递归,非递归遍历)