广度优先遍历二叉树

//3、广度优先遍历二叉树。   
#include   <stdio.h>   
#include   <string.h>   
#include   <stdlib.h>

//int   i   =0;
typedef   struct   treenode   /*树节点结构体定义*/   
{ 
    treenode(char* name)  
    {  
        strcpy(this->data,name);  
        lchild=NULL;  
        rchild=NULL;  
    }  
    treenode* InsertSbOnMyLeft(treenode* Sb)  
    {  
        this->lchild=Sb;  
        return Sb;  
    }  
    treenode* InsertSbOnMyRight(treenode* Sb)  
    {  
        this->rchild=Sb;  
        return Sb;  
    } 	
char   data[256];   
struct   treenode   *lchild;   
struct   treenode   *rchild;   
}node;   

typedef   struct   queuenode   /*队列节点结构体定义*/   
{   
node*   info;   
struct   queuenode   *next;   
}qnode;   

typedef   struct   /*将头尾指针封装在一起*/   
{   
qnode   *front;   
qnode   *rear;   
}lqueue;   

lqueue   *   initlqueue()//   (lqueue   *q)   /*初始化一个空队列*/   
{ 
	lqueue   *q;
	q   =   new lqueue();   
	if   (q)   
	{   
		q   -> front   =   0;   
		q   -> rear   =   0;   
	}   
return   q;   
}   

void   destroylqueue   (lqueue   *q)   /*销毁队列*/   
{   
qnode   *p;   
if(q)   
{   
	while   (q   -> front)   
	{   
	p   =   q   -> front;   
	q   -> front   =   q   -> front   -> next;   
	delete p;   
	}//while   (q   -> front)     
delete q;   
}//if (q)     
//q   =   0;   
}   

int   isempty   (lqueue   *q)   /*判断队列是否为空*/   
{   
return   q   -> front   ==   0;   
}   

void   enqueue   (lqueue   *q,   node*   el)   /*入队列(尾插入)*/   
{   
qnode   *p;   
p   =   new qnode();   
if(!p)   
{   
printf   ( "内存已满! ");   
exit   (0);   
}   
p   -> info   =   el;   
p   -> next   =   0;   
if   (!isempty(q))   
{   
q-> rear   -> next   =   p;  //插入链表尾,作为新的尾 
q   -> rear   =p; //   q   -> rear   -> next;   
}   
else   
q-> front   =   q   -> rear   =   p;   
}   

node*   dequeue   (lqueue   *q)   /*出队列(头删除)*/   
{   
node   *el   =   q   -> front   -> info;   
qnode   *temp   =   q   -> front;   
if   (q   -> front   ==   q   -> rear)   
q   -> front   =   q   -> rear   =   0;   
else   
q   -> front   =   q   -> front   -> next;//头节点往后推
delete temp;//释放出队的节点qnode   
return   el;//返回出队的节点qnode的节点node      
} 
  
void   breadthfirst   (node   *root)   /*广度优先遍历*/   
{   
lqueue   *q;   
q   =   initlqueue();//;   (q);   
node   *p   =   root;   
if   (p)   
{   
enqueue   (q,p);//只插入了一个 node  *p = root 到lqueue   *q
while   (!isempty   (q))   
{   
	p   =   dequeue   (q);//刚插入一个node就让让它出队列了   
	printf   ( "%s   ",   p   -> data);  
	//下面将左右孩子压入队列

	if   (p   -> lchild   !=   0)
	enqueue   (q,   p   -> lchild);/*左孩子入队列*/  	
	if   (p   -> rchild   !=   0)   
	enqueue   (q,   p   -> rchild);/*右孩子入队列*/	 
 
  
}//while   (!isempty   (q))     
printf   ( "\n ");   
}//if   (p)    
else   
printf   ( "这是一颗空树!\n ");   
destroylqueue   (q);   
}


void main()  
{  
	node *A=NULL;  
	A=new node("A");  
	node * B1=A->InsertSbOnMyLeft(new node("B1")); 
	node * B2=A->InsertSbOnMyRight(new node("B2")); 

	node * C1=B1->InsertSbOnMyLeft(new node("C1")); 
	node * C2=B1->InsertSbOnMyRight(new node("C2")); 
	node * C3=B2->InsertSbOnMyLeft(new node("C3")); 
	node * C4=B2->InsertSbOnMyRight(new node("C4")); 

	node * D1=C1->InsertSbOnMyLeft(new node("D1")); 
	node * D2=C1->InsertSbOnMyRight(new node("D2")); 
	node * D3=C2->InsertSbOnMyLeft(new node("D3")); 
	node * D4=C2->InsertSbOnMyRight(new node("D4")); 
	node * D5=C3->InsertSbOnMyLeft(new node("D5")); 
	node * D6=C3->InsertSbOnMyRight(new node("D6")); 
	node * D7=C4->InsertSbOnMyLeft(new node("D7")); 
	node * D8=C4->InsertSbOnMyRight(new node("D8")); 
breadthfirst(A); 
}

/*  
二叉搜索树的广度优先遍历   
A   B1   B2   C1   C2   C3   C4   D1   D2   D3   D4   D5   D6   D7   D8
 Press any key to continue
//*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值