中序遍歷訣竅 用C語(yǔ)言編程實(shí)現(xiàn)二叉樹(shù)的中序遍歷算法?
用C語(yǔ)言編程實(shí)現(xiàn)二叉樹(shù)的中序遍歷算法?#Include#Includestruct bitnode*stack[100]struct bitnode//define struct{char datas
用C語(yǔ)言編程實(shí)現(xiàn)二叉樹(shù)的中序遍歷算法?
#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
以后(P->lchild)
以后(P->rchild)
}
void print(struct bitnode*P)//前序遍歷(輸出二叉樹(shù))
{
int i=-1
while(1)
{
while P!=null)
]{
堆棧[i]=P->rchild/*printf(”確定?N“)*/
printf(”%C“,P->data)
P=P->lchild
}
如果(I!=-1)
{
P=stack[i
]i-->
else
]}
]void main()//main function
{
]struct bitnode*P,*t
later(P)
print(P)
}
某二叉樹(shù)的后序遍歷序列與中序遍歷序列相同?
post order遍歷顯示e是根節(jié)點(diǎn)。在中間順序中,E的左子樹(shù)在左邊,右子樹(shù)在右邊。我們可以看到左子樹(shù)只有一個(gè)D節(jié)點(diǎn),當(dāng)我們查看后序遍歷中的Acb序列時(shí),我們發(fā)現(xiàn)B是右子樹(shù)的根節(jié)點(diǎn)。當(dāng)我們發(fā)現(xiàn)B在中間順序時(shí),我們發(fā)現(xiàn)B沒(méi)有左子樹(shù),也就是說(shuō),AC在B的右子樹(shù)上。我們還知道,在后序遍歷中,AC表示a是C的子節(jié)點(diǎn),而AC表示a在C的左子樹(shù)上,前序是edbca