2017年学习的数据结构作的笔记,一些算法思想(伪代码实现)在日常学习中仍显得很重要,很多很多,在此记录整理不断补充,反复看反复理解反复记忆,加油!
1、孩子-兄弟链(child-sibiling)表示法的树的叶子数量
int leaf(csTree T)
{
if(T==Null) return 0;
else if(T->firstchild==Null)
return leaf(T->nextsibling)+1;
else
return leaf(T->firstchild)+leaf(T->nextsibling);
}//firstchild相当于lchild,nextsibling相当于rchild
2、孩子-兄弟链表示法的树的度
void degree(csTree T,int &d)
{
if(T!=Null)
{
if(t->firstchild!=Null)
{
p=T->firstchild;
n=1;
while(p->nextsibling!=Null)
{
p=p->nextsibling;
n++;
}
if(n>d)
d=n;
}
degree(T->firstchild,d);
degree(T->nextsibling,d);
}
}//调用前第2个实际参数赋0
3、孩子-兄弟链表示法的树的深度
int depth(csTree T)
{
if(T==Null)
return 0;
else
return max(1+depth(T->firstchild),depth(T->nextsibling));
}
4、按凹入表方式打印孩子-兄弟链表存储的树
void printCS(csTree T,int column)
{
if(T!=Null)
{
for(int i=1;i<column;i++)
{
printf(" ");//空格键
putchar(T->data);
printf("\n");
printCS(T->firstchild,column+1);
printCS(T->nextsibling,column);
}
}
}//调用时第2个实际参数为1