insert.c 3.59 KB
/**
 * \file
 *
 * \author	Georg Hopp
 *
 * \copyright
 * Copyright © 2012  Georg Hopp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "trbase.h"
#include "tr/tree.h"

void TR_treeRotateLeft(TR_Tree *, TR_Tree);
void TR_treeRotateRight(TR_Tree *, TR_Tree);

void *
TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp)
{
	TR_Tree node     = *this;
	TR_Tree new_node = NULL;

	/*
	 * insert the node or return the one in tree if comparison
	 * succeeds.
	 */
	if (NULL == node) {
		/*
		 * if the root is NULL we simple add the element and set
		 * node to it.
		 */
		*this = node = new_node = TR_new(TR_Tree, search);
	} else {
		/*
		 * first search for it and if its found return the data
		 * and we are done...
		 */
		int  comparison;

		while (NULL != node) {
			comparison = comp(node->data, search);

			if (0 < comparison) {
				if (NULL != TR_TREE_LEFT(node)) {
					node = TR_TREE_LEFT(node);
					continue;
				} else {
					break;
				}
			}

			if (0 > comparison) {
				if (NULL != TR_TREE_RIGHT(node)) {
					node = TR_TREE_RIGHT(node);
					continue;
				} else {
					break;
				}
			}

			if (0 == comparison) {
				return node->data;
			}
		}   

		/*
		 * as we have not found it now add a new element.
		 */
		if (0 < comparison) {
			node->left = TR_new(TR_Tree, search);
			TR_TREE_LEFT(node)->parent = node;
			node = new_node = TR_TREE_LEFT(node);
		} else {
			node->right = TR_new(TR_Tree, search);
			TR_TREE_RIGHT(node)->parent = node;
			node = new_node = TR_TREE_RIGHT(node);
		}
	}

	/*
	 * we expect node not to be NULL and pointing to our
	 * new node at this point...now rabalance the tree
	 */
	while (1) {
		// case 1
		if (NULL == TR_TREE_PARENT(node)) {
			node->color = rbBlack;
			break;
		}

		// case 2
		if (rbBlack == TR_TREE_PARENT(node)->color) {
			break;
		}

		// case 3
		if (NULL != TR_TREE_UNCLE(node) && rbRed == TR_TREE_UNCLE(node)->color) {
			TR_TREE_PARENT(node)->color      = rbBlack;
			TR_TREE_UNCLE(node)->color       = rbBlack;
			TR_TREE_GRANDPARENT(node)->color = rbRed;

			node = TR_TREE_GRANDPARENT(node);
			continue;
		}

		// case 4
		if (node == TR_TREE_PARENT(node)->right
				&& TR_TREE_PARENT(node) == TR_TREE_GRANDPARENT(node)->left) {

			//TREE_ROTATE_LEFT(this, TREE_PARENT(node));
			TR_treeRotateLeft(this, TR_TREE_PARENT(node));
			node = TR_TREE_LEFT(node);

		} else if (node == TR_TREE_PARENT(node)->left
				&& TR_TREE_PARENT(node) == TR_TREE_GRANDPARENT(node)->right) {

			//TREE_ROTATE_RIGHT(this, TREE_PARENT(node));
			TR_treeRotateRight(this, TR_TREE_PARENT(node));
			node = TR_TREE_RIGHT(node);

		}

		// case 5
		TR_TREE_PARENT(node)->color      = rbBlack;
		TR_TREE_GRANDPARENT(node)->color = rbRed;

		if (node == TR_TREE_PARENT(node)->left) {
			//TREE_ROTATE_RIGHT(this, TREE_GRANDPARENT(node));
			TR_treeRotateRight(this, TR_TREE_GRANDPARENT(node));
		} else {
			//TREE_ROTATE_LEFT(this, TREE_GRANDPARENT(node));
			TR_treeRotateLeft(this, TR_TREE_GRANDPARENT(node));
		}

		break;
	}

	return new_node->data;
}

// vim: set ts=4 sw=4: