数据结构学习 队列 二叉树
来源:互联网 发布:javascript动画 编辑:程序博客网 时间:2024/06/07 09:18
一、队列
- 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;
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 );
- }
- }
- 数据结构学习 队列 二叉树
- 【数据结构 队列 + 二叉树】
- 【数据结构学习】二叉树
- 内核数据结构:链表,队列,映射二叉树
- 数据结构(栈,队列,链表,二叉树)
- 【数据结构】数组、链表、栈、队列、二叉树
- 数据结构学习之二叉树
- 数据结构学习—二叉树
- 数据结构学习笔记:二叉树
- 数据结构学习笔记 --- 二叉树
- 数据结构学习笔记:二叉树
- 数据结构学习之二叉树
- 数据结构学习五:二叉树
- 数据结构学习笔记 --- 二叉树
- 数据结构学习之二叉树
- 【数据结构】二叉树的学习
- 数据结构学习笔记:二叉树
- 数据结构学习-平衡二叉树
- 实习入职第二天:PackageInfo、ResolveInfo
- 啦啦啦啦~Django1.96---编写第一个Django应用(5)
- 安装tensorflow
- Spring的组件装配
- 批处理之 正确使用for语句 以及!errorlevel!、%errorlevel% 与 DelayedExpansion用法详解
- 数据结构学习 队列 二叉树
- 2014-2015 ACM-ICPC Northeastern European Regional Contest (NEERC 14)
- 静态碎片切换 和动态viewpage切换和view颜色切换
- TensorFlow实战之:Quick Start
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- bzoj4569[Scoi2016]萌萌哒
- LayoutInflater的错误用法(Avoid passing null as the view root )
- Linux下的进程通信方式: 管道通信详解
- 简单的 URL 编程