二叉树的基本操作-递归实现
来源:互联网 发布:夜神模拟器for mac 编辑:程序博客网 时间:2024/05/16 11:09
二叉树的一些基本操作学习
1.二叉树定义
typedef char Elemtype;typedef struct TreeNode{ Elemtype data; struct TreeNode *lchild,*rchild;}TreeNode,*BinTree;2.二叉树建立(先序递归建立)
//先序序列构造二叉树,形如ABC##DE###FG###void CreatTree(BinTree &T)3.先序递归遍历二叉树
void PreOrderBinTree(BinTree T)4.中序递归遍历二叉树
void InOrderBinTree(BinTree T)5.后序递归遍历二叉树
void PostOrderBinTree(BinTree T)6.求二叉树深度
int TreeDepth(BinTree T)
7.求叶子结点数
int TreeLeaves(BinTree T)8.交换左右孩子
void ExChild(BinTree T)
9.二叉树销毁
void DestroyTree(BinTree &T)
完整例子:
#include<iostream>#include<cstring>#include<stdlib.h>using namespace std;typedef char Elemtype;typedef struct TreeNode{ Elemtype data; struct TreeNode *lchild,*rchild;}TreeNode,*BinTree;//先序序列构造二叉树,形如ABC##DE###FG###void CreatTree(BinTree &T){ Elemtype ch; cin>>ch; if(ch=='#') { T=NULL; } else { T=new TreeNode; //T=(TreeNode*)malloc(sizeof(TreeNode)); T->data=ch; CreatTree(T->lchild); CreatTree(T->rchild); }} //递归先序遍历二叉树void PreOrderBinTree(BinTree T){ if(T!=NULL) { cout<<T->data<<" "; //访问结点 PreOrderBinTree(T->lchild); PreOrderBinTree(T->rchild); }}//递归中序遍历二叉树void InOrderBinTree(BinTree T){ if(T!=NULL) { InOrderBinTree(T->lchild); cout<<T->data<<" "; //访问结点 InOrderBinTree(T->rchild); }}void PostOrderBinTree(BinTree T){ if(T!=NULL) { PostOrderBinTree(T->lchild); PostOrderBinTree(T->rchild); cout<<T->data<<" "; //访问结点 }}int TreeDepth(BinTree T){ int ldepth,rdepth; if(T==NULL) return 0; ldepth=TreeDepth(T->lchild); rdepth=TreeDepth(T->rchild); return ldepth>rdepth?(ldepth+1):(rdepth+1);}int TreeLeaves(BinTree T){ if(T==NULL) return 0; if(T->lchild==NULL && T->rchild==NULL) return 1; return TreeLeaves(T->lchild)+TreeLeaves(T->rchild);}void ExChild(BinTree T){ if(T!=NULL) { BinTree temp=NULL; if(T->lchild!=NULL || T->rchild!=NULL) { temp=T->lchild; T->lchild=T->rchild; T->rchild=temp; ExChild(T->lchild); ExChild(T->rchild); } }}void DestroyTree(BinTree &T){ if(T!=NULL) { DestroyTree(T->lchild); DestroyTree(T->rchild); delete T; //free(T); T=NULL; }}//主函数int main(){ BinTree T; CreatTree(T); //PreOrder cout<<"PreOrderBinTree:"<<endl; PreOrderBinTree(T); cout<<endl; //InOrder cout<<"InOrderBinTree:"<<endl; InOrderBinTree(T); cout<<endl; //PostOrder cout<<"PostOrderBinTree:"<<endl; PostOrderBinTree(T); cout<<endl; //Tree depth cout<<"Tree depth:"<<TreeDepth(T)<<endl; //The number of leaves cout<<"The number of leaves:"<<TreeLeaves(T)<<endl; //Exchange Tree lchild and rchild; ExChild(T); cout<<"After exchange Tree child:"<<endl; PreOrderBinTree(T); cout<<endl; //Destroy Tree cout<<"Destroy Tree."<<endl; DestroyTree(T); return 0;}
0 0
- 二叉树的基本操作-递归实现
- *二叉树的基本操作(递归实现)*
- 数据结构 — 二叉树的基本操作(递归实现)
- 数据结构 — 二叉树的基本操作实现(递归算法)
- 二叉树的建立和基本操作(递归实现)
- 二叉树的基本操作(递归)
- 二叉树的基本操作--递归
- 二叉树的递归基本操作
- C语言实现二叉树的递归和非递归算法的基本操作
- C++实现二叉树的基本操作(递归+非递归)
- 二叉树的基本操作实现(递归和非递归)
- 二叉搜索树递归&&非递归的基本实现
- 第4章第1节练习题1 二叉树的基本操作(递归实现)
- 数据结构——排序/搜索二叉树(非递归)的基本操作实现
- 数据结构——排序/搜索二叉树(递归)的基本操作实现
- 二叉树的基本操作(非递归)
- 二叉树的基本操作(非递归)(修改)
- 二叉树基本操作的程序实现
- Xhprof----facebook开源的,轻量级的PHP性能分析工具
- c/c++使用gsoap发布和调用webservice
- CCFontAtlasCache,CCFontCharMap,CCFontFNT,CCFontFreeType解析
- [Leetcode 264] Ugly Number II
- Sublime Text 3 常用插件以及安装方法
- 二叉树的基本操作-递归实现
- 基于spring框架的BS构架web应用程序的一些学习心得
- Asp.mvc ~ 使用AngularJs构建SPA
- TFT显示汉字
- c/c++使用gsoap调用wcf发布的webservice
- HDOJ 题目3397 Sequence operation(线段树区间覆盖异或合并)
- HDU 1712 ACboy needs your help(泛化物品dp)
- Java编程:Beanutils-No destination bean specified问题分析
- MySQL 取得两个时间相差的分钟数 及 常用时间函数