C语言队列实现广度优先遍历

上一篇只是定义了数据结构。

基于上一篇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;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值