递归初始化二叉树

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define LEFT 1
#define RIGHT 2
#define ON 0

typedef struct node{
 int data;
 struct node * next[2];
}BiTNode, *BiTree;

int init_tree(BiTNode *tree, int deep)
{
 int num , delet;

 printf("enter the number of data\n");
 scanf("%d" , &num);

 printf("which node do you want to delet? (0)no (1)left (2)right\n");
 scanf("%d" , &delet);

 printf("the deep is %d\n", deep);


 tree->data = num;
 if(deep > 1)
 {
  if(delet != LEFT)
  {
   tree->next[0] = (BiTNode *)malloc(sizeof(BiTNode));
   init_tree(tree->next[0], deep-1);
  }
  if(delet != RIGHT)
  {
   tree->next[1] = (BiTNode *)malloc(sizeof(BiTNode));
   init_tree(tree->next[1], deep-1);
  }
 }
 else
  return 1;
}

int main(int argc, char *argv[])
{
 BiTNode first;

 init_tree(&first, 4);
}

你可能感兴趣的:(数据结构,C语言)