二叉链表、三叉链表、双亲链表(数组表示)、先序遍历、中序遍历、后续遍历

本文详细介绍了二叉树的链表实现及其遍历算法,包括先序、中序和后序遍历,并探讨了三叉链表及双亲链表的构建与应用。通过具体代码示例,深入解析了不同链表结构的特点和使用场景。

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

#include <iostream>
using namespace std;
//二叉链表
struct BinNode
{
    int data;
    struct BinNode * lchild, *rchild;
};
typedef struct BinNode BinNode;
typedef struct BinNode * BinTree;

//三叉链表  多了一个指向双亲节点指针,可以很快访问到双亲
struct BinTNode
{
    int data;
    struct BinTNode * lchild, *rchild;
    struct BinTNode * parent;
};

//先序遍历
void Preindex(BinNode * root)
{
    if (root == NULL)
    {
        return;
    }
    printf("%d ", root->data);
    Preindex(root->lchild);
    Preindex(root->rchild);
}

//中序遍历
void Middleindex(BinNode * root)
{
    if (root == NULL)
    {
        return;
    }
    Middleindex(root->lchild);
    printf("%d ", root->data);
    Middleindex(root->rchild);
}
//后序遍历
void Lastindex(BinNode * root)
{
    if (root == NULL)
    {
        return;
    }
    Lastindex(root->lchild);
    Lastindex(root->rchild);
    printf("%d ", root->data);
}

int main01()
{
    BinNode t1, t2, t3, t4, t5;
    memset(&t1, 0, sizeof(t1));
    memset(&t2, 0, sizeof(t2));
    memset(&t3, 0, sizeof(t3));
    memset(&t4, 0, sizeof(t4));
    memset(&t5, 0, sizeof(t5));
    t1.data = 1;
    t2.data = 2;
    t3.data = 3;
    t4.data = 4;
    t5.data = 5;
    t1.lchild = &t2;
    t1.rchild = &t3;
    t2.lchild = &t4;
    t3.rchild = &t5;
    //遍历
    Preindex(&t1);
    cout << endl;
    Middleindex(&t1);
    cout << endl;
    Lastindex(&t1);
    cout << endl;
    return 0;
}

int main02()
{
    BinNode * t1,* t2,* t3,* t4,* t5;
    t1 = (BinNode *)malloc(sizeof(BinNode));
    t2 = (BinNode *)malloc(sizeof(BinNode));
    t3 = (BinNode *)malloc(sizeof(BinNode));
    t4 = (BinNode *)malloc(sizeof(BinNode));
    t5 = (BinNode *)malloc(sizeof(BinNode));
    memset(t1, NULL, sizeof(BinNode));  //必须初始化,否则会报错
    memset(t2, NULL, sizeof(BinNode));
    memset(t3, NULL, sizeof(BinNode));
    memset(t4, NULL, sizeof(BinNode));
    memset(t5, NULL, sizeof(BinNode));
    t1->data = 1;
    t2->data = 2;
    t3->data = 3;
    t4->data = 4;
    t5->data = 5;
    t1->lchild = t2;
    t1->rchild = t3;
    t2->lchild = t4;
    t3->rchild = t5;
    Preindex(t1);
    cout << endl;
    Middleindex(t1);
    cout << endl;
    Lastindex(t1);
    cout << endl;
    free(t1);
    free(t2);
    free(t3);
    free(t4);
    free(t5);
    return 0;
}

//双亲链表(数组表示)
typedef struct TwoBinNode  //定义节点,结点结构
{
    int data;
    struct TwoBinNode* parent;  //指向parent
    int Flag; //0为左孩子,1为右孩子
}TwoBinNode;
typedef struct TwoBinTree  //树结构
{
    TwoBinNode *node;
    int num;
    TwoBinNode *root;
}TwoBinTree;

//双亲链表(数组表示)  建树
int main()
{
    TwoBinTree * tree = (TwoBinTree *)malloc(sizeof(TwoBinTree));
    tree->node = (TwoBinNode *)malloc(sizeof(TwoBinNode) * 100);
    tree->num = 0;
    tree->root = NULL;
    memset(tree->node, NULL, sizeof(TwoBinNode) * 100);
    tree->node[0].data = 1;
    tree->node[0].parent = NULL;
    tree->node[0].Flag = NULL;
    tree->root = &tree->node[0];
    tree->num++;
    tree->node[1].data = 2;
    tree->node[0].parent = &tree->node[0];
    tree->node[0].Flag = 0;
    tree->num++;
    tree->node[2].data = 3;
    tree->node[0].parent = &tree->node[0];
    tree->node[0].Flag = 1;
    tree->num++;
    tree->node[3].data = 4;
    tree->node[0].parent = &tree->node[1];
    tree->node[0].Flag = 0;
    tree->num++;
    tree->node[4].data = 5;
    tree->node[4].parent = &tree->node[2];
    tree->node[0].Flag = 1;
    tree->num++;
    for (int i = 0; i < tree->num; i++)
    {
        cout << tree->node[i].data << " ";
    }
    cout << endl;
    return 0;
}

理论学习参考链接:https://blog.csdn.net/w_linux/article/details/78473491

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值