/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file bst.h
 * @brief Binary search tree of integers.
 *
 * Implementation of a standard BST holding integer values. Note that
 * the tree is not balanced (but this is subject to future changes).
 */

#ifndef POLIQARP_BST_H
#define POLIQARP_BST_H

#include <poliqarp-config.h>

#include <sakura/common/memory-arena.h>

/** 
 * Node of a BST tree.
 */
struct bst_node {
   uint32_t val;              /**< Value held by this node. */
   struct bst_node *left;     /**< Left child of this node. */
   struct bst_node *right;    /**< Right child of this node. */
};

/**
 * BST tree.
 */
struct bst {
   struct bst_node *root;  /**< Root node of the tree. */
   struct marena *arena;   /**< Memory arena used to allocate new nodes. */
   bool private_arena;     /**< True if the arena is private to this tree. */
};

/** 
 * Creates a new, empty BST tree.
 * @param t tree to be created.
 * @param arena memory arena to allocate new tree nodes from. If this is NULL,
 * a private arena will be created for the tree's exclusive use and deleted
 * upon removal of the tree.
 */
void bst_create(struct bst *t, struct marena *arena);

/**
 * Removes all elements of the tree. 
 * @note Takes constant time thanks to the features of memory arenas.
 * @param t tree to be emptied.
 */
void bst_empty(struct bst *t);

/**
 * Destroys a tree and frees all resources associated with it.
 * @param t tree to be destroyed.
 */
void bst_destroy(struct bst *t);

/** 
 * Inserts an element to the tree.
 * @param t the tree.
 * @param val the value to be inserted.
 */
void bst_insert(struct bst *t, uint32_t val);

#endif /* POLIQARP_BST_H */
