题干:
二叉链表存储结构如下:typedef struct BiNode{
ElemType data;
struct BiNode *lchild,*rchild;
}BiNode,*BiTree;
算法声明如下,请用递归思想完成该算法
BiTree create(ElemType A[],int i){
....
}
思想:
正常创建即可
函数:
BiTree create(ElemType A[],int i)//i表示当前元素下标
{
if(i>MAXSIZE)//递归出口,MAXSIZE是数组实际长度
return NULL;
BiNode *p=(BiNode *)malloc(sizeof(BiNode));
p->data=A[i-1];//头结点放进去
//树是从地址1开始创建的,但实际中要从数组的0地址开始,所以i要减1
p->lchild=create(A,2*i);//递归创建左子树
p->rchild=create(A,2*i+1);
return p;
}
测试:
#include<stdio.h>
#include <stdlib.h>
#include<math.h> //数学函数,求平方根、三角函数、对数函数、指数函数...
//定义常量 MAXSIZE
#define MAXSIZE 5