数据结构学习 队列 二叉树
2016-05-20 22:12
501 查看
一、队列
typedef int Position;
struct QNode {
ElementType *Data; /* 存储元素的数组 */
Position Front, Rear; /* 队列的头、尾指针 */
int MaxSize; /* 队列最大容量 */
};
typedef struct QNode *Queue;
Queue CreateQueue( int MaxSize )
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
Q->Front = Q->Rear = 0;
Q->MaxSize = MaxSize;
return Q;
}
bool IsFull( Queue Q )
{
return ((Q->Rear+1)%Q->MaxSize == Q->Front);
}
bool AddQ( Queue Q, ElementType X )
{
if ( IsFull(Q) ) {
printf("队列满");
return false;
}
else {
Q->Rear = (Q->Rear+1)%Q->MaxSize;
Q->Data[Q->Rear] = X;
return true;
}
}
bool IsEmpty( Queue Q )
{
return (Q->Front == Q->Rear);
}
ElementType DeleteQ( Queue Q )
{
if ( IsEmpty(Q) ) {
printf("队列空");
return ERROR;
}
else {
Q->Front =(Q->Front+1)%Q->MaxSize;
return Q->Data[Q->Front];
}
}
二、二叉树
typedef struct Tree *binTree;
a4a5
typedef binTree Position;
struct Tree{
ElementType Date;
binTree Left;
binTree Right;
}
/*先序*/
void PreorderTraversal(binTree BT){
printf("%d",BT->Date);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
/*中序*/
void InorderTraversal(binTree BT){
InorderTraversal(BT->Left);
printf("%d",BT->Date);
InorderTraversal(BT->Right);
}
/*后序*/
void PostorderTraversal(binTree BT){
PostorderTraversal(BT->Left);
PostorderTraversal(BT->Right);
printf("%d",BT->Date);
}
/*非递归中序*/
void InorderTraversal(binTree BT){
binTree T=BT;
Stack S=CreateStack(MaxSize);
while(T||IsEmpty(S)){
while(T){
Push(S,T);
T=T->Left;
}
if(!IsEmpty(S)){
T=Pop(S);
printf("%5d",T->Date);
T=T->Right;
}
}
}
/*非递归先序*/
void InorderTraversal(binTree BT){
binTree T=BT;
Stack S=CreateStack(MaxSize);
while(T||IsEmpty(S)){
while(T){
Push(S,T);
printf("%5d",T->Date);
T=T->Left;
}
if(!IsEmpty(S)){
T=Pop(S);
T=T->Right;
}
}
}
void LevelorderTraversal ( BinTree BT )
{
Queue Q;
BinTree T;
if ( !BT ) return; /* 若是空树则直接返回 */
Q = CreatQueue(); /* 创建空队列Q */
AddQ( Q, BT );
while ( !IsEmpty(Q) ) {
T = DeleteQ( Q );
printf("%d ", T->Data); /* 访问取出队列的结点 */
if ( T->Left ) AddQ( Q, T->Left );
if ( T->Right ) AddQ( Q, T->Right );
}
}
typedef int Position;
struct QNode {
ElementType *Data; /* 存储元素的数组 */
Position Front, Rear; /* 队列的头、尾指针 */
int MaxSize; /* 队列最大容量 */
};
typedef struct QNode *Queue;
Queue CreateQueue( int MaxSize )
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
Q->Front = Q->Rear = 0;
Q->MaxSize = MaxSize;
return Q;
}
bool IsFull( Queue Q )
{
return ((Q->Rear+1)%Q->MaxSize == Q->Front);
}
bool AddQ( Queue Q, ElementType X )
{
if ( IsFull(Q) ) {
printf("队列满");
return false;
}
else {
Q->Rear = (Q->Rear+1)%Q->MaxSize;
Q->Data[Q->Rear] = X;
return true;
}
}
bool IsEmpty( Queue Q )
{
return (Q->Front == Q->Rear);
}
ElementType DeleteQ( Queue Q )
{
if ( IsEmpty(Q) ) {
printf("队列空");
return ERROR;
}
else {
Q->Front =(Q->Front+1)%Q->MaxSize;
return Q->Data[Q->Front];
}
}
二、二叉树
typedef struct Tree *binTree;
a4a5
typedef binTree Position;
struct Tree{
ElementType Date;
binTree Left;
binTree Right;
}
/*先序*/
void PreorderTraversal(binTree BT){
printf("%d",BT->Date);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
/*中序*/
void InorderTraversal(binTree BT){
InorderTraversal(BT->Left);
printf("%d",BT->Date);
InorderTraversal(BT->Right);
}
/*后序*/
void PostorderTraversal(binTree BT){
PostorderTraversal(BT->Left);
PostorderTraversal(BT->Right);
printf("%d",BT->Date);
}
/*非递归中序*/
void InorderTraversal(binTree BT){
binTree T=BT;
Stack S=CreateStack(MaxSize);
while(T||IsEmpty(S)){
while(T){
Push(S,T);
T=T->Left;
}
if(!IsEmpty(S)){
T=Pop(S);
printf("%5d",T->Date);
T=T->Right;
}
}
}
/*非递归先序*/
void InorderTraversal(binTree BT){
binTree T=BT;
Stack S=CreateStack(MaxSize);
while(T||IsEmpty(S)){
while(T){
Push(S,T);
printf("%5d",T->Date);
T=T->Left;
}
if(!IsEmpty(S)){
T=Pop(S);
T=T->Right;
}
}
}
void LevelorderTraversal ( BinTree BT )
{
Queue Q;
BinTree T;
if ( !BT ) return; /* 若是空树则直接返回 */
Q = CreatQueue(); /* 创建空队列Q */
AddQ( Q, BT );
while ( !IsEmpty(Q) ) {
T = DeleteQ( Q );
printf("%d ", T->Data); /* 访问取出队列的结点 */
if ( T->Left ) AddQ( Q, T->Left );
if ( T->Right ) AddQ( Q, T->Right );
}
}
相关文章推荐
- 数据结构----希尔排序(最小增量排序)
- 数据结构基础知识(一)——链表
- 数据结构----直接插入排序
- 串的基础知识
- sdutoj 3379 ///数据结构实验之查找七:线性之哈希表 ti(哈希)
- 树链剖分
- 数据结构上机测试1:顺序表的应用
- 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction
- 数据结构课后题目源码
- 常用数据结构
- List Leaves (25分)
- 栈Stack
- 数据结构:线段树
- 数据结构课后题目源码
- 【数据结构】大量数据(20万)的快速排序的递归与非递归算法、三数取中思想
- 二叉搜索树小结
- 数据结构的实现细节
- 二叉堆 build算法 c++ 数据结构与算法
- MySQL索引背后的数据结构及算法原理
- POJ 3294 后缀数组+二分