Ejemplo n.º 1
0
NODE *insert (NODE** root, int data)
{
	NODE **p = root;
	if (*p == NULL)
	{
		*p = (NODE *)malloc(sizeof(NODE));
		if (*p == NULL)
		{
			printf("메모리 할당 실패 \n");
			exit(-1);
		}
		(*p)->data = data;
		(*p)->left = (*p)->right = NULL;
	} else if (data < (*p)->data)
	{
		(*p)->left = insert(&((*p)->left), data);
		(*p) = balance_tree(p);
	} else if (data > (*p)->data)
	{
		(*p)->right = insert(&((*p)->right),data);
		(*p) = balance_tree(p);
	}
	else {
		printf("중복데이터 입력\n");
		exit(-1);
	}

	return *p;
}
Ejemplo n.º 2
0
void InsertNode(node* n, int item, void* value)
{
    if(n== NULL)
    {
        n= malloc(sizeof(node));
        n->key = item;
    }
    else if(n->key > item)
    {
        if(n->left == NULL)
        {
			n->left= malloc(sizeof(node));
			n->left->key =item;
			n->left->value =value;
            n->left->parent= n;
            n->left->left=NULL;
            n->left->right=NULL;
			n->left->left_height = 0;
			n->left->right_height = 0;
			update_height(n->left);
			balance_tree(n->left);
        }
        else
        {
            InsertNode(n->left, item, value);
        }
    }
    else if(n->key < item)
    {
        if(n->right == NULL)
        {
			n->right= malloc(sizeof(node));
			n->right->key =item;
			n->right->value=value;
            n->right->parent= n;
            n->right->left=NULL;
            n->right->right=NULL;
			n->right->left_height = 0;
			n->right->right_height = 0;
			update_height(n->right);
			balance_tree(n->right);
        }
        else
        {
            InsertNode(n->right, item, value);
        }
    }
    else
    {
        printf("error item already in tree\n");
    }
}
Ejemplo n.º 3
0
static void balance_tree(AVLTREE *b, struct bitree_node *node)
{
	int bf, child_bf;

	if (node == NULL)
		return;

	bf = node_bf(node);

	if (bf > 1) {

		child_bf = node_bf(node->left);
		if (child_bf >= 0)
			roate_right(b, node);
		else
			roate_double_right(b, node);

	} else if (bf < -1) {

		child_bf = node_bf(node->right);
		if (child_bf <= 0)
			roate_left(b, node);
		else
			roate_double_left(b, node);
	}

	balance_tree(b, node->parent);

	return;
}
Ejemplo n.º 4
0
static void update_height_balance(AVLTREE *b, struct bitree_node *node)
{
	update_height(node);

	balance_tree(b, node);

	return;
}
Ejemplo n.º 5
0
/*!*************************************************************************
	\fn main
	\brief - main fn to perform heapsort
****************************************************************************/
int main(void)
{
	node *root = NULL;
	int      i = 0;
	int   scan = 0;
#if 0
	int arr[] = {5,10,7,4,15,25,13};

	for(i = 0; i < 7; i++)
	{
		add_node(&root, arr[i], root);
    #ifndef HEAPSORT
      balance_tree(&root);
    #endif  /* !HEAPSORT */
	}
	PRINT("============ BEGIN SORTING============\n");
#else
	while(1)
	{
		printf("Enter number:\n");
		scanf("%d", &scan);
		if(scan == (int)-100)
			break;
		else
      {
			add_node(&root, scan, root);

        #ifndef HEAPSORT
         balance_tree(&root);
        #endif  /* !HEAPSORT */
      }
   }
#endif

	print_tree(root);
	printf("\n");
  #ifdef HEAPSORT
   sort(&root);
  #endif /* HEAPSORT */
	free_tree(root);
	return 0;
}
Ejemplo n.º 6
0
// test balancing of a binary tree
void test5(int n, int min, int max)
{
	test("balance binary tree ", 5);
	tree *t = create_random_tree(n, min, max);
	printf("Binary Tree: \n");
	print_ascii_tree(t);
	printf("Balanced Tree: \n");
	balance_tree(&t, n);
	print_ascii_tree(t);
	free_tree(t);
}
Ejemplo n.º 7
0
void balance_tree(node* n)
{
	if (n!=NULL)
	{
		if(n->right_height - n->left_height >1)
		{
			if(n->right->right_height - n->right->left_height <0)
				rotate_right(n->right);
			rotate_left(n);
			balance_tree(n->parent);
		}
		else if(n->right_height - n->left_height <-1)
		{
			if(n->left->right_height - n->left->left_height >0)
				rotate_left(n->left);
			rotate_right(n);
			balance_tree(n->parent);
		}
		else
		{
			balance_tree(n->parent);
		}
	}
}
void insert(BinaryTree** tree, int val){
  if((*tree) == NULL){
    BinaryTree* temp = new BinaryTree;
    temp->left = NULL;
    temp->right = NULL;
    temp->data = val;;
    (*tree) = temp;
    return;
  }

  if((*tree)->data < val){
    insert(&(*tree)->right, val);
  }
  else
  {
    insert(&(*tree)->left, val);
  }
  balance_tree(tree);
}
Ejemplo n.º 9
0
/* balance tree after insertion of N */
static void balance_tree(ScmTreeCore *tc, Node *n)
{
    Node *p = n->parent;

    if (!p) { BALANCE_CASE("1"); n->color = BLACK; return; }  /* root */
    if (BLACKP(p)) { BALANCE_CASE("2"); return; }      /* nothing to do */

    /* Here we're sure we have grandparent. */
    Node *g = p->parent;
    SCM_ASSERT(g != NULL);
    Node *u = (g->left == p)? g->right : g->left;

    if (REDP(u)) {
        p->color = u->color = BLACK;
        g->color = RED;
        BALANCE_CASE("3");
        balance_tree(tc, g);
        return;
    }
    if (n == p->right && p == g->left) {
        rotate_left(tc, p);
        n = n->left;
        BALANCE_CASE("4a");
    } else if (n == p->left && p == g->right) {
        rotate_right(tc, p);
        n = n->right;
        BALANCE_CASE("4b");
    }
    p = n->parent;
    g = p->parent;
    p->color = BLACK;
    g->color = RED;
    if (n == p->left && p == g->left) {
        rotate_right(tc, g);
        BALANCE_CASE("5a");
    } else {
        rotate_left(tc, g);
        BALANCE_CASE("5b");
    }
}
Ejemplo n.º 10
0
/* accessor */
Node *core_ref(ScmTreeCore *tc, intptr_t key, enum TreeOp op,
               Node **lo, Node **hi)
{
    Node *e = ROOT(tc), *n = NULL;

    if (e == NULL) {
        /* Tree is empty */
        if (op == TREE_CREATE) {
            n = new_node(NULL, key);
            PAINT(n, BLACK);
            SET_ROOT(tc, n);
            tc->num_entries++;
        }
        if (op == TREE_NEAR) {
            *lo = *hi = NULL;
        }
        return n;
    }

    for (;;) {
        int r = 0;
        if (tc->cmp) r = tc->cmp(tc, e->key, key);

        if (tc->cmp? (r == 0) : (e->key == key)) {
            /* Exact match */
            if (op == TREE_DELETE) {
                n = delete_node(tc, e);
                tc->num_entries--;
                return n;
            }
            if (op == TREE_NEAR) {
                *lo = prev_node(e);
                *hi = next_node(e);
            }
            return e;
        }

        if (tc->cmp? (r < 0) : (e->key < key)) {
            /* Key is larger than E */
            if (e->right) {
                e = e->right;
            } else {
                if (op == TREE_CREATE) {
                    n = new_node(e, key);
                    e->right = n;
                    balance_tree(tc, n);
                    tc->num_entries++;
                    return n;
                }
                if (op == TREE_NEAR) {
                    *lo = e;
                    *hi = next_node(e);
                }
                return NULL;
            }
        } else {
            /* Key is smaller than E */
            if (e->left) {
                e = e->left;
            } else {
                if (op == TREE_CREATE) {
                    n = new_node(e, key);
                    e->left = n;
                    balance_tree(tc, n);
                    tc->num_entries++;
                    return n;
                }
                if (op == TREE_NEAR) {
                    *hi = e;
                    *lo = prev_node(e);
                }
                return NULL;
            }
        }
    }
}
Ejemplo n.º 11
0
void DeleteNode(node* n)
{
	if (n->left == NULL && n->right ==NULL)
	{
		if(n->parent != NULL)
		{
			if(n->key > n->parent->key)
			{
				n->parent->right = NULL;
				n->parent->right_height =0;
				update_height(n->parent);
				balance_tree(n->parent);
			}
			else
			{
				n->parent->left = NULL;
				n->parent->left_height =0;
				update_height(n->parent);
				balance_tree(n->parent);
			}
		}
	}
	else if (n->left != NULL ^ n->right != NULL)
	{
		if(n->parent != NULL)
		{
			if(n->left != NULL)
			{
				n->left->parent= n->parent;
				n->parent->left= n->left;
				update_height(n->left);
				balance_tree(n->left);
			}
			else
			{
				n->right->parent= n->parent;
				n->parent->right= n->right;
				update_height(n->right);
				balance_tree(n->right);
			}
		}
		else
		{
			if(n->left != NULL)
			{
				n->left->parent= NULL;
				update_height(n->left);
				balance_tree(n->left);
			}
			else
			{
				n->right->parent= NULL;
				update_height(n->right);
				balance_tree(n->right);
			}
		}
	}
	else
	{
		
	}
}
Ejemplo n.º 12
0
void	remove_chunk(BinaryTree	*tree,MemoryChunk	*chunk)
{
	MemoryChunk	*left,*right,*parent,*newNode;
	boolean	bLeafNode;

	if(tree->root == 0 || tree->totalChunks == 0 || chunk == 0)
	{
		return;
	}

	/*remove the chunk and give priority to the right side*/

	left	=	chunk->left;
	right	=	chunk->right;
	parent	=	chunk->parent;
	newNode	=	0;

	if(!chunk->left && !chunk->right)
	{
		bLeafNode = TRUE;
	}
	else
	{
		bLeafNode = FALSE;
	}

	/*relocate left part (if possible)*/

	if(right == 0)
	{
		newNode = left;
	}
	else
	{
		newNode	=	right;
	}

	if(left && right)
	{
		MemoryChunk	*ptr,*prev;

		ptr = right;
		prev = 0;

		while(ptr)
		{
			prev = ptr;
			ptr = ptr->left;
		}

		prev->left = left;
		left->parent = prev;
	}

	if(parent)
	{
		if(GET_SIZE(chunk->size) > GET_SIZE(parent->size))
		{
			parent->right = newNode;
		}
		else
		{
			parent->left = newNode;
		}

		if(newNode)
		{
			newNode->parent = parent;
		}
	}
	else
	{
		tree->root = newNode;
		if(newNode)
		{
			newNode->parent = 0;
		}
	}

	tree->totalChunks--;

	if(bLeafNode && tree->root)
	{
		if(GET_SIZE(chunk->size) > GET_SIZE(tree->root->size))
		{
			tree->right_leaf_count--;
		}
		else
		{
			tree->right_leaf_count--;
		}
		balance_tree(tree);
	}
}
Ejemplo n.º 13
0
void	insert_chunk(BinaryTree	*tree,MemoryChunk	*chunk)
{
	MemoryChunk	*node,*parent;
	uint32	insertion_depth;

	if(tree == 0 || chunk == 0)
	{
		return;
	}

	insertion_depth = 0;
	node = tree->root;
	parent = 0;


	while(node)
	{
		parent = node;
		if(GET_SIZE(chunk->size) > GET_SIZE(node->size))
		{
			node = node->right;
		}
		else
		{
			node = node->left;
		}

		insertion_depth++;
	}

	if(parent)
	{
		if(GET_SIZE(chunk->size) > GET_SIZE(parent->size))
		{
			parent->right = chunk;
		}
		else
		{
			parent->left = chunk;
		}

		chunk->left = 0;
		chunk->right = 0;
		chunk->parent = parent;

		tree->totalChunks ++;
	}
	else
	{	
		tree->root = chunk;
		chunk->left = 0;
		chunk->right = 0;
		chunk->parent = 0;
		tree->totalChunks = 1;
	}

	if(tree->totalChunks > 1)
	{
		if(GET_SIZE(chunk->size) > GET_SIZE(tree->root->size))
		{
			if(insertion_depth > tree->right_depth)
			{
				tree->right_depth = insertion_depth;
				tree->right_leaf_count = 1;
			}
			else if(insertion_depth == tree->right_depth)
			{
				tree->right_leaf_count++;
			}
		}
		else
		{
			if(insertion_depth > tree->left_depth)
			{
				tree->left_depth = insertion_depth;
				tree->left_leaf_count = 1;
			}
			else if(insertion_depth == tree->left_depth)
			{
				tree->left_leaf_count++;
			}
		}
	}

	balance_tree(tree);
}