孩子兄弟链表存储其实就是二叉链表。下面这段代码只是简单的实现了二叉链表的功能。

#include <iostream>
using namespace std;


typedef char ElemType;
typedef struct TreeCode{
    ElemType data;
    TreeCode* firstchild;
    TreeCode* nextsbling;
}TreeCode;

TreeCode* sblingInsert(TreeCode *tree);
TreeCode* Insertchild(TreeCode *tree);

// 初始化
void Init(TreeCode* tree){
    tree->data='0';
    tree->firstchild= nullptr;
    tree->nextsbling= nullptr;
}

// 根节点
TreeCode* Root(TreeCode* tree,ElemType data){
    tree->data=data;
    tree->firstchild= nullptr;
    tree->nextsbling= nullptr;
    return tree;
}

// 插入兄弟节点
TreeCode* sblingInsert(TreeCode *tree){
    ElemType data;
    cout<<"输入兄弟数据:";
    cin>>data;
    if (data=='#'){
        return nullptr;
    }
    // 寻找最后一个兄弟节点
    TreeCode* p = tree;
    while (p->nextsbling != nullptr) {
        p = p->nextsbling;
    }
    auto newsbling=new TreeCode;
    newsbling->data=data;
    TreeCode* temp=tree;
    while(temp->nextsbling!= nullptr){
        temp=temp->nextsbling;
    }
    temp->nextsbling=newsbling;
    newsbling->firstchild= nullptr;
    newsbling->nextsbling= nullptr;
    p->nextsbling=newsbling;

    return tree;
}

// 插入孩子节点
TreeCode* Insertchild(TreeCode *tree){
    ElemType data;
    cout<<"输入孩子节点数据:";
    cin>>data;
    if (tree->firstchild== nullptr) {
        auto *firstchild = new TreeCode;
        firstchild->data = data;
        tree->firstchild=firstchild;
        firstchild->firstchild= nullptr;
        firstchild->nextsbling= nullptr;
    } else {
        auto *newcode = new TreeCode;
        newcode->data = data;
        TreeCode *temp = tree;
        while (temp->firstchild != nullptr) {
            temp = temp->firstchild;
        }
        temp->firstchild = newcode;
        newcode->firstchild = nullptr;
        newcode->nextsbling = nullptr;
    }
    return tree;
}

// 递归释放内存
TreeCode* Free(TreeCode* tree){
    // 释放孩子节点内存
    TreeCode* p = tree->firstchild;
    while (p != nullptr) {
        TreeCode* temp=p;
        Free(p);  // 递归调用Free函数释放孩子节点的子树
        p=p->firstchild;
        temp->firstchild = nullptr;
    }

    // 释放兄弟节点内存
    TreeCode* q = tree->nextsbling;
    while (q != nullptr) {
        TreeCode* temp = q;
        q = q->nextsbling;
        Free(q);  // 递归调用Free函数释放兄弟节点的子树
        temp->nextsbling = nullptr;
    }


    delete tree;  // 删除当前节点
    return nullptr;  // 返回nullptr表示释放完成
}

// 访问输出
void Visit(TreeCode* tree){
    // 访问根节点
    cout<<"根节点:";
    cout<<tree->data<<endl;
    //访问孩子节点
    TreeCode* p=tree->firstchild;
    while (p!= nullptr){
        cout<<"孩子节点:";
        cout<<p->data<<endl;
        p=p->firstchild;
    }
    // 最后访问兄弟节点
    TreeCode* q=tree->nextsbling;
    while (q!= nullptr){
        cout<<"兄弟节点:";
        cout<<q->data<<endl;
        q=q->nextsbling;
    }
}

int main(){
    TreeCode tree;
    Init(&tree);
    Root(&tree,'A');
    Insertchild(&tree);
    sblingInsert(&tree);
    Visit(&tree);
    Free(&tree);
    return 0;
}

运行结果:

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐