c语言在二叉树中找一个结点,C语言 求一个二叉树中位于先序序列中第k个位置的结点的值...

C语言 求一个二叉树中位于先序序列中第k个位置的结点的值

更新时间:2017/2/8 22:53:00  浏览量:786  手机版

编制一个递归算法,求一个二叉树中位于先序序列中第k个位置的结点的值

#include

#include

#define NULL 0

typedef char TElemType;

typedef struct BiNode

{ struct BiNode *lchild;

TElemType data;

struct BiNode *rchild;

}BiNode;

struct BiNode * CreateBiTree(struct BiNode *T)

{ TElemType a;

struct BiNode *t;

scanf("%c",&a);

if(a==' ') T=NULL;

else

{ t=(struct BiNode *)malloc(sizeof(BiNode));if(t!=NULL)

{ t->data=a;

T=t;

T->lchild=CreateBiTree(T->lchild);

T->rchild=CreateBiTree(T->rchild);

}

}

return T;

}

void show(struct BiNode *T)

{ if(T!=NULL)

{ printf("%c",T->data);

show(T->lchild);

show(T->rchild);

}

}

int tree(struct BiNode *T,int k,int a)

{ if(T==NULL)

{ a=a-1;

return a;

}

else

{ if(a==k)

{ printf("%c",T->data);

}

else

{ a=tree(T->lchild,k,a+1);

a=tree(T->rchild,k,a+1);

}

return a;

}

}

void main()

{ int i,k;

struct BiNode t;

struct BiNode *T;

T=&t;

printf("input elements of BiTree: "); T=CreateBiTree(T);

printf("\nthe BiTree is: ");

show(T);

printf("\ninput k: "); scanf("%d",&k);

printf("\nthe result is :");

tree(T,k,1);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值