树的存储与遍历
来源:互联网 发布:http index.php 编辑:程序博客网 时间:2024/05/19 02:05
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#define N 10
typedef int datatype;
typedef struct _btnode_
{
datatype no;
struct _btnode_ *lchild, *rchild;
} bitree;
{
datatype no;
struct _btnode_ *lchild, *rchild;
} bitree;
typedef struct
{
bitree *data[N];
int front, rear;
} sequeue;
{
bitree *data[N];
int front, rear;
} sequeue;
sequeue *CreateEmptyQueue()
{
sequeue *sq;
{
sequeue *sq;
sq = (sequeue *)malloc(sizeof(sequeue));
sq->front = sq->rear = 0;
sq->front = sq->rear = 0;
return sq;
}
}
void EnQueue(sequeue *sq, bitree *r)
{
sq->rear = (sq->rear + 1) % N;
sq->data[sq->rear] = r;
{
sq->rear = (sq->rear + 1) % N;
sq->data[sq->rear] = r;
return;
}
}
bitree *DeQueue(sequeue *sq)
{
sq->front = (sq->front + 1) % N;
return sq->data[sq->front];
}
{
sq->front = (sq->front + 1) % N;
return sq->data[sq->front];
}
int EmptyQueue(sequeue *sq)
{
return (sq->front == sq->rear);
}
{
return (sq->front == sq->rear);
}
bitree *CreateBitree(int i, int n)
{
bitree *root;
{
bitree *root;
root = (bitree *)malloc(sizeof(bitree));
root->no = i;
root->no = i;
if (2*i <= n)
{
root->lchild = CreateBitree(2*i, n);
}
else
{
root->lchild = NULL;
}
if ((2*i+1) <= n)
{
root->rchild = CreateBitree(2*i+1, n);
}
else
{
root->rchild = NULL;
}
{
root->lchild = CreateBitree(2*i, n);
}
else
{
root->lchild = NULL;
}
if ((2*i+1) <= n)
{
root->rchild = CreateBitree(2*i+1, n);
}
else
{
root->rchild = NULL;
}
return root;
}
}
void PreOrder(bitree *root)
{
printf("%d ", root->no);
if (root->lchild != NULL) PreOrder(root->lchild);
if (root->rchild != NULL) PreOrder(root->rchild);
{
printf("%d ", root->no);
if (root->lchild != NULL) PreOrder(root->lchild);
if (root->rchild != NULL) PreOrder(root->rchild);
return;
}
}
void InOrder(bitree *root)
{
if (root->lchild != NULL) InOrder(root->lchild);
printf("%d ", root->no);
if (root->rchild != NULL) InOrder(root->rchild);
{
if (root->lchild != NULL) InOrder(root->lchild);
printf("%d ", root->no);
if (root->rchild != NULL) InOrder(root->rchild);
return;
}
}
void PostOrder(bitree *root)
{
if (root->lchild != NULL) PostOrder(root->lchild);
if (root->rchild != NULL) PostOrder(root->rchild);
printf("%d ", root->no);
{
if (root->lchild != NULL) PostOrder(root->lchild);
if (root->rchild != NULL) PostOrder(root->rchild);
printf("%d ", root->no);
return;
}
}
void NoOrder(bitree *root)
{
sequeue *sq;
bitree *r;
{
sequeue *sq;
bitree *r;
sq = CreateEmptyQueue();
EnQueue(sq, root);
while ( ! EmptyQueue(sq) )
{
r = DeQueue(sq);
printf("%d ", r->no);
if (r->lchild != NULL) EnQueue(sq, r->lchild);
if (r->rchild != NULL) EnQueue(sq, r->rchild);
}
EnQueue(sq, root);
while ( ! EmptyQueue(sq) )
{
r = DeQueue(sq);
printf("%d ", r->no);
if (r->lchild != NULL) EnQueue(sq, r->lchild);
if (r->rchild != NULL) EnQueue(sq, r->rchild);
}
printf("\n");
}
}
int main()
{
bitree *root;
{
bitree *root;
root = CreateBitree(1, 10);
printf("PreOrder : ");
PreOrder(root);
printf("\n");
printf("InOrder : ");
InOrder(root);
printf("\n");
printf("PostOrder : ");
PostOrder(root);
printf("\n");
printf("PreOrder : ");
PreOrder(root);
printf("\n");
printf("InOrder : ");
InOrder(root);
printf("\n");
printf("PostOrder : ");
PostOrder(root);
printf("\n");
printf("NoOrder : ");
NoOrder(root);
NoOrder(root);
return 0;
}
}
- 树的存储与遍历
- 二叉树的存储与遍历
- 树的输入、存储与遍历
- 图的存储与遍历
- 图的存储与遍历
- 图的存储与遍历
- 图的存储与遍历
- 普通树的存储与遍历(合集)
- 二叉树的建立与存储,以及遍历方法
- 二叉树的链表存储与遍历
- 数据结构 树的表示,存储结构与遍历
- 数据结构-二叉树的存储结构与遍历
- 图的存储结构与遍历
- 图的邻接矩阵存储与遍历
- 图的邻接表存储与遍历
- 树的存储及遍历
- 树的分类、存储、遍历
- 二叉树数组顺序存储与遍历
- 栈的实现—由链表实现
- 栈的实例—表达式求值
- 队列的实现—由数组实现
- 队列的实现—由链表实现
- 利用队列和栈实现球钟问题
- 树的存储与遍历
- 图的存储—数组
- 图的存储—链表
- 利用图求最短路径
- 快速排序实现
- 学习使用linux下tags文件
- 标准IO的函数分析
- getopt函数的使用
- 用文件IO操作实现ls命令