Commit 47c35c61 authored by Lenitra's avatar Lenitra
Browse files

tree empty

parent 7ade1f6b
Loading
Loading
Loading
Loading
−63
Changes for src/bst.c: 0 added lines, 63 removed lines.
Original line number Diff line number Diff line
@@ -15,82 +15,19 @@ typedef struct bst_t {
} bst_t;

bst_t bst_empty(void){
  bst_t bst;
  bst.h = NULL;
  bst.size = 0;
  bst.tree = NULL;
  return bst;
}

int bst_is_empty(bst_t t){
  return t.tree == NULL;
}

bst_t bst_add(bst_t t, void* x){
  if (t.h == NULL)
    return t;

  unsigned long int key = t.h(x);
  struct node_t* node = malloc(sizeof(*node));
  node->key = key;
  node->val = x;
  node->left = NULL;
  node->right = NULL;

  if (bst_is_empty(t)){
    t.tree = node;
    t.size = 1;
    return t;
  }

  struct node_t* current = t.tree;
  while (current != NULL){
    if (key < current->key){
      if (current->left == NULL){
        current->left = node;
        t.size++;
        return t;
      }
      current = current->left;
    }
    else if (key > current->key){
      if (current->right == NULL){
        current->right = node;
        t.size++;
        return t;
      }
      current = current->right;
    }
    else{
      // key == current->key

  }
}

void bst_destroy(bst_t t, void (*free_void)(void*)){
}

int bst_in(bst_t t, void* x){
  if (t.h == NULL)
    return 0;

  unsigned long int key = t.h(x);
  struct node_t* current = t.tree;
  while (current != NULL){
    if (key < current->key){
      current = current->left;
    }
    else if (key > current->key){
      current = current->right;
    }
    else{
      // key == current->key
      return 1;
    }
  }
  return 0;
}

unsigned long int bst_size(bst_t t){
  return t.size;
}