Пример #1
0
AvlIndex AvlNode::balance(AvlIndex tree)
{
    int balance, lh, rh;

    balance = get_balance(tree);

    if (balance < -1)
    {
        balance = get_balance(AVLNODE(tree)->left);

        if (balance > 0)
            tree = left_right(tree);
        else
            tree = left_left(tree);
    }
    else if (balance > 1)
    {
        balance = get_balance(AVLNODE(tree)->right);

        if (balance < 0)
            tree = right_left(tree);
        else
            tree = right_right(tree);
    }

    lh = node_height(AVLNODE(tree)->left);
    rh = node_height(AVLNODE(tree)->right);
    AVLNODE(tree)->height = (lh > rh ? lh : rh) + 1;
    return tree;
}
Пример #2
0
static inline
void node_fix_height(avl_tree_node_t *n) {
    unsigned char hl = node_height(n->left);
    unsigned char hr = node_height(n->right);

    n->height = (hl > hr ? hl : hr) + 1;
}
Пример #3
0
static size_t
node_height(const sp_node* node)
{
    size_t l = node->llink ? node_height(node->llink) + 1 : 0;
    size_t r = node->rlink ? node_height(node->rlink) + 1 : 0;
    return MAX(l, r);
}
void total_rotate(element_type x,SEARCH_TREE T,SEARCH_TREE root)
{
  tree_ptr father=find_father(T,root),rotated_tree;
    if((height(T->left)-height(T->right))==2){
      if(x<T->left->element)
	rotated_tree=s_rotated_left(T);
      else
	rotated_tree=d_rotated_left(T);
      if(parent->left==T)
	parent->left=rotated_tree;
      else
	parent->right=rotated_tree;
    }else{
	T->height=node_height(T);
    }
    if((height(T->right)-height(T->left))==2){
      if(x>T->right->element)
	rotated_tree=s_rotated_right(T);
      else
	rotated_tree=d_rotated_right(T);
      if(parent->right==T)
	parent->right=rotated_tree;
      else
	parent->left=rotated_tree;
    }else{
      T->height=node_height(T);
    }
}
Пример #5
0
static gpr_avl_node *assert_invariants(gpr_avl_node *n) {
  if (n == NULL) return NULL;
  assert_invariants(n->left);
  assert_invariants(n->right);
  assert(calculate_height(n) == n->height);
  assert(labs(node_height(n->left) - node_height(n->right)) <= 1);
  return n;
}
avl_ptr s_rotate_right(avl_ptr k1)
{
  avl_ptr k2=k1->right;
  k1->right=k2->left;
  k2->left=k1;
  k1->height=node_height(k1);
  k2->height=node_height(k2);
  return k2;
}
Пример #7
0
static size_t
node_height(const tr_node* node)
{
    ASSERT(node != NULL);

    size_t l = node->llink ? node_height(node->llink) + 1 : 0;
    size_t r = node->rlink ? node_height(node->rlink) + 1 : 0;
    return MAX(l, r);
}
Пример #8
0
static void update_height(struct bitree_node *node)
{
	if (node == NULL)
		return;

	node->height = 
		max(node_height(node->left), node_height(node->right)) + 1;
	update_height(node->parent);
	return;
}
Пример #9
0
AvlIndex AvlNode::right_right(AvlIndex p3)
{
    //printf("applying right right\n");
    AvlIndex p5 = AVLNODE(p3)->right;
    AVLNODE(p3)->right = AVLNODE(p5)->left;
    AVLNODE(p5)->left = p3;
    AVLNODE(p3)->height = max(node_height(AVLNODE(p3)->left), node_height(AVLNODE(p3)->right)) + 1;
    AVLNODE(p5)->height = max(AVLNODE(p3)->height, node_height(AVLNODE(p5)->right)) + 1;
    return p5;
}
Пример #10
0
// rebalancing operations from wikipedia diagram
// see http://en.wikipedia.org/wiki/AVL_tree
AvlIndex AvlNode::left_left(AvlIndex p5)
{
    //printf("applying left left\n");
    AvlIndex p3 = AVLNODE(p5)->left;
    AVLNODE(p5)->left = AVLNODE(p3)->right;
    AVLNODE(p3)->right = p5;
    AVLNODE(p5)->height = max(node_height(AVLNODE(p5)->left), node_height(AVLNODE(p5)->right)) + 1;
    AVLNODE(p3)->height = max(node_height(AVLNODE(p3)->left), AVLNODE(p5)->height) + 1;
    return p3;
}
Пример #11
0
gpr_avl_node *new_node(void *key, void *value, gpr_avl_node *left,
                       gpr_avl_node *right) {
  gpr_avl_node *node = gpr_malloc(sizeof(*node));
  gpr_ref_init(&node->refs, 1);
  node->key = key;
  node->value = value;
  node->left = assert_invariants(left);
  node->right = assert_invariants(right);
  node->height = 1 + GPR_MAX(node_height(left), node_height(right));
  return node;
}
Пример #12
0
/* same for left */
NODE* LeftRot(NODE* root, NODE* pivot)
{
	root->right = pivot->left;
	pivot->left = root;

	/* update heights */
	root->height = max(node_height(root->left), node_height(root->right))+1;
	pivot->height = max(node_height(pivot->left), node_height(pivot->right))+1;

	return pivot;
}
Пример #13
0
static unsigned
node_height(const hb_node *node)
{
	unsigned l, r;

	ASSERT(node != NULL);

	l = node->llink ? node_height(node->llink) + 1 : 0;
	r = node->rlink ? node_height(node->rlink) + 1 : 0;
	return MAX(l, r);
}
Пример #14
0
/*
 * Full height: longest path from node x to a leag
 */
static uint32_t node_height(rba_buffer_t *b, uint32_t x) {
  uint32_t i, j, hi, hj;

  if (x == 0) return 0;

  i = b->child[x][0];
  j = b->child[x][1];
  hi = node_height(b, i);
  hj = node_height(b, j);
  if (hi > hj) {
    return hi + 1;
  } else {
    return hj + 1;
  }
}
Пример #15
0
static ret_t
node_check (chula_avl_generic_node_t *node)
{
	int32_t                   left_height;
	int32_t                   right_height;
	int32_t                   balance;
	chula_avl_generic_node_t *tmp;

	if (node == NULL)
		return ret_ok;

	if (node->left_child) {
		tmp = node_prev (node);
		if (tmp->right != node) {
            chula_log_error ("AVL Tree inconsistency: Right child");
			return ret_error;
		}
	}

	if (node->right_child) {
		tmp = node_next (node);
		if (tmp->left != node) {
            chula_log_error ("AVL Tree inconsistency: Left child");
			return ret_error;
		}
	}

	left_height  = 0;
	right_height = 0;

	if (node->left_child)
		left_height  = node_height (node->left);
	if (node->right_child)
		right_height = node_height (node->right);

	balance = right_height - left_height;
	if (balance != node->balance) {
        chula_log_error ("AVL Tree inconsistency: Balance");
		return ret_error;
	}

	if (node->left_child)
		node_check (node->left);
	if (node->right_child)
		node_check (node->right);

	return ret_ok;
}
Пример #16
0
static ret_t
node_check (http2d_avl_generic_node_t *node)
{
	cint_t                       left_height;
	cint_t                       right_height;
	cint_t                       balance;
	http2d_avl_generic_node_t *tmp;

	if (node == NULL)
		return ret_ok;

	if (node->left_child) {
		tmp = node_prev (node);
		if (tmp->right != node) {
			LOG_ERROR_S (HTTP2D_ERROR_AVL_PREVIOUS);
			return ret_error;
		}
	}

	if (node->right_child) {
		tmp = node_next (node);
		if (tmp->left != node) {
			LOG_ERROR_S (HTTP2D_ERROR_AVL_NEXT);
			return ret_error;
		}
	}

	left_height  = 0;
	right_height = 0;

	if (node->left_child)
		left_height  = node_height (node->left);
	if (node->right_child)
		right_height = node_height (node->right);

	balance = right_height - left_height;
	if (balance != node->balance) {
		LOG_ERROR_S (HTTP2D_ERROR_AVL_BALANCE);
		return ret_error;
	}

	if (node->left_child)
		node_check (node->left);
	if (node->right_child)
		node_check (node->right);

	return ret_ok;
}
Пример #17
0
size_t
tr_tree_height(const tr_tree* tree)
{
    ASSERT(tree != NULL);

    return tree->root ? node_height(tree->root) : 0;
}
Пример #18
0
unsigned
hb_tree_height(const hb_tree *tree)
{
	ASSERT(tree != NULL);

	return tree->root ? node_height(tree->root) : 0;
}
SEARCH_TREE insert1(element_type x,SEARCH_TREE T,avl_ptr parent)
{
  avl_ptr rotated_tree;
  if(T==NULL){
    T=(SEARCH_TREE) malloc(sizeof(struct avl_node));
    if(T==NULL)
      fatal_error("Out of space\n");
    else{
      T->element=x;
      T->height=0;
      T->left=T->right=NULL;
    }
  }else{
    if(x<T->element){
      T->left=insert1(x,T->left,T);
      if((height(T->left)-height(T->right))==2){
	if(x<T->left->element)
	  rotated_tree=s_rotated_left(T);
	else
	  rotated_tree=d_rotated_left(T);
	if(parent->left==T)
	  parent->left=rotated_tree;
	else
	  parent->right=rotated_tree;
      }else{
	T->height=node_height(T);
      }
    }else if(x>T->element){
      T->right=insert1(x,T->right,T);
      if((height(T->right)-height(T->left))==2){
	if(x>T->right->element)
	  rotated_tree=s_rotated_right(T);
	else
	  rotated_tree=d_rotated_right(T);
	if(parent->right==T)
	  parent->right=rotated_tree;
	else
	  parent->left=rotated_tree;
      }else{
	T->height=node_height(T);
      }
    }
  }
  return T;
}
Пример #20
0
static cint_t
node_height (http2d_avl_generic_node_t *node)
{
	cint_t left_height;
	cint_t right_height;

	if (node) {
		left_height = 0;
		right_height = 0;

		if (node->left_child)
			left_height = node_height (node->left);

		if (node->right_child)
			right_height = node_height (node->right);

		return MAX (left_height, right_height) + 1;
	}
	return 0;
}
Пример #21
0
static int32_t
node_height (chula_avl_generic_node_t *node)
{
	int32_t left_height;
	int32_t right_height;

	if (node) {
		left_height = 0;
		right_height = 0;

		if (node->left_child)
			left_height = node_height (node->left);

		if (node->right_child)
			right_height = node_height (node->right);

		return MAX (left_height, right_height) + 1;
	}
	return 0;
}
Пример #22
0
NODE* node_insert(NODE* node, SYMBOL *symbol)
{
	NODE *root;
	int balance;
	int cmp, cmpchild;
	root = node;

	if (node == NULL)
		return node_new(symbol);
	
	cmp = strcmp(symbol->id, node->symbol->id);
	if (cmp < 0)
	{	 
		/* Left */
		node->left = node_insert(node->left, symbol);
		
		balance = node_height(node->left) - node_height(node->right);
		if (balance == 2)
		{
			cmpchild = strcmp(symbol->id, node->left->symbol->id);
			if (cmpchild < 0)
				root = LL(node);
			else if (cmpchild > 0)
				root = LR(node);
		}

	} else if (cmp > 0)
	{
		/* Right */
		node->right = node_insert(node->right, symbol);

		balance = node_height(node->right) - node_height(node->left);		
		if (balance == 2)
		{
			cmpchild = strcmp(symbol->id, node->right->symbol->id);
			if (cmpchild < 0)
				root = RL(node);
			else if (cmpchild > 0)
				root = RR(node);
		}
 
	}/* else 
	{
		the implementation doesn't allow duplicates, since a lookup is ALWAYS performed first
	} */

	node->height = max(node_height(node->left), node_height(node->right))+1;
	return root;
}
Пример #23
0
static gpr_avl_node *rebalance(const gpr_avl_vtable *vtable, void *key,
                               void *value, gpr_avl_node *left,
                               gpr_avl_node *right) {
  switch (node_height(left) - node_height(right)) {
    case 2:
      if (node_height(left->left) - node_height(left->right) == -1) {
        return assert_invariants(
            rotate_left_right(vtable, key, value, left, right));
      } else {
        return assert_invariants(rotate_right(vtable, key, value, left, right));
      }
    case -2:
      if (node_height(right->left) - node_height(right->right) == 1) {
        return assert_invariants(
            rotate_right_left(vtable, key, value, left, right));
      } else {
        return assert_invariants(rotate_left(vtable, key, value, left, right));
      }
    default:
      return assert_invariants(new_node(key, value, left, right));
  }
}
Пример #24
0
static int node_bf(struct bitree_node *node)
{
	return node_height(node->left) - node_height(node->right);
}
Пример #25
0
static inline
int node_balance_factor(const avl_tree_node_t *n) {
    return node_height(n->left) - node_height(n->right);
}
Пример #26
0
static uint32_t tree_height(rba_buffer_t *b) {
  return node_height(b, b->root);
}