技术博客第十周

输入两个参数,其中一个为树的顺序存储结构,另一个参数为结点数据,输出此结点数据的父结点与子结点,并在主函数中验证。

 

 

#include "stdafx.h"
#include <stdio.h>
#define Max 100
typedef char DataType;
typedef struct Trees{
 DataType datas[Max];
 int n;
}Trees;
#define NoNode 0;
void GetchildParent (Trees *t,DataType x){
 int i;
 for(i=1;i<=t->n;i++){
  if(t->datas[i]==x)
   break;
 }
 if(i>t->n){
  printf("无此结点\n\n");
 }
 if(i==1){
  printf("此结点%c为父节点,",x);
 }
 else
 {
  printf("此结点%c的父节点为%c",x,t->datas[i/2]);
  if(2*i <= t->n)
  {
   if(t->datas[i*2]!=NoNode)
    printf("左孩子结点为%c,",t->datas[i*2]);
   else
    printf("无左结点");
  }
   else
    printf("无左结点");
   if(2*i+1<=t->n)
   {
    if(t->datas[i*2+1]!=NoNode)
     printf("右孩子结点为%c",t->datas[i*2+1]);
    else
     printf("无右结点");
    printf("无右结点");
   }
 }
}
int main(int argc, char* argv[])
{
 Trees t;
 t.n=13;
 t.datas[8]=NoNode;t.datas[9]=NoNode;
 t.datas[1]='A';t.datas[2]='B';
 t.datas[3]='D';t.datas[4]='E';
 t.datas[5]='F';t.datas[6]='G';
 t.datas[7]='H';t.datas[10]='I';
 t.datas[11]='J';
 char c[20];
 while(true){
  printf("请接入结点数据:");
  scanf("%s",c);
  if(c[0]=='0')break;
 }
 return 0;
}

转载于:https://www.cnblogs.com/mms316/p/5470735.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值