#include<stdio.h>
#include<stdlib.h>
typedef struct BT {
int data;
struct BT * leftchild;
struct BT * rightchild;
} tree;
void create(int bt[],tree *root,int i,int length);
void traval(int bt[],tree *root,int i,int length);
void destroy(int bt[],tree *root,int i,int length);
int main() {
int bt[6] = {1,2,3,4,5,6};
tree *root = (tree *)malloc(sizeof(tree));
int length = 0;
length = sizeof(bt)/sizeof(bt[0]);
create(bt,root,1,length);
traval(bt,root,1,length);
printf("\n");
destroy(bt,root,1,length);
return 0;
}
void create(int bt[],tree *root,int i,int length) {
root->data = bt[i-1];
if(2*i>length) {
return;
}
root->leftchild = (tree *)malloc(sizeof(tree));
create(bt,root->leftchild,2*i,length);
if(2*i+1>length) {
return;
}
root->rightchild = (tree *)malloc(sizeof(tree));
create(bt,root->rightchild,2*i+1,length);
}
void traval(int bt[],tree *root,int i,int length) {
printf("正在遍历结点 %d\n",root->data);
if(2*i>length) {
return;
}
traval(bt,root->leftchild,2*i,length);
if(2*i+1>length) {
return;
}
traval(bt,root->rightchild,2*i+1,length);
}
void destroy(int bt[],tree *root,int i,int length) {
printf("正在删除...结点 %d\n",i);
if(2*i>length) {
free(root);
return;
}
destroy(bt,root->leftchild,2*i,length);
if(2*i+1>length) {
free(root);
return;
}
destroy(bt,root->rightchild,2*i+1,length);
free(root);
}