数据结构学习 队列 二叉树

一、队列

  1. typedef int Position;
  2. struct QNode {
  3.     ElementType *Data;     /* 存储元素的数组 */
  4.     Position Front, Rear;  /* 队列的头、尾指针 */
  5.     int MaxSize;           /* 队列最大容量 */
  6. };
  7. typedef struct QNode *Queue;
  8.  
  9. Queue CreateQueue( int MaxSize )
  10. {
  11.     Queue Q = (Queue)malloc(sizeof(struct QNode));
  12.     Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
  13.     Q->Front = Q->Rear = 0;
  14.     Q->MaxSize = MaxSize;
  15.     return Q;
  16. }
  17.  
  18. bool IsFull( Queue Q )
  19. {
  20.     return ((Q->Rear+1)%Q->MaxSize == Q->Front);
  21. }
  22.  
  23. bool AddQ( Queue Q, ElementType X )
  24. {
  25.     if ( IsFull(Q) ) {
  26.         printf("队列满");
  27.         return false;
  28.     }
  29.     else {
  30.         Q->Rear = (Q->Rear+1)%Q->MaxSize;
  31.         Q->Data[Q->Rear] = X;
  32.         return true;
  33.     }
  34. }
  35.  
  36. bool IsEmpty( Queue Q )
  37. {
  38.     return (Q->Front == Q->Rear);
  39. }
  40.  
  41. ElementType DeleteQ( Queue Q )
  42. {
  43.     if ( IsEmpty(Q) ) { 
  44.         printf("队列空");
  45.         return ERROR;
  46.     }
  47.     else  {
  48.         Q->Front =(Q->Front+1)%Q->MaxSize;
  49.         return  Q->Data[Q->Front];
  50.     }
  51. }

二、二叉树

typedef struct Tree *binTree;
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;
}
}
}

  1. void LevelorderTraversal ( BinTree BT )
  2.     Queue Q; 
  3.     BinTree T;
  4.  
  5.     if ( !BT ) return/* 若是空树则直接返回 */
  6.      
  7.     Q = CreatQueue(); /* 创建空队列Q */
  8.     AddQ( Q, BT );
  9.     while ( !IsEmpty(Q) ) {
  10.         T = DeleteQ( Q );
  11.         printf("%d ", T->Data); /* 访问取出队列的结点 */
  12.         if ( T->Left )   AddQ( Q, T->Left );
  13.         if ( T->Right )  AddQ( Q, T->Right );
  14.     }
  15. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值