Solution Idea

50106. Construct a Binary Search Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "construct.h"
 
 
Node *insert_bs_tree(Node *root, char name[16], int height, int weight){
  Node *current;
  if (root == NULL) {
    current = (Node*)malloc( sizeof(Node) );
    assert(current != NULL);
 
    strcpy(current->name, name);
    current->height = height;
    current->weight = weight;
    current->left = NULL;
    current->right = NULL;
    return(current);
  }
 
    #ifdef HEIGHT
      if (height < root->height)
      root->left = insert_bs_tree(root->left, name, height, weight);
      else
      root->right =insert_bs_tree(root->right, name, height, weight);
    #endif
    #ifdef WEIGHT
      if (weight < root->weight)
      root->left = insert_bs_tree(root->left, name, height, weight);
      else
      root->right =insert_bs_tree(root->right, name, height, weight);
    #endif
 
  return(root);
}
 
Node *ConstructBSTree(int N, char name[][16], int height[], int weight[]){
    Node *root = NULL;
    for( int i = 0 ; i < N ; ++i )
        root = insert_bs_tree(root, name[i], height[i], weight[i]);
    return root;
}

Discussion