二叉树的基本操作
来源:互联网 发布:js 日期时间格式化 编辑:程序博客网 时间:2024/06/05 22:31
#include<stdio.h> #include<iostream> #include<malloc.h> typedef struct node{ int data; struct node *lchild; struct node *rchild; }*BiTree;//此处*BiTree的作用与 typedef struct node *BiTree等价 int CreateTree(BiTree &T);//创建二叉树 int EmptyTree(BiTree T);//判断二叉树是否为空 int TreeDepth(BiTree T);//求树的深度 int search(BiTree T,int num);//在二叉树中查找节点 int QX(BiTree T);//前序遍历 int ZX(BiTree T);//中序遍历 int HX(BiTree T);//后序遍历 int b=0,c=0; int main(){ //BiTree T作用和 struct node *T相同 int depth; int num; struct node *T; CreateTree(T); EmptyTree(T); depth=TreeDepth(T); printf("树的深度为%d\n",depth); printf("请输入要查找的数:"); scanf("%d",&num); search(T,num); if(b==c) printf("没有找到节点\n"); printf("前序遍历:"); QX(T); printf("\n"); printf("中序遍历:"); ZX(T); printf("\n"); printf("后序遍历:"); HX(T); system("pause"); return 0; } ////////////////////////////////////////创建二叉树 int CreateTree(BiTree &T){ int a; printf("请输入数值:"); scanf("%d",&a); if(a==-1){ T=NULL; return 0;//return的作用是用来终止函数,return在此处的作用可有可无 } else{ T=(struct node *)malloc(sizeof(struct node)); T->data=a; b++; CreateTree(T->lchild);//递归 CreateTree(T->rchild);//递归 } return 0; } ////////////////////////////////////////判断二叉树是否为空 int EmptyTree(BiTree T){ if(T==NULL) printf("此二叉树为空\n"); if(T!=NULL) printf("此二叉树不为空\n"); return 0; } ///////////////////////////////////////判断树的深度 int TreeDepth(BiTree T){ int right,left; if(!T) { return 0; } else{ left=TreeDepth(T->lchild); right=TreeDepth(T->rchild); if(left>right) return left+1; else return left+1; } } ////////////////////////////////////////前序遍历 int QX(BiTree T){ if(T){ printf("%d ",T->data); QX(T->lchild); QX(T->rchild); return 0; } else return 1; } ////////////////////////////////////////中序遍历 int ZX(BiTree T){ if(T){ ZX(T->lchild); printf("%d ",T->data); ZX(T->rchild); return 0; } else return 1; } ///////////////////////////////////////后序遍历 int HX(BiTree T){ if(T){ HX(T->lchild); HX(T->rchild); printf("%d ",T->data); return 0; } else return 1; } /////////////////////////////////////查找节点 int search(BiTree T,int num){ if(T){ if(T->data==num) printf("找到节点\n"); else{ c++; search(T->lchild,num); search(T->rchild,num); } return 0; } else{ return 1; } } #include<stdio.h> #include<iostream> #include<malloc.h> typedef struct node{ int data; struct node *lchild; struct node *rchild; }*BiTree;//此处*BiTree的作用与 typedef struct node *BiTree等价 int CreateTree(BiTree &T);//创建二叉树 int EmptyTree(BiTree T);//判断二叉树是否为空 int TreeDepth(BiTree T);//求树的深度 int search(BiTree T,int num);//在二叉树中查找节点 int QX(BiTree T);//前序遍历 int ZX(BiTree T);//中序遍历 int HX(BiTree T);//后序遍历 int b=0,c=0; int main(){ //BiTree T作用和 struct node *T相同 int depth; int num; struct node *T; CreateTree(T); EmptyTree(T); depth=TreeDepth(T); printf("树的深度为%d\n",depth); printf("请输入要查找的数:"); scanf("%d",&num); search(T,num); if(b==c) printf("没有找到节点\n"); printf("前序遍历:"); QX(T); printf("\n"); printf("中序遍历:"); ZX(T); printf("\n"); printf("后序遍历:"); HX(T); system("pause"); return 0; } ////////////////////////////////////////创建二叉树 int CreateTree(BiTree &T){ int a; printf("请输入数值:"); scanf("%d",&a); if(a==-1){ T=NULL; return 0;//return的作用是用来终止函数,return在此处的作用可有可无 } else{ T=(struct node *)malloc(sizeof(struct node)); T->data=a; b++; CreateTree(T->lchild);//递归 CreateTree(T->rchild);//递归 } return 0; } ////////////////////////////////////////判断二叉树是否为空 int EmptyTree(BiTree T){ if(T==NULL) printf("此二叉树为空\n"); if(T!=NULL) printf("此二叉树不为空\n"); return 0; } ///////////////////////////////////////判断树的深度 int TreeDepth(BiTree T){ int right,left; if(!T) { return 0; } else{ left=TreeDepth(T->lchild); right=TreeDepth(T->rchild); if(left>right) return left+1; else return left+1; } } ////////////////////////////////////////前序遍历 int QX(BiTree T){ if(T){ printf("%d ",T->data); QX(T->lchild); QX(T->rchild); return 0; } else return 1; } ////////////////////////////////////////中序遍历 int ZX(BiTree T){ if(T){ ZX(T->lchild); printf("%d ",T->data); ZX(T->rchild); return 0; } else return 1; } ///////////////////////////////////////后序遍历 int HX(BiTree T){ if(T){ HX(T->lchild); HX(T->rchild); printf("%d ",T->data); return 0; } else return 1; } /////////////////////////////////////查找节点 int search(BiTree T,int num){ if(T){ if(T->data==num) printf("找到节点\n"); else{ c++; search(T->lchild,num); search(T->rchild,num); } return 0; } else{ return 1; } }
0 0
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- windows handle msg
- C#调用C生成DLL文件
- 我的android学习笔记
- java.lang.ClassCastException: org.apache.commons.dbcp.BasicDataSource cannot be cast to
- 源码推荐(1月22日):用数据驱动的UITableView内容管理 仿网易订阅的球形3D标签
- 二叉树的基本操作
- tomcat服务器设置项目为默认访问
- blackmagic Capture.cpp
- ubuntu下c/c++基本编译环境的搭建
- java中synchronized用法(二)
- 插入排序 insertion_sort
- Qt程序crash信息的捕捉与跟踪
- 简单的android游戏框架——zgf
- snmp协议