树转二叉树(C语言实现, 递归)

本文介绍了一种将多叉树转换为二叉树的方法,通过队列数据结构实现。首先创建二叉树节点,然后将原始树的子节点入队,按照顺序出队并构建二叉树的左子树,接着处理剩余的子节点构建右子树。提供的示例代码展示了如何定义树节点、二叉树节点和队列,并进行了具体的操作演示。

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

本方法需要队列实现

设定

树的结点定义

将树节点定义为具有数据域和子结点域的结构体

typedef struct prototype_treeNode
{
	Datatype data;
	struct prototype_treeNode* children[N];
	//N为子结点最大数量
} TreeNode, *Tree;

且子结点域中无子结点的位置用NULL空指针填充

二叉树结点的定义

二叉树使用二叉链表表示

typedef struct prototype_biNode
{
	Datatype data;
	struct prototype_biNode *lchild, *rchild;
} BiNode, *BiTree;

队列定义

typedef struct prototype_queue
{
	int front, rear;
	void** Que;
} Queue;

Queue *createQueue()                   //创建队列
{
	Queue *new_que = (Queue*)malloc(sizeof(Queue));
    new_que->Que = (void**)malloc(100*sizeof(void*));
    //100可以是任何数字
    new_que->front = 0;
    new_que->rear = 0;
    return new_que;
}

bool isEmptyQueue(Queue *Q)        //队为空返回true,不为空时返回false
{
	return Q->front == Q->rear ? true : false;
}
void *deQueue(Queue *Q)            //结点指针出队
{
	if(isEmptyQueue(Q))
    {
        return NULL;
    }
    return Q->Que[Q->front++];
}
void enQueue(Queue *Q, void *node) //结点指针入队
{
	Q->Que[Q->rear] = node;
    Q->rear++;
}

实现代码

BiTree transform(TreeNode *root)
{
    if (root == NULL)
    {
        return NULL;
    }
    
    BiTree retNode = (BiTree)malloc(sizeof(BiNode));
    retNode->data = root->data;
    retNode->rchild = NULL;

    Queue *que = createQueue();
    for (int i = 0; root->children[i] != NULL; i++)
    {
        enQueue(que, root->children[i]);
    }

    retNode->lchild = transform((TreeNode*)deQueue(que));

    BiNode *ptr = retNode->lchild;
    while (!isEmptyQueue(que))
    {
        ptr->rchild = transform((TreeNode*)deQueue(que));
        ptr = ptr->rchild;
    }

    free(que);
    return retNode;
}

附上一段示例代码

typedef char Datatype;

int main(void)
{
    TreeNode *test = (TreeNode *)malloc(sizeof(TreeNode));
    test->data = 'a';
    test->children[0] = (TreeNode *)malloc(sizeof(TreeNode));
    test->children[1] = (TreeNode *)malloc(sizeof(TreeNode));
    test->children[2] = (TreeNode *)malloc(sizeof(TreeNode));
    test->children[0]->data = 'b';
    test->children[1]->data = 'c';
    test->children[2]->data = 'd';
    test->children[3] = NULL;

    test->children[0]->children[0] = (TreeNode *)malloc(sizeof(TreeNode));
    test->children[0]->children[1] = (TreeNode *)malloc(sizeof(TreeNode));
    test->children[0]->children[0]->data = 'e';
    test->children[0]->children[0]->children[0] = NULL;
    test->children[0]->children[1]->data = 'f';
    test->children[0]->children[1]->children[0] = NULL;
    test->children[0]->children[2] = NULL;

    test->children[1]->children[0] = (TreeNode *)malloc(sizeof(TreeNode));
    test->children[1]->children[0]->data = 'g';
    test->children[1]->children[0]->children[0] = NULL;
    test->children[1]->children[1] = NULL;

    test->children[2]->children[0] = (TreeNode *)malloc(sizeof(TreeNode));
    test->children[2]->children[1] = (TreeNode *)malloc(sizeof(TreeNode));
    test->children[2]->children[2] = (TreeNode *)malloc(sizeof(TreeNode));
    test->children[2]->children[0]->data = 'h';
    test->children[2]->children[1]->data = 'i';
    test->children[2]->children[2]->data = 'j';
    for (int i = 0; i <= 2; i++)
    {
        test->children[2]->children[i]->children[0] = NULL;
    }
    test->children[2]->children[3] = NULL;

    BiTree shift = transform(test);

    return 0;
}

即将如下的树1

在这里插入图片描述

转换成如下的二叉树2

在这里插入图片描述


  1. 1: 图片来源 , 侵删 ↩︎

  2. 2: 图片来源 , 侵删 ↩︎

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值