孩子兄弟鏈表

typedef struct CSTNode{
	int data;
	struct CSTNode *firstchild,*nextsibling;
}CSTNode,*CSTree; 

int height(CSTree T){
	if(!T)return 0;
	int hc=height(T->firstchild);
	int hs=height(T->nextsibling);
	if(hc+1>hs)
		return hc+1;
	else
		return hs;
}

int leaves(CSTree T){
	if(!T)return 0;
	if(T->firstchild==NULL)return 1+leaves(T->nextsibling);
	return leaves(T->firstchild)+leaves(T->nextsibling);
}

void degree(CSTree T,int &d){
	if(!T)return;
	if(T->firstchild!=NULL){
		CSTree p=T->firstchild;
		int i=1;
		while(p->nextsibling!=NULL){
			p=p->nextsibling;
			i++;
		}
		if(i>d)d=i;
	}
	degree(T->firstchild,d);
	degree(T->nextsibling,d);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章