数据结构二叉树的操作

来源:百度知道 编辑:UC知道 时间:2024/05/25 00:34:45
(1)用递归方法创建二叉链表
(2)用递归算法对二叉树进行先序遍历,中序遍历和后序遍历,并输出遍历结果
(3)对二叉树进行层次遍历,并输出遍历序列
(4)求二叉树的深度并输出
那位高手麻烦不要太复杂的,好了还追加分

#include <stdio.h>//头文件
#include <stdlib.h>
#include <malloc.h>
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}
BiTNode,*BiTree;//定义结点类型
typedef struct QNode
{
BiTNode data;
struct QNode *next;
} //定义队列的节点类型
QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}
LinkQueue;//队列
void InitQueue(LinkQueue *Q)//创建队列
{
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
Q->front->next=NULL;
}
void EnQueue(LinkQueue *Q,BiTNode e)//将元素入队
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));//为结点开辟空间
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
}
BiTNode DeQueue(LinkQueue *Q)//将元素出列并返回元素的值。
{
BiTNode e;QueuePtr p;
p=Q->front->next;
e=p->data;