数据结构-二叉树(叶子节点到根节点的路径相关问题)

二叉树的括号表示法:A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))

实现的功能:

        1.输出所有的叶子节点

        2.输出所有叶子节点到根节点的路径

        3.输出2中第一条最长的路径

复习数据结构中......,代码适合接触过数据结构的老铁解惑时看,程序运行结果是正确的,当然有的数组范围设置可以根据情况修改:

#include <iostream>
#include<malloc.h>
#include<stdio.h>
using namespace std;
typedef struct node
{
    char data;
    struct node* lchild;
    struct node* rchild;
}BTNode;
void CreaBT(BTNode *&b,char *str)
{
    BTNode *St[100],*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;
                    }
                }
        }
        j++;
        ch=str[j];
    }
}
//输出所有叶子节点到根节点的路径(非递归)
void AllPath(BTNode *b)
{
    struct node
    {
        int parent;
        BTNode *node;
    }qu[100];
    BTNode *q;
    int front,rear,p;
    front=rear=-1;
    rear++;
    qu[rear].node=b;
    qu[rear].parent=-1;
    while(front!=rear)
    {
        front++;
        q=qu[front].node;
        if(q->lchild==NULL&&q->rchild==NULL)
        {
            p=front;
            while(qu[p].parent!=-1)
            {
                cout<<qu[p].node->data<<"->";
                p=qu[p].parent;
            }
            cout<<qu[p].node->data<<endl;
        }
        if(q->lchild!=NULL)
        {
            rear++;
            qu[rear].node=q->lchild;
            qu[rear].parent=front;
        }
        if(q->rchild!=NULL)
        {
            rear++;
            qu[rear].node=q->rchild;
            qu[rear].parent=front;
        }
    }
}
//递归得到每个叶子结点到根节点的路径
void AllPath1(BTNode *b,char path[],int pathLen)
{
    if(b!=NULL)
    {
        if(b->lchild==NULL&&b->rchild==NULL)
        {
            cout<<b->data<<"到根节点路径:"<<b->data;
            for(int i=pathLen-1;i>=0;i--)
                cout<<"->"<<path[i];
            cout<<endl;
        }
        else
        {
            path[pathLen]=b->data;
            pathLen++;
            AllPath1(b->lchild,path,pathLen);
            AllPath1(b->rchild,path,pathLen);
            pathLen--;
        }
    }
}
//叶子结点到根节点最长路径长度
void LongPath(BTNode *b,char path[],int pathLen,char longpath[],int &longpathlen)
{
    if(b==NULL)
    {
        if(pathLen>longpathlen)
        {
            for(int i=pathLen-1;i>=0;i--)
                longpath[i]=path[i];
            longpathlen=pathLen;
        }
    }
    else
    {
        path[pathLen]=b->data;
        pathLen++;
        LongPath(b->lchild,path,pathLen,longpath,longpathlen);
        LongPath(b->rchild,path,pathLen,longpath,longpathlen);
        pathLen--;
    }
}
void DispBT(BTNode *b)
{
    if(b!=NULL)
    {
        cout<<b->data;
        if(b->lchild!=NULL||b->rchild!=NULL)
        {
            cout<<'(';
            DispBT(b->lchild);
            if(b->rchild!=NULL)
                cout<<',';
            DispBT(b->rchild);
            cout<<')';
        }
    }
}
int main()
{
    BTNode *b;
    char s[100],path[100],longpath[100];
    int pathlen=0,longpathlen=0;
    gets(s);
    CreaBT(b,s);
    AllPath(b);
    AllPath1(b,path,pathlen);
    LongPath(b,path,0,longpath,longpathlen);
    cout<<"第一条最长路径长度:"<<longpathlen<<endl;
    cout<<"第一条最长路径:";
    cout<<longpath[longpathlen-1];
    for(int i=longpathlen-2;i>=0;i--)
        cout<<"->"<<longpath[i];
    cout<<endl;
    DispBT(b);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zw159357/article/details/79931124