您的位置:首页 > 编程语言 > C语言/C++

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!

-----------

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: