Ejemplo n.º 1
0
void BTree<T>::split(int p_id, int n_id) {
  BNode<T>& n = get_node(n_id);
  BNode<T>& t = manager.new_node();
  int pos = n.ascendpos();
  T& newkey = n.keys[pos];
  t.leaf = n.leaf;
  t.sibling = n.sibling;
  t.numkeys = n.numkeys - pos - 1;
  memcpy(t.keys, &(n.keys[pos+1]), sizeof(T)*(t.numkeys));
  if (!n.leaf) {
    n.numkeys = pos;  // key ascended
    memcpy(t.next, &(n.next[pos+1]), sizeof(int)*(t.numkeys+1));
  } else {
    n.numkeys = pos + 1;  // key copied
    n.sibling = t.id; 
    memcpy(t.next, &(n.next[pos+1]), sizeof(int)*(t.numkeys));
  }
  if (p_id == -1) {  // root splits
    BNode<T>& pp = manager.new_root();
    pp.leaf = 0;
    pp.sibling = -1;
    p_id = pp.id;
    return_node(pp.id);
  }
  BNode<T>& p = get_node(p_id);
  int newpos = p.findkey(newkey);
  p.addkey(newkey, newpos);
  p.addnext(n.id, t.id, newpos);
  p.numkeys++;
  update_node(n.id); return_node(n.id);
  update_node(t.id); return_node(t.id);
  update_node(p.id); return_node(p.id);
  return;
}
Ejemplo n.º 2
0
void remove_tree(tree_node_t *tree)
{
	tree_node_t *current_node, *tmp;
	if( tree->left == NULL )
		return_node( tree );
	else
	{  
		current_node = tree;
		while(current_node->right != NULL )
		{  
			if( current_node->left->right == NULL )
			{  
				return_node( current_node->left );
				tmp = current_node->right;
				return_node( current_node );
				current_node = tmp;
			}
			else
			{  
				tmp = current_node->left;
				current_node->left = tmp->right;
				tmp->right = current_node; 
				current_node = tmp;
			}
		}
		return_node( current_node );
	}
}
Ejemplo n.º 3
0
void BTree<T>::insert(int p_id, int n_id, T& key, void *data, int length) {
  BNode<T> &n = get_node(n_id);
  int pos = n.findkey(key), sz = n.numkeys;
  if (pos < sz && key == n.keys[pos]) { // duplicated
    return_node(n_id);
    return;
  }
  if (n.leaf && n.addkey(key, pos) >= 0) {
    n.adddata(manager.new_data(data,length), pos);
    n.numkeys++;
    update_node(n_id);
    return_node(n_id);
  } else {
    int m_id = n.next[pos];
    return_node(n_id);
    insert(n_id, m_id, key, data, length);
  }
  BNode<T> &nn = get_node(n_id);
  if (nn.numkeys >= CHUNK_SIZE) {
    return_node(n_id);
    split(p_id, n_id);
  } else {
    return_node(n_id);
  }
}
Ejemplo n.º 4
0
object_t *_delete(tree_node_t *tree, key_t delete_key)
{
	tree_node_t *tmp_node, *upper_node, *other_node;
	object_t *deleted_object;
	if( tree->left == NULL )
		return( NULL );
	else if( tree->right == NULL )
	{
		if(  tree->key == delete_key )
		{
			deleted_object = (object_t *) tree->left;
			tree->left = NULL;
			return( deleted_object );
		}
		else
			return( NULL );
	}
	else
	{
		tmp_node = tree;
		while( tmp_node->right != NULL )
		{
			upper_node = tmp_node;
			if( delete_key < tmp_node->key )
			{
				tmp_node   = upper_node->left; 
				other_node = upper_node->right;
			} 
			else
			{
				tmp_node   = upper_node->right; 
				other_node = upper_node->left;
			} 
		}
		if( tmp_node->key != delete_key )
			return( NULL );
		else
		{
			upper_node->key   = other_node->key;
			upper_node->left  = other_node->left;
			upper_node->right = other_node->right;
			deleted_object = (object_t *) tmp_node->left;
			return_node( tmp_node );
			return_node( other_node );
			return( deleted_object );
		}
	}
}
Ejemplo n.º 5
0
void remove_list(list* the_list){
	list* temp;
	do{
		temp = the_list->next;
		return_node(the_list);
		the_list = temp;
	} while (temp != NULL);
}
Ejemplo n.º 6
0
int BTree<T>::search(T& key, bool force=false) {
  int ret, cur_id = manager.get_root().id;
  while (true) {
    BNode<T>& cur = manager.get_node(cur_id);
    int i = cur.findkey(key);
    if (cur.leaf) {
      if (i >= cur.numkeys) {
        ret = force ? cur_id : -1;
      } else {
        ret = cur_id;
      }
      return_node(cur.id);
      return ret;
    }
    cur_id = cur.next[i];
    return_node(cur.id);
  }
}
Ejemplo n.º 7
0
region pop_region(list* the_list){
	list* temp;
	region temp_region;
	temp = the_list->next;
	the_list->next = temp->next;
	temp_region = temp->the_region;
	return_node(temp);
	return(temp_region);
}
Ejemplo n.º 8
0
void affiche_chemin_ricochet (Case *courant,int x_dep, int y_dep)
{
    cout << " ----- CHEMIN A L'ENVERS-------" << endl;
    Case test = return_node(courant->x,courant->y);
    int x;
    int y;
    while(1)
    {
        if(test.ancienne_direction == HAUT)
        {
            cout << "HAUT " << endl;
            chemin_ricochet.push_back(HAUT);
            y = -1;
            x= 0;
        }
        if(test.ancienne_direction == DROITE)
        {
            cout << "DROITE " << endl;
            chemin_ricochet.push_back(DROITE);
            x = - 1;
            y = 0;
        }
        if(test.ancienne_direction == BAS)
        {
            cout << "BAS " << endl;
            chemin_ricochet.push_back(BAS);
            x = 0;
            y = 1;
        }
        if(test.ancienne_direction == GAUCHE)
        {
            cout << "GAUCHE " << endl;
            chemin_ricochet.push_back(GAUCHE);
            x = 1;
            y = 0;
        }
        if(test.x == x_dep && test.y == y_dep)
        {
            break;
        }
        test = return_node(test.x + x,test.y + y);
    }
}
Ejemplo n.º 9
0
int BTree<T>::search_data(T& key) {
  int nodeid;
  if ((nodeid = search_node(key)) < 0)
    return -1;
  BNode<T> &node = get_node(nodeid);
  int pos = node.findkey(key);
  assert(pos >= 0);
  int dataid = node.next[pos];
  return_node(nodeid);
  return dataid;
}
Ejemplo n.º 10
0
void BTree<T>::preorder(BNode<T>& n) {
  T tkeys[CHUNK_SIZE+1];
  int tnext[CHUNK_SIZE+2];
  int sz=n.numkeys;
  if (n.leaf) {
    for (int i = 0; i < sz; ++i)
      cout << n.keys[i] << endl;
    return_node(n.id);
    return;
  }
  memcpy(tkeys, n.keys, sizeof(T) * (CHUNK_SIZE+1));
  memcpy(tnext, n.next, sizeof(int) * (CHUNK_SIZE+2));

  return_node(n.id);

  for (int i = 0; i < sz; ++i) {
    preorder(get_node(tnext[i]));
  //  cout << tkeys[i] << endl;
  }
  preorder(get_node(tnext[sz]));
}
Ejemplo n.º 11
0
void BManager<T>::optimize_data() {
  string data_path = prefix+".data";
  string tmp_path  = prefix+".data.tmp";
  FILE *tmp_file = fopen(tmp_path.c_str(), "w");
  if (!tmp_file || !data_file || !node_file) {
    cerr << "WARNING::BManager::no file to optimize" << endl;
    return;
  }
  char *buf = new char[1024];
  int buf_len = 1024;
  int cur_node;
  vector<pair<long long, int> > backup;

  backup.swap(data_zone);

  cur_node = first_leaf_id;
  while (cur_node != -1) {
    BNode<T> &n = get_node(cur_node);
    for (int i = 0; i < n.numkeys; ++i) {
      if (n.next[i] < 0)
        continue;
      int dataid = n.next[i];
      int newlen = backup[dataid].second;
      long long oldfp = backup[dataid].first;
      long long newfp = ftell(tmp_file);
      fseek(data_file, oldfp, SEEK_SET);
      if (buf_len < newlen) {
        delete []buf;
        buf = new char[newlen];
        buf_len = newlen;
      }
      int r = fread(buf, newlen, 1, data_file);
      assert(r > 0);
      fwrite(buf, 1, newlen, tmp_file);
      n.next[i] = data_zone.size();
      data_zone.push_back(make_pair(newfp, newlen));
    }
    cur_node = n.sibling;
    update_node(n.id);
    return_node(n.id);
  }
  optimized = 1;
  cerr << "[0]" << endl;
  fclose(tmp_file);
  cerr << "[1]" << endl;
  fclose(data_file);
  cerr << "[2]" << endl;
  remove(data_path.c_str());
  rename(tmp_path.c_str(), data_path.c_str());
  data_file = fopen(data_path.c_str(), "rb+");
  delete []buf;
}
Ejemplo n.º 12
0
void BTree<T>::inorder(BNode<T>& n) {
  if (n.id == manager.get_root().id)
    cout << "*";
  cout << "" << n.id << "[";
  for (int i = 0; i < n.numkeys-1; ++i)
    cout << n.keys[i] << " ";
  for (int i = n.numkeys-1; i >= 0  && i < n.numkeys; ++i)
    cout << n.keys[i];
  cout << "] ";
  int tmp[CHUNK_SIZE+2];
  int sz = n.numkeys;
  if (!n.leaf) {
    memcpy(tmp, n.next, sizeof(int) * (CHUNK_SIZE+2));
    return_node(n.id);
    cout << "( ";
    for (int i = 0; i < sz + 1; ++i)
      inorder(get_node(tmp[i]));
    cout << ") ";
  } else {
    return_node(n.id);
  }
}
int main()
{
	int i,j;
	mirror_1();
	printf_MATRIX();
	init_node();	
	calculate_node();
	return_node();
//	print_NODE();
	mirror_2();
	printf_MATRIX();
	return 0;
}
tree_node_t *make_tree(tree_node_t *list)
{
  typedef struct {
    tree_node_t  *node1;
    tree_node_t  *node2;
    list_node_t *intv_list;
    int number; } st_item;
   st_item current, left, right;
   st_item stack[100]; int st_p = 0;
   tree_node_t *tmp, *root;
   int length = 0;
   for( tmp = list; tmp != NULL; tmp = tmp->right )
      length += 1; /* find length of list */

   root = get_node();
   current.node1 = root; /* put root node on stack */
   current.node2 = NULL;
   current.number = length;/* root expands to length leaves */
   stack[ st_p++ ] = current;
   while( st_p >0 )/* there is still unexpanded node */
   {  current = stack[ --st_p ];
      if( current.number > 1 ) /* create (empty) tree nodes */
      { left.node1 = get_node();
        left.node2 = current.node2;
        left.number = current.number / 2;
        right.node1 = get_node();
        right.node2 = current.node1;
        right.number = current.number - left.number;
        (current.node1)->left  = left.node1;
        (current.node1)->right = right.node1;
        stack[ st_p++ ] = right;
        stack[ st_p++ ] = left;
      }
      else /* reached a leaf, must be filled with list item */
      { (current.node1)->left  = list->left;   /* fill leaf */
        (current.node1)->key   = list->key;    /* from list */
       // (current.node1)->intv_list = list->intv_list;
        (current.node1)->right = NULL;
        if( current.node2 != NULL ){
           /* insert comparison key in interior node */
           (current.node2)->key   = list->key;
	        // (current.node2)->intv_list = list->intv_list;
	}
        tmp = list;          /* unlink first item from list */
        list = list->right;  /* content has been copied to */
        return_node(tmp);    /* leaf, so node is returned */
      }
   }
   return( root );
}
Ejemplo n.º 15
0
void stmt(Token *tk)
{// statement
	make_empty();
	if 		(tk->id == If) 	  if_node(tk);
	else if (tk->id == While) while_node(tk);
	else if (tk->id == For)	  for_node(tk);
	else if (tk->id == Int)   int_node(tk);
	else if (tk->id == Ret)   return_node(tk);
	else if (tk->id == Prtf)  printf_node(tk);
	else if (tk->id == Stru)  struct_dec(tk);
	else if (tk->id == ';')   next(tk);
	else if (tk->id == '}')   return ;
	else { expr(tk); assembly(pop()); }
}
Ejemplo n.º 16
0
void BTree<T>::search_key_between(T& akey, T& bkey, pair<int, int>& node, pair<int, int>& pos) {
  int fnid = search(akey, true);
  int tnid = search(bkey, true);
  int fpos, tpos;
  int num, next;
  assert(fnid >= 0);
  BNode<T> &fnode = get_node(fnid);
  fpos = fnode.findkey(akey);
  num = fnode.numkeys;
  next = fnode.sibling;
  return_node(fnode.id);
  if (fpos >= num) {
    fnid = next;
    fpos = 0;
  }
  BNode<T> &tnode = get_node(tnid);
  tpos = tnode.findkey(bkey);
  return_node(tnode.id);
  node.first  = fnid;
  node.second = tnid;
  pos.first  = fpos;
  pos.second = tpos;
}
Ejemplo n.º 17
0
// remove from tail
region tail_pull(list* the_list){
	list* temp;
	region temp_region;
	temp = the_list->next;
	// while you're not the one before the end
	while (temp->next->next != NULL){
		temp = temp->next;
	}
	// get the last region
	temp_region = temp->next->the_region;
	// tell this penulatimate region it's now the end

	// ditch the end
	return_node(temp->next->next);
	temp->next = NULL;
	return(temp_region);
}
Ejemplo n.º 18
0
char *delete_line(text_t *txt, int index) {
	text_t *tmp_node, *upper_node, *other_node;
	object_t *deleted_object;
	int finished;
	text_t * path_stack[100];
	int path_st_p;
	int tmp_height, old_height;

	if(txt == NULL) {
		return NULL;
	}

	if(index > txt->key) {
		return NULL;
	}

	if(txt->right == NULL) {
		if(txt->key == index){
			deleted_object = (object_t *)txt->left;
			txt->left = NULL;
			txt->key = 0;
			return (deleted_object);
		}
	}else{
		path_st_p = 0;
		tmp_node = txt;

		while(tmp_node->right != NULL){
			path_stack[path_st_p++] = tmp_node;
			upper_node = tmp_node;
			if(tmp_node->left->key >= index) {
			    tmp_node = tmp_node->left;
			    other_node = upper_node->right;

			}else {
			    index -= tmp_node->left->key;
			    tmp_node = tmp_node->right;
			    other_node = upper_node->left;

		    }
		}

		{
			upper_node->key = other_node->key;
			upper_node->left = other_node->left;
			upper_node->right = other_node->right;
			upper_node->height = other_node->height;

			deleted_object = (object_t *) tmp_node->left;
			return_node(tmp_node);
			return_node(other_node);
		}

		//rebalance
		finished = 0;
		path_st_p -= 1;
		while(path_st_p > 0 && !finished){
			tmp_node = path_stack[--path_st_p];
			tmp_node->key = tmp_node->left->key + tmp_node->right->key;

			old_height = tmp_node->height;
			if(tmp_node->left->height - tmp_node->right->height == 2 ){
				if(tmp_node->left->left->height - tmp_node->right->height == 1 ){
					right_rotation(tmp_node);
					tmp_node->key = tmp_node->right->key;
					tmp_node->right->key = tmp_node->right->left->key + tmp_node->right->right->key;
					tmp_node->right->height = tmp_node->right->left->height + 1;
					tmp_node->height = tmp_node->right->height + 1;
				}else{
					left_rotation(tmp_node->left);
					right_rotation(tmp_node);
					tmp_node->key = tmp_node->right->key;
					tmp_node->right->key = tmp_node->right->left->key + tmp_node->right->right->key;

					tmp_height = tmp_node->left->left->height;
					tmp_node->left->height = tmp_height + 1;
					tmp_node->right->height = tmp_height + 1;
					tmp_node->height = tmp_height + 2;
				}
			}else if (tmp_node->left->height - tmp_node->right->height == -2 ){
				if( tmp_node->right->right->height - tmp_node->left->height == 1 ){
					left_rotation(tmp_node);
					//add line to update key.
					tmp_node->key = tmp_node->left->key;
					tmp_node->left->key = tmp_node->left->left->key + tmp_node->left->right->key;

					tmp_node->left->height = tmp_node->left->right->height + 1;
					tmp_node->height = tmp_node->left->height + 1;
				}else{
					right_rotation(tmp_node->right);
					left_rotation(tmp_node);
					tmp_node->key = tmp_node->left->key;
					tmp_node->left->key = tmp_node->left->left->key + tmp_node->left->right->key;
					tmp_height = tmp_node->right->right->height;
					tmp_node->left->height = tmp_height + 1;
					tmp_node->right->height = tmp_height + 1;
					tmp_node->height = tmp_height + 2;
				}
			}else{
				if(tmp_node->left->height > tmp_node->right->height)
					tmp_node->height = tmp_node->left->height + 1;
				else
					tmp_node->height = tmp_node->right->height + 1;
			}
			if(tmp_node->height == old_height)
				finished = 1;
		}
		while( path_st_p > 0){
			tmp_node = path_stack[--path_st_p];
			// add new line update key
			tmp_node->key = tmp_node->left->key + tmp_node->right->key;
		}
		return (deleted_object);
   }
   return NULL;
}
Ejemplo n.º 19
0
Archivo: main.c Proyecto: goshng/cocoa
void
return_uf_node (uf_node_t * node)
{
    return_node ((tree_node_t *) node);
}
Ejemplo n.º 20
0
object_t *_delete_balanced(tree_node_t *tree, key_t delete_key)
{
	tree_node_t *tmp_node, *upper_node, *other_node;
	//int finished;
	stack_t *stack;
	object_t *deleted_object;
	if( tree->left == NULL )
		return( NULL );
	else if( tree->right == NULL )
	{
		if(  tree->key == delete_key )
		{
			deleted_object = (object_t *) tree->left;
			tree->left = NULL;
			return( deleted_object );
		}
		else
			return( NULL );
	}
	else
	{
		stack = create_stack();
		tmp_node = tree;
		while( tmp_node->right != NULL )
		{
			push(tmp_node,stack);
			upper_node = tmp_node;
			if( delete_key <= tmp_node->left->key )
			{  
				tmp_node   = upper_node->left; 
				other_node = upper_node->right;
			} 
			else
			{
				delete_key = delete_key  - tmp_node->left->key;
				tmp_node   = upper_node->right; 
				other_node = upper_node->left;
			} 
		}
		if( tmp_node->key != delete_key )
			return( NULL );
		else
		{
			upper_node->key   = other_node->key;
			upper_node->left  = other_node->left;
			upper_node->right = other_node->right;
			upper_node->height = 0;
			deleted_object = (object_t *) tmp_node->left;
			return_node( tmp_node );
			return_node( other_node );
			update_leafcount(upper_node);
		}
		/* rebalance */
		//finished = 0;
		/*if (!stack_empty(stack))
		{
			print_stack(stack);
		}*/
		//Throw out the top of stack.
		pop(stack);
		while( !stack_empty(stack))// && !finished )
		{
			int tmp_height, old_height;
			tmp_node = pop(stack);
			tmp_node->key = tmp_node->left->key + tmp_node->right->key;
			old_height= tmp_node->height;
			if( tmp_node->left->height - tmp_node->right->height == 2 )
			{
				if( tmp_node->left->left->height - tmp_node->right->height == 1 )
				{
					right_rotation(tmp_node);
					tmp_node->right->height = tmp_node->right->left->height + 1;
					tmp_node->height = tmp_node->right->height + 1;
				}
				else
				{
					left_rotation(tmp_node->left);
					right_rotation(tmp_node);
					tmp_height = tmp_node->left->left->height;
					tmp_node->left->height = tmp_height + 1;
					tmp_node->right->height = tmp_height + 1;
					tmp_node->height = tmp_height + 2;
				}
			}
			else if( tmp_node->left->height - tmp_node->right->height == -2 )
			{
				if( tmp_node->right->right->height - tmp_node->left->height == 1 )
				{
					left_rotation( tmp_node );
					tmp_node->left->height = tmp_node->left->right->height + 1;
					tmp_node->height = tmp_node->left->height + 1;
				}
				else
				{
					right_rotation( tmp_node->right );
					left_rotation( tmp_node );
					tmp_height = tmp_node->right->right->height;
					tmp_node->left->height = tmp_height + 1;
					tmp_node->right->height = tmp_height + 1;
					tmp_node->height = tmp_height + 2;
				}
			}
			else /* update height even if there
				 was no rotation */
			{
				if( tmp_node->left->height > tmp_node->right->height )
					tmp_node->height = tmp_node->left->height + 1;
				else
					tmp_node->height = tmp_node->right->height + 1;
			}
			update_leafcount(tmp_node);
			/*if( tmp_node->height == old_height )
				finished = 1;*/
		}
		remove_stack(stack);
	}
	return( deleted_object );
}
char * delete_line(text_t *txt, int index) {

	char *deleted_object;
	text_t *path_stack[100];
	int path_st_p = 0;
	text_t *tmp_node, *upper_node, *other_node;
	tmp_node = txt;

	while (index != 1 || tmp_node->weight != 1) {
		path_stack[path_st_p++] = tmp_node;
		upper_node = tmp_node;
		if (index <= tmp_node->left->weight) {
			//printf("goes to the left <----\n");
			tmp_node = tmp_node->left;
			tmp_node = upper_node->left;
			other_node = upper_node->right;


		} else {
			//printf("goes to the right---->\n");
			index = index - tmp_node->left->weight;
			tmp_node = tmp_node->right;
			tmp_node = upper_node->right;
			other_node = upper_node->left;

		}

	}


	upper_node->key = other_node->key;
	upper_node->left = other_node->left;
	upper_node->right = other_node->right;
	upper_node->weight = other_node->weight;
	deleted_object = (char *) tmp_node->left;
	return_node(tmp_node);
	return_node(other_node);

	/*start rebalance*/
	path_st_p -= 1;
	while (path_st_p > 0) {
		tmp_node = path_stack[--path_st_p];
		tmp_node->weight = tmp_node->left->weight + tmp_node->right->weight;
		if (tmp_node->right->weight < ALPHA * tmp_node->weight) {
			if (tmp_node->left->left->weight
					> (ALPHA + EPSILON) * tmp_node->weight) {
				right_rotation(tmp_node);
				tmp_node->right->weight = tmp_node->right->left->weight
						+ tmp_node->right->right->weight;
			} else {
				left_rotation(tmp_node->left);
				right_rotation(tmp_node);
				tmp_node->right->weight = tmp_node->right->left->weight
						+ tmp_node->right->right->weight;
				tmp_node->left->weight = tmp_node->left->left->weight
						+ tmp_node->left->right->weight;
			}
		} else if (tmp_node->left->weight < ALPHA * tmp_node->weight) {
			if (tmp_node->right->right->weight
					> (ALPHA + EPSILON) * tmp_node->weight) {
				left_rotation(tmp_node);
				tmp_node->left->weight = tmp_node->left->left->weight
						+ tmp_node->left->right->weight;
			} else {
				right_rotation(tmp_node->right);
				left_rotation(tmp_node);
				tmp_node->right->weight = tmp_node->right->left->weight
						+ tmp_node->right->right->weight;
				tmp_node->left->weight = tmp_node->left->left->weight
						+ tmp_node->left->right->weight;
			}
		}
	}
	/*end rebalance*/
	return (deleted_object);





}
Ejemplo n.º 22
0
Archivo: main.c Proyecto: goshng/cocoa
object_t *
find (tree_node_t * tree, key_t query_key)
{
  int finished = 0;
  if (tree->object == NULL)
    return (NULL);              /* tree empty */
  else
    {
      tree_node_t *current_node, *stack_top, *tmp_stack;
      stack_top = NULL;
      current_node = tree;
      while (!finished)
        {
          tmp_stack = get_node ();
          tmp_stack->right = stack_top;
          tmp_stack->left = current_node;
          stack_top = tmp_stack;
          if (query_key < current_node->key && current_node->left != NULL)
            current_node = current_node->left;
          else if (query_key > current_node->key
                   && current_node->right != NULL)
            current_node = current_node->right;
          else
            finished = 1;
        }
      if (current_node->key != query_key)
        return (NULL);
      else
        {
          tree_node_t *upper, *upper2;
          tmp_stack = stack_top;
          stack_top = stack_top->right;
          return_node (tmp_stack);
          while (current_node != tree)
            {
              upper = stack_top->left;
              tmp_stack = stack_top;
              stack_top = stack_top->right;
              return_node (tmp_stack);
              if (upper == tree)
                {
                  if (upper->left == current_node)
                    right_rotation (upper);
                  else
                    left_rotation (upper);
                  current_node = upper;
                }
              else
                {
                  upper2 = stack_top->left;
                  tmp_stack = stack_top;
                  stack_top = stack_top->right;
                  return_node (tmp_stack);
                  if (upper == upper2->left)
                    {
                      if (current_node == upper->left)
                        right_rotation (upper2);
                      else
                        left_rotation (upper);
                      right_rotation (upper2);
                    }
                  else
                    {
                      if (current_node == upper->right)
                        left_rotation (upper2);
                      else
                        right_rotation (upper);
                      left_rotation (upper2);
                    }
                  current_node = upper2;
                }
            }
          return (current_node->object);
        }
    }
}
Ejemplo n.º 23
0
void BTree<T>::insert(T& key, void *data=NULL, int length=0) {
  int rootid = manager.get_root().id;
  return_node(rootid);
  insert(-1, rootid, key, data, length);
}
Ejemplo n.º 24
0
/* This function deletes the line of number index, renumbering all lines after that line, and returns a pointer to the deleted line */
char * delete_line(text_t *txt, int index) 
{
	text_t *tmp_node, *upper_node, *other_node;
   	object_t *deleted_object;
	int count = index;
	int finished;
   	tmp_node = txt;
	if (txt->left == NULL || length_text(txt) < index)
	{
		return NULL;
	}
	else if (txt->right == NULL)
	{
		deleted_object = (object_t *)txt->left;
		txt->left = NULL;
		txt->parent = NULL;
		txt->key = -1;
		txt->height = -1;
		return (deleted_object);
	}
	else
	{
		while(tmp_node->right != NULL)
      		{   
			upper_node = tmp_node;
			(tmp_node->key)--;
                        if (count <= tmp_node->left->key)
                        {
                                tmp_node = tmp_node->left;
				tmp_node = upper_node->left;
                        	other_node = upper_node->right;
                        }
                        else
                        {
                                count -= tmp_node->left->key;
                                tmp_node = tmp_node->right;
				tmp_node = upper_node->right;
				other_node = upper_node->left;
                        }
      		}
		upper_node->key   = other_node->key;
         	upper_node->left  = other_node->left;
         	upper_node->right = other_node->right;
                upper_node->height = other_node->height;
		if (upper_node->right != NULL)
		{
			upper_node->left->parent = upper_node;
			upper_node->right->parent = upper_node;
         	}
		deleted_object = (object_t *) tmp_node->left;
         	return_node(tmp_node);
         	return_node(other_node);
		upper_node = upper_node->parent;
		tmp_node = upper_node;
 
		finished = 0;
		while(!finished && tmp_node!=NULL)
		{
			int tmp_height, old_height;
			old_height = tmp_node->height;
			if (tmp_node->left->height - tmp_node->right->height == 2)
			{ 
				if (tmp_node->left->left->height - tmp_node->right->height == 1)
				{ 
					right_rotation(tmp_node);
					tmp_node->right->height = tmp_node->right->left->height + 1;
					tmp_node->height = tmp_node->right->height + 1;
					tmp_node->right->key = tmp_node->right->left->key + tmp_node->right->right->key;
					tmp_node->key = tmp_node->left->key + tmp_node->right->key;
				}
				else
				{ 
					left_rotation(tmp_node->left);
					right_rotation(tmp_node);
					tmp_height = tmp_node->left->left->height;
					tmp_node->left->height = tmp_height + 1;
					tmp_node->right->height = tmp_height + 1;
					tmp_node->height = tmp_height + 2;
					tmp_node->left->key = tmp_node->left->left->key + tmp_node->left->right->key;
					tmp_node->right->key = tmp_node->right->left->key + tmp_node->right->right->key;
                                        tmp_node->key = tmp_node->left->key + tmp_node->right->key;
				}
			}
			else if (tmp_node->left->height - tmp_node->right->height == -2)
			{ 
				if( tmp_node->right->right->height - tmp_node->left->height == 1)
				{ 
					left_rotation(tmp_node);
					tmp_node->left->height = tmp_node->left->right->height + 1;
					tmp_node->height = tmp_node->left->height + 1;
					tmp_node->left->key = tmp_node->left->left->key + tmp_node->left->right->key;
                                        tmp_node->key = tmp_node->left->key + tmp_node->right->key;
				}
				else
				{ 
					right_rotation(tmp_node->right);
					left_rotation(tmp_node);
					tmp_height = tmp_node->right->right->height;
					tmp_node->left->height = tmp_height + 1;
					tmp_node->right->height = tmp_height + 1;
					tmp_node->height = tmp_height + 2;
					tmp_node->right->key = tmp_node->right->left->key + tmp_node->right->right->key;
					tmp_node->left->key = tmp_node->left->left->key + tmp_node->left->right->key;
                                        tmp_node->key = tmp_node->left->key + tmp_node->right->key;
				}
			}
      			else 
			{ 
				if(tmp_node->left->height > tmp_node->right->height)
				{
					tmp_node->height = tmp_node->left->height + 1;
				}
				else
				{
					tmp_node->height = tmp_node->right->height + 1;
				}
			}
			if (tmp_node->height == old_height)
			{
				finished = 1;
			}
			tmp_node = tmp_node->parent;
		}

	       	return (deleted_object);
	}
}