int CheckIfCompleteBinaryTree(struct TNode* root,int n){ QNode* queue=CreateQueue(n); struct TNode* tmpNode; EnQueue(queue,root); int result; while(!IsEmptyQueue(queue)){ tmpNode=DeQueue(queue); if(IsFullNode(tmpNode)){ EnQueue(queue,tmpNode->left); EnQueue(queue,tmpNode->right); }else if(tmpNode->right){ result=0; break; }else if(tmpNode->left){ continue; } else{ while(IsLeafNode(tmpNode) && !IsEmptyQueue(queue)){ tmpNode=DeQueue(queue); } if(IsEmptyQueue(queue)){ result=1; break; }else{ result=0; break; } } } return result; }
void printtopsortorder(graph g) { Queue q; ptrtoedge tempedge; int *degree; int count,count2,tempint; q = NewQueue(); printf("\n"); /*set up the degree array*/ degree = malloc(sizeof(int)*g->numvertices); if(degree == NULL) FatalError("\nout of memory"); for(count=0;count<g->numvertices;count++) degree[count] = 0; for(count = 0;count<g->numvertices;count++) { tempedge = g->array[count]; while(tempedge != NULL) { degree[tempedge->vertex]++; tempedge = tempedge->next; } } /*sort and print out the sorted vertices*/ count2 = g->numvertices; while(count2 != 0) { /*get vertices to print out this time around*/ for(count=0;count<g->numvertices;count++) { if(degree[count] == 0) { Enqueue(count,q); } } /*in case of a cycle exit wiht error*/ if(IsEmptyQueue(q)) FatalError("There is a cycle involving the remaining vertices."); /*adjust degree for the removed vertices and print out the vertex*/ while(!IsEmptyQueue(q)) { count2--; tempint = Dequeue(q); printf("%d ",tempint); degree[tempint] = -1; tempedge = g->array[tempint]; while(tempedge != NULL) { degree[tempedge->vertex] -= 1; tempedge = tempedge->next; } } } DeleteQueue(q); }
void LevelOrderTraversal(struct TNode* root,int n){ if(!root){ return; } QNode* queue=CreateQueue(n); EnQueue(queue,root); struct TNode* tmpNode; while(!IsEmptyQueue(queue)){ tmpNode=DeQueue(queue); if(tmpNode){ printf("%d ",tmpNode->data); } if(tmpNode->left){ EnQueue(queue,tmpNode->left); } if(tmpNode->right){ EnQueue(queue,tmpNode->right); } } }
tree *findNode(tree **root,int data) { queue *Q; tree *temp; if(!(*root)){ return NULL;} Q=create(); Enqueue(Q,*root); while(!(IsEmptyQueue(Q))) { temp=Dequeue(Q); if(temp->data == data) {deleteQueue(Q); return temp; } if(temp->left) Enqueue(Q,temp->left); if(temp->right) Enqueue(Q,temp->right); } deleteQueue(Q); return NULL; }
int BFSTraversal(GNode* graphRoot,int u,int v){ QNode* queue=CreateQueue(graphRoot->count); EnQueue(queue,u); int data; ALNode* tmpNode; while(!IsEmptyQueue(queue)){ data=DeQueue(queue); if(visited[data]==0){ //printf("%d ",data); tmpNode=graphRoot->GArray[data]->head; while(tmpNode){ if(tmpNode->destination==v){ return 1; } if(visited[tmpNode->destination]==0){ EnQueue(queue,tmpNode->destination); } tmpNode=tmpNode->next; } visited[data]=1; } } return 0; }
void Dequeue(Queue Q) { if (IsEmptyQueue(Q)) { return; } else { Q->Size--; Q->Front = Succ(Q->Front, Q); } }
ElementQueueType Front(Queue Q) { if (!IsEmptyQueue(Q)) { return Q->Array[Q->Front]; } /* Return value to avoid warnings from the compiler */ return 0; }
int NBElmt(Queue Q) /* Mengirimkan banyaknya elemen queue. Mengirimkan 0 jika Q kosong. */ { if (IsEmptyQueue(Q)) { return 0; } else { return Tail(Q) - Head(Q) + 1 + (Head(Q) > Tail(Q) ? MaxEl(Q) : 0); } }
int LevelOrderTraverse(tree *root) { int count=0; queue *Q; tree *temp; if(!root){ return 0;} Q=create(); Enqueue(Q,root); Enqueue(Q,NULL); while(!(IsEmptyQueue(Q))) { temp=Dequeue(Q); if(temp){ printf("%d\t",temp->data); count++; } //completion of current Level if(temp == NULL) { if(!IsEmptyQueue(Q)){ Enqueue(Q,NULL); printf("\n"); } } else { if(temp->left) Enqueue(Q,temp->left); if(temp->right) Enqueue(Q,temp->right); } } deleteQueue(Q); return count; }
int heightOfBinary(tree *root) { int level=1; queue *Q; if(!root){ return 0;} Q=create(); Enqueue(Q,root); //End Of first Level Enqueue(Q,NULL); while(!(IsEmptyQueue(Q))) { root=Dequeue(Q); //completion of current Level if(root == NULL) { if(!IsEmptyQueue(Q)){ Enqueue(Q,NULL); level++; } } else { if(root->left) Enqueue(Q,root->left); if(root->right){ Enqueue(Q,root->right); } } } deleteQueue(Q); return level; }
ElementQueueType FrontAndDequeue(Queue Q) { ElementQueueType X = 0; if (IsEmptyQueue(Q)) { return X; } else { Q->Size--; X = Q->Array[Q->Front]; Q->Front = Succ(Q->Front, Q); } return X; }
struct BinaryTreeNode *DeQueue(struct DynArrayQueue *Q) { if(IsEmptyQueue(Q)) { printf("Queue is Empty\n"); return NULL; } else { struct BinaryTreeNode *data = Q->array[Q->front]; if(Q->front == Q->rear) Q->front = Q->rear = -1; else Q->front = (Q->front + 1) % Q->capacity; return data; } }
TNode* DeQueue(QNode* queue){ if(IsEmptyQueue(queue)){ printf("\nNo data"); return NULL; }else{struct TNode* data=queue->Array[queue->front]; if(queue->front==queue->rear){ queue->front=-1; queue->rear=-1; }else{ queue->front=(queue->front+1)%queue->capacity; } return data; } }
int DeQueue() { int data =0; struct ListNode *temp; if(IsEmptyQueue(front)) { printf("Queue is empty"); return 0; } else { temp = front; data = front->pid; front = front->next; free(temp); } return data; }
int DeQueue(QNode* queue){ int data=-1; if(IsEmptyQueue(queue)){ printf("\nUnderflow..\n"); } else{ data=queue->Array[queue->front]; if(queue->front==queue->rear){ queue->front=queue->rear=-1; }else{ queue->front=(queue->front +1)%queue->capacity; } } return data; }
void FindMinDistance(GNode* graph,int* distance,int sourceNode){ int i; for(i=0;i<graph->count;i++){ present[i]=0; } distance[sourceNode]=0; ALNode* tmpNode; QNode* queue=CreateQueue(graph->count); EnQueue(queue,sourceNode); present[sourceNode]=1; int data; int newDistance; while(!IsEmptyQueue(queue)){ data=DeQueue(queue); present[data]=0; tmpNode=graph->GArray[data]->head; while(tmpNode){ newDistance=distance[data]+tmpNode->wait; if(distance[tmpNode->destination]> newDistance){ distance[tmpNode->destination]=newDistance; // doesnt work if negative cycle formed if(present[tmpNode->destination]==0){ EnQueue(queue,tmpNode->destination); present[tmpNode->destination]=1; } } tmpNode=tmpNode->next; } } }
int SizeOfBinaryTreeNonRecrusive(struct BinaryTreeNode *root) { int size = 0; struct BinaryTreeNode *temp; struct DynArrayQueue *Q = CreateQueue(); if(!root) { return size; } EnQueue(Q, root); while(!IsEmptyQueue(Q)) { temp = DeQueue(Q); size++; if (temp->left) { EnQueue(Q, temp->left); } if (temp->right) { EnQueue(Q, temp->right); } } DeleteQueue(Q); return size; }
/* *** Primitif Add/Delete *** */ void Add(Queue *Q, infotypeQueue X) /* Proses: Menambahkan X pada Q dengan aturan FIFO */ /* I.S. Q mungkin kosong, tabel penampung elemen Q TIDAK penuh */ /* F.S. X menjadi TAIL yang baru, TAIL "maju" dengan mekanisme circular buffer */ { if (IsEmptyQueue(*Q)) { Head(*Q) = 1; Tail(*Q) = 1; } else if (Tail(*Q) == MaxEl(*Q)) { Tail(*Q) = 1; } else { ++Tail(*Q); } InfoTail(*Q) = X; }
void Insert(tree **root,int data) { queue *Q; tree *temp,*Tnode; Tnode=(tree*)malloc(sizeof(tree)); Tnode->data=data; Tnode->left=Tnode->right=NULL; if(!(*root)){ *root=Tnode; return;} Q=create(); Enqueue(Q,*root); while(!(IsEmptyQueue(Q))) { temp=Dequeue(Q); if(temp->left) { Enqueue(Q,temp->left); } else { temp->left=Tnode; deleteQueue(Q); return ; } if(temp->right){ Enqueue(Q,temp->right); } else{ temp->right=Tnode; deleteQueue(Q); return; } } deleteQueue(Q); return; }
/* Deepest Node in Binary Tree */ tree *depthOfTree(tree **root) { queue *Q; tree *temp,*temp1; if(!(*root)){ return NULL;} Q=create(); Enqueue(Q,*root); while(!(IsEmptyQueue(Q))) { temp=Dequeue(Q); if(temp->left) Enqueue(Q,temp->left); if(temp->right) Enqueue(Q,temp->right); } deleteQueue(Q); return temp; }
void InsertInBinaryTree(struct BinaryTreeNode *root, int insertData) { struct BinaryTreeNode *temp; struct BinaryTreeNode *insertNode; struct DynArrayQueue *Q = CreateQueue(); insertNode = (struct BinaryTreeNode *)malloc(sizeof(struct BinaryTreeNode)); if(!insertData) { printf("Memory Error!\n"); return; } insertNode->data = insertData; insertNode->left = NULL; insertNode->right = NULL; if (!root) { printf("!root!\n"); root = insertNode; return; } EnQueue(Q, root); while (!IsEmptyQueue(Q)) { temp = DeQueue(Q); if (temp->left) { EnQueue(Q, temp->left); } else { temp->left = insertNode; printf("%d insert into left of %d\n", insertNode->data, temp->data); DeleteQueue(Q); return; } if (temp->right) { EnQueue(Q, temp->right); } else { temp->right = insertNode; printf("%d insert into right of %d\n", insertNode->data, temp->data); DeleteQueue(Q); return; } } DeleteQueue(Q); }
/******************************************************************* 函数功能: 取队列中一个元素,fifo 入口参数: *element;数据 返 回 值: 1: 成功 0: 失败 ********************************************************************/ u8 GetQueue(_CANQUEUE* canqueue,u16* CAN_ID,u8 Msg[]) { CPU_SR_ALLOC();// 申请cpu_sr //// CPU_CRITICAL_ENTER(); // 关键段,关闭全局中断,视具体情况添加此语句 if(!IsEmptyQueue(canqueue))//队列非空 { _CANMSG CANMsg; memcpy(&CANMsg,&canqueue->Elem[canqueue->front],sizeof(_CANMSG));//得到数据 *CAN_ID = CANMsg.CAN_ID; memcpy(Msg,CANMsg.Msg,sizeof(CANMsg.Msg));//得到数据 // 队头加一 canqueue->front = (canqueue->front + 1) % MAX_CAN_QUEUE_SIZE ; CPU_CRITICAL_EXIT(); // 退出关键段,开启全局中断,视具体情况添加此语句 return 1; } else { CPU_CRITICAL_EXIT(); // 退出关键段,开启全局中断,视具体情况添加此语句 return 0; //无指定对头数据 } }
void InsertBinaryTree(TNode** root,int data,int n){ if((*root)==NULL){ (*root)=(struct TNode*)malloc(sizeof(TNode)); (*root)->data=data; (*root)->left=NULL; (*root)->right=NULL; return; } QNode* queue=CreateQueue(n); EnQueue(queue,(*root)); struct TNode* node=(struct TNode*)malloc(sizeof(TNode)); node->left=NULL; node->right=NULL; node->data=data; struct TNode* tmpNode; while(!IsEmptyQueue(queue)){ tmpNode=DeQueue(queue); if(!tmpNode->left){ tmpNode->left=node; return; }else{ EnQueue(queue,tmpNode->left); } if(!tmpNode->right){ tmpNode->right=node; return; }else{ EnQueue(queue,tmpNode->right); } } }
int BFS(int** array,int* parent,int s,int t,int n){ int* visited=(int*)malloc(sizeof(int)*n); QNode* queue=CreateQueue(n); int i; for(i=0;i<n;i++){ visited[i]=0; } int u,v; parent[s]=-1; visited[s]=1; EnQueue(queue,s); int data; while(!IsEmptyQueue(queue)){ u=DeQueue(queue); for(v=0;v<n;v++){ if(visited[v]==0 && array[u][v]>0){ EnQueue(queue,v); parent[v]=u; visited[v]=1; } } } return (visited[t]==1); }
void FindIt(TNode* testRoot,int* Array,int size){ int i; int sumAll=0; for(i=0;i<size;i++){ sumAll+=Array[i]; if(Array[i]%3==0){ EnQueue(testRoot->Q1,Array[i]); } else if(Array[i]%3==1){ EnQueue(testRoot->Q2,Array[i]); }else{ EnQueue(testRoot->Q3,Array[i]); } } /*testing printf("\nTesting first queue..\n\n"); Traverse(testRoot->Q1); printf("\nTesting first queue..\n\n"); Traverse(testRoot->Q2); printf("\nTesting first queue..\n\n"); Traverse(testRoot->Q3); end testing */ if(sumAll%3==1){ if(!IsEmptyQueue(testRoot->Q2)){ DeQueue(testRoot->Q2); } else if(!IsEmptyQueue(testRoot->Q3)){ DeQueue(testRoot->Q3); if(!IsEmptyQueue(testRoot->Q3)){ DeQueue(testRoot->Q3); } else{ printf("\nNot possibe\n\n"); return; } }else{ printf("\nNot possibe\n\n"); return; } } if(sumAll%3==2){ if(!IsEmptyQueue(testRoot->Q3)){ DeQueue(testRoot->Q3); } else if(!IsEmptyQueue(testRoot->Q2)){ DeQueue(testRoot->Q2); if(!IsEmptyQueue(testRoot->Q2)){ DeQueue(testRoot->Q2); } else{ printf("\nNot possibe\n\n"); return; } }else{ printf("\nNot possibe\n\n"); return; } } int* AuxArray=(int*)malloc(sizeof(int)*size); int index=0; while(!IsEmptyQueue(testRoot->Q1)){ AuxArray[index++]=DeQueue(testRoot->Q1); } while(!IsEmptyQueue(testRoot->Q2)){ AuxArray[index++]=DeQueue(testRoot->Q2); } while(!IsEmptyQueue(testRoot->Q3)){ AuxArray[index++]=DeQueue(testRoot->Q3); } qsort(AuxArray,index,sizeof(int),Compare); printf("\n\nMax digit is : \n\n"); for(i=0;i<index;i++){ printf("%d",AuxArray[i]); } }