数据结构之二叉树的应用(先序、中序、后序遍历,查找结点数、叶子结点数、宽度等)

代码实现:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxxize 100
typedef struct node{
    char data;
    node *lchild,*rchild;
} BTNode;//结构体类型名
BTNode *St[maxxize],*b;//b为根结点
int num1,num2;//num1保存二叉树结点个数,num2保存二叉树叶子结点个数;
//构建二叉树
void CreatBTree(BTNode *&b,char *str)
{
    BTNode *p;
    int top=-1,k,j=0;
    char ch;
    b=NULL;
    ch=str[j];
    while(ch!='\0'){
        switch(ch){
        case '(':
            top++;
            St[top]=p;
            k=1;
            break;
        case ')':
            top--;
            break;
        case ',':
            k=2;
            break;
        default:
            p=(BTNode *)malloc(sizeof (BTNode) );
            p->data=ch;
            p->lchild=p->rchild=NULL;
            if(b==NULL)
                b=p;
            else{
                switch(k){
                    case 1:
                        St[top]->lchild=p;
                        break;
                    case 2:
                        St[top]->rchild=p;
                        break;
                    }
                }
        }
        ch=str[++j];
    }
}
//先序遍历
void PreOrder(BTNode *p)
{
    if(p!=NULL){
        num1++;//结点数加1
        if(p->lchild==NULL&&p->rchild==NULL)//判断是否为叶子结点
            num2++;
        cout<<p->data<<" ";
        PreOrder(p->lchild);
        PreOrder(p->rchild);
    }
}
//中序遍历
void InOrder(BTNode *p)
{
    if(p!=NULL){
        InOrder(p->lchild);
        cout<<p->data<<" ";
        InOrder(p->rchild);
    }
}
//后序遍历
void PostOrder(BTNode *p)
{
    if(p!=NULL){
        PostOrder(p->lchild);
        PostOrder(p->rchild);
        cout<<p->data<<" ";
    }
    else ;
}
//查找结点深度
int find_floor(char ch,BTNode *p,int floor)
{
    if(p==NULL)
        return -1;
    if(ch==p->data)
        return floor;
    int l=find_floor(ch,p->lchild,floor+1);
    int r=find_floor(ch,p->rchild,floor+1);
    if(l!=-1)
        return l;
    if(r!=-1)
        return r;
    return -1;
}
//查找二叉树宽度
struct iqueue{
    int value;
    BTNode *p;
}Queue[maxxize];
int find_width(BTNode *b)
{

    int left,right;
    int x,maxx,i,n;
    left=right=0;
    if(b!=NULL){
        right++;Queue[right].p=b;
        Queue[right].value=1;
        while(right!=left){
            left++;b=Queue[left].p;
            x=Queue[left].value;
            if(b->lchild!=NULL){
                right++;Queue[right].p=b->lchild;
                Queue[right].value=x+1;
            }
            if(b->rchild!=NULL){
                right++;Queue[right].p=b->rchild;
                Queue[right].value=x+1;
            }
        }
        maxx=0;x=1;i=1;
        while(i<=right){
            n=0;
            while(i<=right&&Queue[i].value==x){
                n++;i++;
            }
            x=Queue[i].value;
            if(n>maxx) maxx=n;
        }
        return maxx;
    }
    return 0;
}
int main()
{
    char ch[3],s[]="A(B(D,E(H(J,K(L,M(,N))),)),C(F,G(,I)))";
    b=(BTNode *)malloc(sizeof (BTNode));
    CreatBTree(b,s);

    cout<<"先序遍历"<<endl;
    PreOrder(b);
    cout<<endl<<"中序遍历"<<endl;
    InOrder(b);
    cout<<endl<<"后序遍历"<<endl;
    PostOrder(b);
    cout<<endl<<endl;
    //num1和num2的求解在先序遍历函数
    cout<<"二叉树结点个数为 "<<num1<<endl;
    cout<<"二叉树叶子结点个数为 "<<num2<<endl<<endl;

    cout<<"二叉树的宽度为 "<<find_width(b)<<endl<<endl;

    cout<<"请输入要查询的结点"<<endl;
    cin>>ch;
    int floor_ch=find_floor(ch[0],b,1);
    if(floor_ch!=-1)
        cout<<"该结点的层次为 "<<floor_ch<<endl;
    else
        cout<<"二叉树中没有该结点"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_15742375/article/details/84566596