栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

树的孩子兄弟表示法C++代码实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

树的孩子兄弟表示法C++代码实现

树的孩子兄弟表示法
//  孩子兄弟表示法

#include 

#include 

using namespace std;

#define ElemType char

typedef struct Node{
    ElemType data;
    struct Node * firstchild,*nextsibling;
}Node,*Tree;

Tree init_data2(Tree tree){
    ElemType data;
    cin >> data;
    if (data=='#') {
        tree = NULL;
    }else{
        tree = (Node *)malloc(sizeof(Node));
        tree->data = data;
        tree->firstchild = NULL;
        tree->nextsibling = NULL;
        cout << tree->data << "他的第一个孩子, 没有#" <firstchild = init_data2(tree->firstchild);
        cout << tree->data << "他的下一个兄弟" <nextsibling = init_data2(tree->nextsibling);
    }
    
    return tree;
}

void init_data(Tree &tree){
    tree = (Node *)malloc(sizeof(Node));
    tree->data = ' ';//头结点
    tree->firstchild = NULL;
    tree->nextsibling = NULL;
    
    cout << "请输入根结点:" << endl;
    tree->firstchild = init_data2(NULL);//头结点只需要1个指针即可
}


void print_data(Tree tree){
    if (tree!=NULL) {
        cout << "element:" << tree->data << endl;
        print_data(tree->firstchild);
        print_data(tree->nextsibling);
    }
}
int main(){
    std::cout << "welcome, to my world!" << std::endl;
    Tree tree = NULL;
    
//    cout << "size of:" << sizeof(tree) <firstchild);//传头结点
    return 0;
}



输出

welcome, to my world!
请输入根结点:
R
R他的第一个孩子, 没有#
A
A他的第一个孩子, 没有#
B
B他的第一个孩子, 没有#

B他的下一个兄弟

A他的下一个兄弟
C
C他的第一个孩子, 没有#

C他的下一个兄弟

R他的下一个兄弟

element:R
element:A
element:B
element:C
Program ended with exit code: 0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/303715.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号