js復(fù)雜樹形結(jié)構(gòu)的遍歷算法 二叉樹求葉子結(jié)點(diǎn)個(gè)數(shù)的算法(遞歸遍歷)?
二叉樹求葉子結(jié)點(diǎn)個(gè)數(shù)的算法(遞歸遍歷)?Int BTREE depth(BT->lchild){//find the depth of binary tree if(BT==null)//empt
二叉樹求葉子結(jié)點(diǎn)個(gè)數(shù)的算法(遞歸遍歷)?
Int BTREE depth(BT->lchild){//find the depth of binary tree if(BT==null)//empty tree returns 0return 0else{Int dep1=BTREE depth(BT->lchild)//遞歸調(diào)用逐層分析Int dep2=BTREE depth(BT->rchild)if(dep1>dep2)return dep2 1}}Int leave(bitnode*BT){//find二叉樹中的葉節(jié)點(diǎn)數(shù)if(BT==null)返回0else{if(BT->lchild==null)&這是學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)的練習(xí)。它使用遞歸形式。理解的時(shí)候需要考慮一下,但是函數(shù)相對(duì)簡單。
用C語言編程實(shí)現(xiàn)二叉樹的中序遍歷算法?
#Include
#Include
struct bitnode*stack[100
]struct bitnode//define struct
{
char data
struct bitnode*lchild,*rchild
}
void later(struct bitnode*&)//preorder create tree
{
char Ch
scanf scanf(%C“,& CH)
if(CH=”)
P=null
else
{
P=(struct bitnode*)malloc(sizeof(struct bitnode*)bitnode)
P->data=CH
later(P->lchild)
later(P->rchild)
}
void print(struct bitnode*P)//預(yù)序遍歷(輸出二叉樹)
{
int i=-1(1)
]{
而(P!=null)
]{
堆棧[i]=P->rchild/*printf(”確定?N“)*/
printf(”%C“,P->data)
P=P->lchild
}
如果(I!=-1)
{
P=stack[i
]i-->
else
return
}
]void main()//main function
]{
]struct bitnode*P,*t
later(P)
print(P)
}
數(shù)據(jù)結(jié)構(gòu)中二叉樹的定義是遞歸的,所以很自然,也很容易理解。
二叉樹的層次遍歷不是遞歸的,而是使用隊(duì)列。數(shù)據(jù)結(jié)構(gòu)中二叉樹的定義如下(不同于圖論中樹的定義):1。這是一個(gè)空集。2它由根節(jié)點(diǎn)及其左右子樹組成,左右子樹滿足二叉樹的定義。