C语言队列实现广度优先遍历
2014-03-23 18:15
375 查看
上一篇只是定义了数据结构。
基于上一篇http://blog.csdn.net/xzongyuan/article/details/21832237增加了队列操作。
打印出来的结果是这样的,没有分层打印,分层打印要计算几个参数,比较麻烦,所以我打算以一个正确的广度遍历为基础,对原先的数据结构和算法进行修改。代码比较繁杂,后续打算整理出头文件和多个c文件,脉络会更清晰,水平有限,仅供参考。
insert node to queue
root is 73
insert :34
insert :46
insert :58
insert :12
insert :43
insert :41
insert :35
insert :98
insert :51
creat tree succuess!Tree heigh is:6
_______________________
breath begin,enter root:
73 34 98 12 46 43 58 41 51 35
-----------
breath end!
-----------
基于上一篇http://blog.csdn.net/xzongyuan/article/details/21832237增加了队列操作。
打印出来的结果是这样的,没有分层打印,分层打印要计算几个参数,比较麻烦,所以我打算以一个正确的广度遍历为基础,对原先的数据结构和算法进行修改。代码比较繁杂,后续打算整理出头文件和多个c文件,脉络会更清晰,水平有限,仅供参考。
insert node to queue
root is 73
insert :34
insert :46
insert :58
insert :12
insert :43
insert :41
insert :35
insert :98
insert :51
creat tree succuess!Tree heigh is:6
_______________________
breath begin,enter root:
73 34 98 12 46 43 58 41 51 35
-----------
breath end!
-----------
#include<stdio.h> #include<stdlib.h> #include<time.h> #define NUM 10 typedef struct _node { int value; struct _node *left; struct _node *right; }TNode,*Tree; //add a *next in q_node is my purpose //other wise , we need to add in the Tree node struct //So, for the sake of doesn't modify the struct of tree //I design a q_node struct to include it //we can use define command to make it as a template. typedef struct _q_node { TNode *t_node; struct _q_node *next; }QNode; typedef struct _Queue { QNode *head; QNode *tail; }Queue; Queue* init_queue() { Queue *queue=(Queue*)malloc(sizeof(Queue)); queue->head = queue->tail = NULL; return queue; } int enQueue(Queue *pQueue,TNode *pTNode) { QNode *pQNode = (QNode *)malloc(sizeof(QNode)); pQNode->t_node = pTNode; if(pQueue->head == NULL) {//when it's empty pQueue->head = pQNode; pQueue->tail = pQNode; } else { pQueue->tail->next = pQNode; pQueue->tail = pQNode; } } QNode* deQueue(Queue *pQueue) { if(pQueue->head == NULL) { return NULL; } QNode *deNode= pQueue->head; pQueue->head = pQueue->head->next; return deNode; } TNode* init_TNode(int value) { TNode *new_node = (TNode*)malloc(sizeof(TNode)); new_node->value=value; new_node->left = new_node->right = NULL; return new_node; } //0:empty int ifEmpty(Queue *pQueue) { if(pQueue->head == NULL) { //printf("empty tree\n"); return 0; } //printf("queue is not empty\n"); return 1; } int insert_tree(Tree pTree,int pValue) { //found NULL sub tree, then add to his father->left if(!pTree) { return 0; } TNode *tNode = init_TNode(pValue); if(tNode==NULL) { printf("create TNode error!\n"); return 0; } if(pValue < pTree->value) if(insert_tree(pTree->left,pValue)==0) { //no left child any more,set a new left child to pTree pTree->left = tNode; printf("insert :%d\n",pValue); } if(pValue > pTree->value) if(insert_tree(pTree->right,pValue)==0) { pTree->right = tNode; printf("insert :%d\n",pValue); } } Tree creatTree() { srand(time(NULL)); Tree root = init_TNode(rand()%100); printf("root is %d\n",root->value); int i ; for(i=1;i<NUM;i++) { insert_tree(root,rand()%100); } printf("creat tree succuess!Tree heigh is:%d\n",get_tree_height(root)); return root ; } int get_tree_height(Tree pRoot) { if(!pRoot) { return 0; } int lh=0,rh=0; lh = get_tree_height(pRoot->left); rh = get_tree_height(pRoot->right); return (lh<rh)?(rh+1):(lh+1); } int breath_travel(Tree pRoot,Queue *pQueue) { if(!pRoot) { return 0; } enQueue(pQueue,pRoot); printf("_______________________\n"); printf("breath begin,enter root:\n"); while(ifEmpty(pQueue)!=0) { QNode *qNode = deQueue(pQueue); //make suer every enQueue Node is not NULL if(qNode->t_node->left!=NULL) enQueue(pQueue,qNode->t_node->left); if(qNode->t_node->right!=NULL) enQueue(pQueue,qNode->t_node->right); //print the tree node value printf("%d ",qNode->t_node->value); } printf("\n-----------\nbreath end!\n-----------\n"); return 1; } int main() { Queue *queue=init_queue(); int i; ifEmpty(queue); printf("insert node to queue\n"); Tree root = creatTree(); if(!root) { printf("create Tree failed!\n"); return 0; } breath_travel(root,queue); // free(queue); return 0; }
相关文章推荐
- 图的邻接表存储 深度优先遍历 广度优先遍历 C语言实现
- 图的广度优先遍历(c语言实现)
- 图的邻接矩阵存储 深度优先遍历 广度优先遍历 C语言实现
- 广度优先遍历队列实现
- 图--广度优先遍历/深度优先遍历(c语言实现)
- c语言实现图的深度优先遍历和广度优先遍历
- 容易理解的python用队列实现广度优先遍历文件
- 图的邻接表存储 深度优先遍历 广度优先遍历 C语言实现
- C++非递归队列实现二叉树的广度优先遍历
- 无向图 广度优先遍历 c语言实现
- python 用栈和队列实现二叉树的深度优先遍历(三种)和广度优先遍历
- [二叉树专题]:广度优先:按层次遍历二叉树的非递归实现||使用队列实现层次遍历二叉树
- C语言以邻接矩阵为存储结构的图的构造以及广度优先,深度优先遍历
- 二叉树的深度优先遍历(栈)和广度优先遍历(队列)
- 如何实现广度优先遍历(BFS)
- 如何实现广度优先遍历(BFS) .
- 用邻接表实现图的深度优先遍历、广度优先遍历、最短路径(无权图)
- 利用广度优先遍历(BFS)计算最短路径 - Java实现
- 求最大网络流的C++实现(利用广度优先遍历的思想)
- C++实现图的邻接矩阵存储和广度、深度优先遍历实例分析