递归与非递归求树的高度

递归求树高

typedef struct BTNode
{
	char data;
	struct BTNode *lchild;
	struct BTNode *rchild;
}BTNode;
 int TreeDepth(BTNode* pRoot)
    {
        if(pRoot==NULL)
            return 0;
        int left=TreeDepth(pRoot->left);
        int right=TreeDepth(pRoot->right);
        return (left>right?left:right)+1;
    }

非递归求树高

typedef struct BTree{
	int data;
	struct BTree*Lnext;
	 struct BTree*Rnext;
};
int depth(BTree t){
	if(t==NULL){
		return 0;
	}
	BTree Q[maxsize];//队列 
	int front =-1,rear=-1;
	int last=0,level=0;
	Q[++rear]=t
	while(front<rear){
		BTree p=Q[front++];
		if(p->Lnext!=NULL){
			Q[++rear]=p->Lnext;
		}
		if(p->Rnext!=NULL){
			Q[++rear]=p->Rnext;
		}
		if(front==last){
			level++;
			last=rear;
		}
			
	} 
	return level;	
}

判断树宽的最大值

typedef struct queue{
	Bitree data[maxsize];
	int level[maxsize];
	int front ,rear;
}Qu;
 int song(Bitree T){
 	Qu.front=Qu.rear=-1;
 	Qu.data[++rear]=T;
 	Qu.level[rear]=1;
 	while(front<rear){
 		Bitree p=Qu.data[++front];
 		int k=Qu.level[front];
 		if(p->Lchild!=NULL){
 			Qu.data[++rear]=p->Lchild;
 			Qu.level[rear]=k+1;
		 }
		 if(p->Rchild!=NULL){
 			Qu.data[++rear]=p->Rchild;
 			Qu.level[rear]=k+1;
		 }
	 }
	 int max=1,i=0,k=1;
	 while(i<=Qu.rear){
	 	int n=0;
	 	while(Qu.level[i]==k){
	 		i++;
	 		n++;
		 }
		 if(max<n){
		 	max=n;
		 }
		 k=Qu.level[i];
	 }
	 
 }

请写出相应存储结构上的计算有向图G出度为0的顶点个数的算法。
标准答案:
邻接表结构中的边表恰好就是出边表。
因此,其表头数组中firstarc域为空的个数等于出度为零的元素个数。

Void sum_zero2 (AdjList a[], int count) 
/* count的初值为0,a为有向图的邻接表*/
{ for (I=0; I<n; I++)
if (a[I].firstarc==NULL) count++;
} 

合并两个有序的链表

 p hebeng(p head1,p head2){
 	p next3,next4,next6;
 p next5=(p)malloc(sizeof(struct link));
	next5->data=-1;
 	next6=next5;
 	next3=head1->next;
 	next4=head2->next;
 	while(next3!=NULL&&next4!=NULL){
 		if(next3->data>next4->data){
 			next5->next=next4;
 			next5=	next5->next;
 			next4=next4->next;
		 }
		 else{
		 		next5->next=next3;
 			next5=	next5->next;
 			next3=next3->next;
		 }
	 }
	 while(next3!=NULL){
	 	next5=next3->next;
	 	next3=next3->next;
	 }
	 	 while(next4!=NULL){
	 	next5=next4->next;
	 	next4=next4->next;
	 }
 	return next6;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小宋加油啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值