四叉树的前序,后序,层次遍历
来源:互联网 发布:淘宝领优酷会员在哪里 编辑:程序博客网 时间:2024/04/28 20:08
//四叉树的前序,后序,层次遍历
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
typedef char datatype;
typedef struct node //二叉树
{
datatype data;
struct node *lt,*rt,*lb,*rb;
} fourtnode, *fourtree;
typedef struct stack
{
fourtree data[100];
int tag[100];
int top;
} seqstack;
//队列的顺序结构
#define MAXQSIZE 100
typedef struct
{
fourtree * base;
int front;
int rear;
}SqQueue;
int InitQueue (SqQueue &Q)
{ //构造空队列
Q.base=(fourtree*)malloc (MAXQSIZE *sizeof(fourtree));//申请连续的存储空间
if (!Q.base) exit(0);
Q.front=Q.rear=0;//头、尾指针的初始化
return 1;
}
int EnQueue (SqQueue &Q,fourtree t)
{//入队
if ((Q.rear+1) % MAXQSIZE ==Q.front) return 0;//队满则返回出错信息
Q.base[Q.rear]=t;//队尾插入元素
Q.rear = (Q.rear+1)%MAXQSIZE;//队尾指针后移
return 1;
}
int OutQueue (SqQueue &Q, fourtree & t)
{//出队
if (Q.rear ==Q.front) return 0;//队空的话则返回出错信息
t=Q.base[Q.front];
Q.front = (Q.front+1)%MAXQSIZE;//队头指针后移
return 1;
}
void createbintree(fourtree & t)
{
char ch;
if((ch=getchar())== '#')
t= NULL;
else
{
t = (fourtree )malloc(sizeof(fourtnode));
t->data = ch;
createbintree(t->lb);
createbintree(t->rb);
createbintree(t->rt);
createbintree(t->lt);
}
}//CreateBiTree
void preorder(fourtree t)//递归前序遍历
{
if(t)
{
cout<<t->data;
preorder( t->lb);
preorder( t->rb);
preorder( t->rt );
preorder( t->lt);
}
}
void postorder(fourtree t)//递归后序遍历
{
if(t)
{
postorder( t->lb );
postorder( t->rb);
postorder( t->rt);
postorder( t->lt);
cout<<t->data;
}
}
void levelorder(fourtree t)
{ SqQueue Q;
InitQueue (Q);
if(t)
{
EnQueue (Q, t);
while(Q.front !=Q.rear)
{
OutQueue (Q, t);
cout<<t->data;
if(t->lb)
EnQueue (Q, t->lb);
if(t->rb)
EnQueue (Q, t->rb);
if(t->rt)
EnQueue (Q, t->rt);
if(t->lt)
EnQueue (Q, t->lt);
}
}
}
int main()
{
fourtree root;
printf("请输入您要建立的二叉树的先序扩展序列(用#表示空)\n");
createbintree(root);
printf("构造二叉树成功!\n");
cout<<"递归前序遍历"<<endl;
preorder(root);
cout<<endl;
cout<<"递归后序遍历"<<endl;
postorder(root );
cout<<endl;
cout<<"层次遍历"<<endl;
levelorder( root);
cout<<endl;
return 0;
}
- 四叉树的前序,后序,层次遍历
- 树的前序遍历,中序遍历,后序遍历,层次(广度)遍历
- 前序遍历层次遍历
- 二叉树的非递归遍历以及层次遍历(前序、中序、后序)
- 树的遍历-已知中序+(前序|后序)求层次遍历
- 二叉树--前序遍历、中序遍历、后序遍历、层次遍历
- 树的非递归遍历(深度优先:前|中|后序遍历) & (广度优先:层次遍历)
- 二叉树生成,前序、中序、后序、层次遍历的小例子!
- 关于二叉树的所有 前序、中序、后序、层次遍历
- 二叉树的遍历(前序、中序、后序、层次)
- 算法实验 二叉树的创建和前序-中序-后序-层次 遍历
- 二叉树的非递归前序、中序、后序、层次遍历
- 数据结构-二叉树的前序、中序、后序、层次遍历
- 二叉树的创建、二叉排序树、前序、中序、后序、层次遍历
- 二叉树的前序,后序,中序,层次遍历
- 二叉树的遍历(前序、中序、后序、层次)
- 二叉树的遍历(前序、中序、后序、层次)
- LintCode 二叉树的前序,中序,后序,层次遍历
- jQuery-强大的jQuery选择器 (详解)[转]
- TechStars新推Accelerato.rs,创业公司申请加速项目的必备
- Java 常用正则表达式(收集)
- 清除div下面所有的node
- [C++] 关于gdb断点的真相
- 四叉树的前序,后序,层次遍历
- oracle 11g迁移数据库到oracle 10g
- while((ch=getchar())!=EOF && ch != '\n')
- VB6.0FTP操作
- %和&运算的共同点。
- java 线程详解
- Optimizing Data Access and Messaging-Managing Connections – Throttling, latency and transactions
- 使用intent调用Android系统自带应用
- 动态子类化CComboBox以得到子控件EDIT及LISTBOX