Esempio n. 1
0
void div_tree(bintree_t * bt, FILE * out) {
  list_t * cvecs = bt_get_data(bt);
  int32_t split = split_by(cvecs);
  list_t * cvecs_l = NULL;
  list_t * cvecs_r = NULL;
  list_t * tmp = cvecs;
  while (tmp != NULL) {
    void * addr = list_head(tmp);
    int32_t * cvec = (int32_t*)addr;
    if (cvec[split+1]) {
      cvecs_l = list_cons(cvecs_l, addr);
    } else {
      cvecs_r = list_cons(cvecs_r, addr);
    }
    tmp = list_tail(tmp);
  }

  if (cvecs_l == NULL || cvecs_r == NULL) {
    return;
  }

  bt_insl(bt, cvecs_l);
  bt_insr(bt, cvecs_r);

  void * a1 = bt;
  void * a2 = bt_get_left(bt);
  void * a3 = bt_get_right(bt);

  fprintf(out, "\"%p\"->\"%p\",\n", a1, a2);
  fprintf(out, "\"%p\"->\"%p\",\n", a1, a3);

  div_tree(bt_get_left(bt), out);
  div_tree(bt_get_right(bt), out);
}
/* Removes a node and replaces it with its left child 
 * The right child will become the right-most node of the left child */
void bst_node_remove(bt_cursor cur) {
	bt_cursor temp = cur;
	if(!bt_isInternal(cur)) {
		cur = cur->parent;
		cur = (cur->left==temp?cur->left=NULL:cur->right=NULL);
		free(temp);
	}
	if(bt_get_left(*cur) && bt_get_right(*cur)) {
		cur = cur->left;
		while(cur->right)
			cur = cur->right;
		temp->data = cur->data;
		bst_node_remove(cur);
		return;
	}
	if(bt_get_left(*cur)) {
		cur->left->parent = cur->parent;
		cur->parent->left = cur->left;
		free(cur);
	}
}