数据结构_二叉树_遍历算法应用

本文介绍如何根据先序遍历序列构建二叉树,并实现递归与非递归方式的前序、中序及后序遍历。此外,还提供了计算树的深度和叶子节点数量的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



经过几个小时的打拼,江山夺了下来,,哈哈,二叉树已被我拿下~~~~    ^_^




数据结构_二叉树的应用_遍历算法应用 - Essence - ACM_Of_Essence


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!!


数据结构_二叉树_遍历算法应用 - Essence - ACM_Of_Essence
 


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);
}
 
 
数据结构_二叉树的应用_遍历算法应用 - Essence - ACM_Of_Essence
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值