B木非再帰

予約注文

ボイドプレオーダー(BTNODE * B)
{ 
	BTNODE *のP = B。
	SqStack * ST; 
	InitStack(ST); 
	(!B = NULL)の場合
	{ 
		プッシュ(ST、P); 
		しばらく(!StackEmpty(ST))
		{ 
			ポップ(ST、P); 
			printf( "%のC"、P->データ)。
			もし(!p型> rchild = NULL)
			{ 
				プッシュ(ST、P-> rchild); 
			} 
			IF(!P-> lchild = NULL)
			{ 
				プッシュ(ST、P-> lchild)。
			} 
		} 
		のprintf( "の\ n"); 
	} 
	DestroyStack(ST)。
}

  

ボイドPreOrder1(BTNODE * B)
{ 
	BTNODE * P = B。
	SqStack * ST; 
	InitStack(ST); 
	一方、(!p = NULL || StackEmpty(ST))
	{ 
		ながら(!p = NULL)
		{ 
			のprintf( "%C"、P->データ)。
			(ST、p)を押してください。
			P = P - > lchild。
		} 
		IF(!StackEmpty(ST))
		{ 
			ポップ(ST、P)。
			P = P - > rchild。
		} 
	} 
	のprintf( "の\ n"); 
	DestroyStack(ST); 
}

  順番に

ボイド順序どおり(BTNODE * B)
{ 
	BTNODE * P = B。
	SqStack * ST; 
	InitStack(ST); 

	(!p = NULL || StackEmpty(ST))、一方
	{ 
		ながら(!p = NULL)
		{ 
			プッシュ(ST、P)。
			P = P - > lchild。
		} 
		IF(!StackEmpty(ST))
		{ 
			ポップ(ST、P)。
			printf( "%のC"、P->データ)。
			P = P - > rchild。
		} 
	} 
	のprintf( "の\ n"); 
	DestroyStack(ST); 
}

  通信販売

ボイド後順(BTNODE * B)
{ 
	BTNODE * P = B。
	BTNODE * R。
	SqStack * ST; 
	InitStack(ST); 
	ブールフラグ。
	やる
	{ 
		しばらくた(p = NULL!)
		{ 
			プッシュ(ST、P); 
			P = P - > lchild。
		} 
		R = NULL; 
		フラグ= TRUE。
		(!StackEmpty(ST)&&フラグ)一方
		{ 
			GetTop(ST、P)。
			IF(R == NULL && P-> rchild == NULL)
			{ 
				のprintf( "%C"、P->データ)。
				ポップ(ST、P); 
				R =(BTNODE *)はmalloc(はsizeof(BTNODE))。
				R->データ= P->データ; 
			} 
			それ以外の場合((R!= NULL &&のp> rchild!= NULL)&& R->データ== P-> rchild->
				printf( "%のC"、P->データ)。
				ポップ(ST、P); 
				R =(BTNODE *)はmalloc(はsizeof(BTNODE))。
				R->データ= P->データ; 
			} 
			{ 
				P = P-> rchild。
				フラグ= falseは、
			} 
		} 
	}しばらく(!StackEmpty(ST)); 
	printf( "\ n")を。
	DestroyStack(ST); 
}

  レベルトラバース

typedefは構造体{ 
	BTNODEデータ[MAXSIZE]。
	int型のフロント、リア、
} SqQueue。

  

ボイドLEVELORDER(BTNODE * B)
{ 
	SqQueue * Q。
	InitQueue(Q)。
	BTNODE * P = B; 
	ENQUEUE(Q、P)。
	一方、(QueueEmpty(Q)!)
	{ 
		DEQUEUE(Q、P)。
		printf( "%のC"、P->データ)。
		もし(!p型> lchild = NULL)
			エンキュー(Q、P-> lchild); 
		もし(!p型> rchild = NULL)
			エンキュー(Q、P-> rchild); 
	} 
}

  各リーフノードの逆パスにルートから出力階層トラバーサル

typedefは構造体{ 
	BTNODE *のPT。
	int型の親。
}ノードタイプ。

  

構造体のtypedef { 
	ノードタイプデータ[MAXSIZE]。
	int型のフロント、リア、
} QuType

  

AllPath2を無効(BTNODE * B)
{ 
	int型K。
	BTNODE * P; 
	QuType * QU。
	InitQueue2(QU)。
	ノードタイプ* qelem =(ノードタイプ*)はmalloc(はsizeof(ノードタイプ))。
	qelem-> PT = B。
	qelem->親= -1; 
	enQueue2(QU、qelem)。
	しばらく(QueueEmpty2(QU)!)
	{ 
		deQueue2(QU、qelem)。
		P = qelem-> PT。
		IF(P-> lchild == NULL && P-> rchild == NULL)
		{ 
			k = qu->フロント。
			一方、(!qu->データ[K] .parent = -1)
			{ 
				のprintf( "%のC"、qu->データ[K] .pt->データ)。
				K = qu->データ[K] .parent。
			} 
			のprintf( "%C \ n"は、qu->データ[K] .pt->データ)。

		} 
		の場合(P-> lchild!
			qelem-> PT = P-> lchild。
			qelem->親= qu->フロント。
			enQueue2(QU、qelem)。
		} 
		IF(!P-> rchild = NULL)
		{ 
			qelem-> PT = P-> rchild。
			qelem->親= qu->フロント。
			enQueue2(QU、qelem)。
		} 
	} 
}

  

おすすめ

転載: www.cnblogs.com/KIROsola/p/11439996.html