数据结构_二叉树_遍历算法应用
2013-09-18 21:11
483 查看
经过几个小时的打拼,江山夺了下来,,哈哈,二叉树已被我拿下~~~~ ^_^
Description
按先序遍历序列建立二叉树的二叉链表,已知先序序列为:
A B C 0 0 D E 0 G 0 0 F 0 0 0 // 0表示空的
用文件操作,输入;
用concle操作,输出;
Sample Input
A B C 0 0 D E 0 G 0 0 F 0 0 0 // 0表示空的
Sample Output
depth of tree is 5
count of leaf node is 3
前序遍历:ABCDEGF
中序遍历:CBEGDFA
后续遍历:CGEFDBA
Finished!!
Code
//author:essence_of_ACMER
//time:2011.8.8
//theme:Traversal_algorithm
#include <iostream>
#define M 500
#include <malloc.h>
#include <fstream>
using namespace std;
ifstream f("a.txt");
ofstream f1("a1.txt");
typedef struct node
{
char date;
struct node *lchild,*rchild;
}JD ;
JD *crt_bt_pre(JD * bt)//先序遍历建立二叉树
{ char ch;
//printf("ch="); //这个非文件输入的情况
f>>ch;//scanf("%c",&ch); //文件输入(需要自己建立文件a.txt(其中是mian函数的下面的注释))
//getchar(); //这个非文件输入的情况
if(ch=='0') bt=NULL;
else
{ bt=(JD *)malloc(sizeof(JD));
bt->date=ch;
bt->lchild=crt_bt_pre(bt->lchild);
bt->rchild=crt_bt_pre(bt->rchild);
}
return(bt);
}
void inorder_fd(JD *bt)//fd表示非(f)递(d)归
{
JD *p,*s[M];
int i=0;
p=bt;
do{
while(p!=NULL)
{
s[i++]=p;
p=p->lchild;
}
if(i>0)
{
p=s[--i];
cout<<p->date;
f1<<p->date;
p=p->rchild;
}
}while(i>0||p!=NULL);
}
void preorder_d(JD *bt) //前序递归遍历
{
if(bt!=NULL)
{cout<<bt->date;
f1<<bt->date;
preorder_d(bt->lchild);
preorder_d(bt->rchild);}
}
void inorder_d(JD *bt) //中序递归遍历
{
if(bt!=NULL)
{inorder_d(bt->lchild);
cout<<bt->date;
f1<<bt->date;
inorder_d(bt->rchild);}
}
void postorder_d(JD *bt) //后序递归遍历
{
if(bt!=NULL)
{
postorder_d(bt->lchild);
postorder_d(bt->rchild);
cout<<bt->date;
f1<<bt->date;
}
}
void countleaf(JD *bt,int *count) //求叶子节点的数目
{ if(bt!=NULL)
{ if((bt->lchild==NULL)&&(bt->rchild==NULL))
{ (*count)++;
return;
}
countleaf(bt->lchild,count);
countleaf(bt->rchild,count);
}
}
void treedepth(JD *bt,int *l,int *h) //求树的深度
{
int l1=0,l2=0,h1=0,h2=0;
if(bt!=NULL)
{ (*l)++;
if (*l>*h) *h=*l;
treedepth(bt->lchild,&l1,&h1);
treedepth(bt->rchild,&l2,&h2);
if (h1>h2) *h=*h+h1;
else *h=*h+h2;
}
}
int main()
{ /* ABC00DE0G00F000 */
JD *head=NULL;
int level=0,high=0,count=0;
head=crt_bt_pre(head);//创建二叉树(前序)
treedepth(head,&level,&high);//求树的深度
printf("depth of tree is %d\n\n",high);
f1<<"depth of tree is"<<high<<endl;
countleaf(head,&count);//求树的叶子节点数
printf("count of leaf node is %d",count);
f1<<"count of leaf node is"<<count;
cout<<endl<<endl<<"前序遍历:";
f1<<endl<<endl<<"前序遍历:";
preorder_d(head);//前序遍历
cout<<endl<<endl<<"中序遍历:";
f1<<endl<<endl<<"中序遍历:";
inorder_d(head);//中序遍历
cout<<endl<<endl<<"后续遍历:";
f1<<endl<<endl<<"后续遍历:";
postorder_d(head);//后续遍历
cout<<endl<<endl<<"Finished!!"<<endl;
f1<<endl<<endl<<"Finished!!"<<endl;
return (0);
}
Description
按先序遍历序列建立二叉树的二叉链表,已知先序序列为:
A B C 0 0 D E 0 G 0 0 F 0 0 0 // 0表示空的
用文件操作,输入;
用concle操作,输出;
Sample Input
A B C 0 0 D E 0 G 0 0 F 0 0 0 // 0表示空的
Sample Output
depth of tree is 5
count of leaf node is 3
前序遍历:ABCDEGF
中序遍历:CBEGDFA
后续遍历:CGEFDBA
Finished!!
Code
//author:essence_of_ACMER
//time:2011.8.8
//theme:Traversal_algorithm
#include <iostream>
#define M 500
#include <malloc.h>
#include <fstream>
using namespace std;
ifstream f("a.txt");
ofstream f1("a1.txt");
typedef struct node
{
char date;
struct node *lchild,*rchild;
}JD ;
JD *crt_bt_pre(JD * bt)//先序遍历建立二叉树
{ char ch;
//printf("ch="); //这个非文件输入的情况
f>>ch;//scanf("%c",&ch); //文件输入(需要自己建立文件a.txt(其中是mian函数的下面的注释))
//getchar(); //这个非文件输入的情况
if(ch=='0') bt=NULL;
else
{ bt=(JD *)malloc(sizeof(JD));
bt->date=ch;
bt->lchild=crt_bt_pre(bt->lchild);
bt->rchild=crt_bt_pre(bt->rchild);
}
return(bt);
}
void inorder_fd(JD *bt)//fd表示非(f)递(d)归
{
JD *p,*s[M];
int i=0;
p=bt;
do{
while(p!=NULL)
{
s[i++]=p;
p=p->lchild;
}
if(i>0)
{
p=s[--i];
cout<<p->date;
f1<<p->date;
p=p->rchild;
}
}while(i>0||p!=NULL);
}
void preorder_d(JD *bt) //前序递归遍历
{
if(bt!=NULL)
{cout<<bt->date;
f1<<bt->date;
preorder_d(bt->lchild);
preorder_d(bt->rchild);}
}
void inorder_d(JD *bt) //中序递归遍历
{
if(bt!=NULL)
{inorder_d(bt->lchild);
cout<<bt->date;
f1<<bt->date;
inorder_d(bt->rchild);}
}
void postorder_d(JD *bt) //后序递归遍历
{
if(bt!=NULL)
{
postorder_d(bt->lchild);
postorder_d(bt->rchild);
cout<<bt->date;
f1<<bt->date;
}
}
void countleaf(JD *bt,int *count) //求叶子节点的数目
{ if(bt!=NULL)
{ if((bt->lchild==NULL)&&(bt->rchild==NULL))
{ (*count)++;
return;
}
countleaf(bt->lchild,count);
countleaf(bt->rchild,count);
}
}
void treedepth(JD *bt,int *l,int *h) //求树的深度
{
int l1=0,l2=0,h1=0,h2=0;
if(bt!=NULL)
{ (*l)++;
if (*l>*h) *h=*l;
treedepth(bt->lchild,&l1,&h1);
treedepth(bt->rchild,&l2,&h2);
if (h1>h2) *h=*h+h1;
else *h=*h+h2;
}
}
int main()
{ /* ABC00DE0G00F000 */
JD *head=NULL;
int level=0,high=0,count=0;
head=crt_bt_pre(head);//创建二叉树(前序)
treedepth(head,&level,&high);//求树的深度
printf("depth of tree is %d\n\n",high);
f1<<"depth of tree is"<<high<<endl;
countleaf(head,&count);//求树的叶子节点数
printf("count of leaf node is %d",count);
f1<<"count of leaf node is"<<count;
cout<<endl<<endl<<"前序遍历:";
f1<<endl<<endl<<"前序遍历:";
preorder_d(head);//前序遍历
cout<<endl<<endl<<"中序遍历:";
f1<<endl<<endl<<"中序遍历:";
inorder_d(head);//中序遍历
cout<<endl<<endl<<"后续遍历:";
f1<<endl<<endl<<"后续遍历:";
postorder_d(head);//后续遍历
cout<<endl<<endl<<"Finished!!"<<endl;
f1<<endl<<endl<<"Finished!!"<<endl;
return (0);
}
相关文章推荐
- 数据结构--二叉树--输出树中从根到每个叶子节点的路径(树遍历算法的应用) .
- 数据结构--二叉树--求树的深度的算法(树遍历算法的应用)
- 数据结构学习心得——二叉树的三种遍历算法
- SDUT 1291数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构:算法之二叉树各种遍历(申明:转自落日小屋)
- 数据结构与算法——二叉树的创建与遍历
- 数据结构与算法学习之二叉树的遍历:已知先序、中序、后序序列三者中的两种求另外一种遍历的算法
- 数据结构 二叉树的应用算法
- 1291 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 【数据结构】算法6.1-6.4 遍历二叉树
- python数据结构与算法——二叉树结构与遍历方法
- 【算法与数据结构】二叉树的 后序 遍历
- 数据结构上机测试4.1: 4000 二叉树的遍历与应用1
- 【算法与数据结构】二叉树的 先序 遍历
- 【算法与数据结构】二叉树的 先序 遍历
- 数据结构和算法--二叉树创建和递归遍历
- 一步一步复习数据结构和算法基础-层次建立层次遍历二叉树
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构—基于广度优先遍历算法的应用