多叉数层次遍历

本文介绍了一种多叉树的数据结构实现方法,并详细解释了如何根据自定义输入构建多叉树,最后通过广度优先搜索的方式实现了从上到下、从左到右的层次遍历。

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

 1.给定一颗多叉树,每个节点保存一个int类型数字且节点数字不重复,要求从上到下按层次打印每个节点的数字,每一层按从左到右的顺序
要求:
(1)实现一颗多叉数
(2) 根据自定义输入,构造多叉树;
(3)从左到右按层输出多叉树
输入包含多行,每行有空格隔开的多个数字,第一个数字为某一个父节点的值,后面N个数字为该父节点的所有子节点的值,按从左到右的顺序排列。所有及诶点的值为整数,去值范围【0,100】
如:
5 2 3 
2 6 7 8
(5为根节点,有两个子节点,2为5的第一个子节点,包含三个子节点)
输出包含一行用空格隔开
5 2 3 6 7 8



#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
#define Max 100


typedef struct _mtree
{
    int data;
    int size;
    struct _mtree *element[Max];
} mtree;


class queue
{
    mtree *a[100];
    int size ;
    int beg;
    public:
    queue():size(),beg(){}
    void push( mtree* p )
    {
        if(size >= 100) throw "full";
        a[(beg +size)%100] = p;
        size++;
    }
    mtree * top()
    {
        if(size <= 0 )throw "empty";
        return a[beg%100];
    }
    void pop()
    {
        if(size<=0)throw "empty";
        beg++;
        size--;
    }
    bool empty()
    {
        return (size==0);
    }
};
mtree * searchnode(mtree *root,int data)
{
    if(root->data == data) return root;
    int i;
    //printf("srarchnode root->data = %d\n",root->data);
    //printf("srarchnode root->size = %d\n",root->size);
    for( i= 0; i<root->size;i++)
    {
        mtree *result =  searchnode(root->element[i],data);
        if(result != NULL) return result;
    }
    return NULL;
}
mtree * BuildTree(char *a,mtree *root)    
{
    //printf("%s\n",a);
    char tmp[Max];
    strcpy(tmp,a);
    char *tail = tmp;
    char * top = tmp;
    mtree *childroot= NULL;
    int flag = 1;
    while(*tail != '\0')
    {
        if(*tail ==  ' ')
        {
            *tail =  '\0';
            int data = atoi(top);
            //printf("data = %d\n",data);
            if(flag == 1){
                //第一次插入的根节点
                if(root == NULL)
                {
                    mtree *p = (mtree*) malloc(sizeof(mtree));
                    p->data = data;
                    p->size = 0;
                    childroot = p;
                    root = childroot;
                    //printf("root->data  =%d\n",childroot->data);
                }
                //不然需要先找到那个节点,根据data找
                else 
                {
                    childroot = searchnode(root,data);
                    //printf("---childroot->data  =%d\n",childroot->data);
                    //printf("---root->data  =%d\n",root->data);
                }
                flag  = 0;           
            }
            else 
            {
                mtree *p = (mtree*) malloc(sizeof(mtree));
                p->data = data;
                p->size = 0;
                childroot->element[childroot->size++] = p;
            }
            tail++;
            top = tail;
        }
        tail++;
    }


   int data = atoi(top);
    mtree *p = (mtree*) malloc(sizeof(mtree));
    p->data = data;
    p->size = 0;
    childroot->element[childroot->size++] = p;
    //printf("data = %d\n",data);


    return root;
}
void printtree(mtree* root)
{
    queue que;
    que.push(root);
    while(que.empty() == false)
    {
        mtree *p = que.top();
        printf("%d ",p->data);
        que.pop();
        for(int i = 0;i<p->size;i++)
        {
            que.push(p->element[i]);
        } 
    }
    printf("\n");
}
int main()
{
    char a[Max];
    mtree *root = NULL;
    while(1)
    {
        gets(a);
        if(strlen(a) <= 3){break;}
        root = BuildTree(a,root);
    //printf("------------------------------%d\n",root->data);
    }
    if(root == NULL) 
    {
        printf("root null\n");
    }
    //printf("------------------------------%d\n",root->data);
    printtree(root);


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值