中序遍历诀窍 用C语言编程实现二叉树的中序遍历算法?

用C语言编程实现二叉树的中序遍历算法?#Include#Includestruct bitnode*stack[100]struct bitnode//define struct{char datas

用C语言编程实现二叉树的中序遍历算法?

#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)//前序遍历(输出二叉树)

{

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

return

}

]void main()//main function

]{

]struct bitnode*P,*t

later(P)

print(P)

}

数据结构中二叉树的定义是递归的,所以很自然,也很容易理解。

二叉树的层次遍历不是递归的,而是使用队列。数据结构中二叉树的定义如下(不同于图论中树的定义):1。这是一个空集。2它由根节点及其左右子树组成,左右子树满足二叉树的定义。

二叉树的遍历算法实现为何要采用递归?

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)//递归调用逐层分析Int dep2=BTREE depth(BT->rchild)if(dep1>dep2)return dep2 1}}Int leave(bitnode*BT){//find二叉树中的叶节点数if(BT==null)返回0else{if(BT->lchild==null)&这是学习数据结构的练习。它使用递归形式。理解的时候需要考虑一下,但是函数相对简单。