universe@192: /*
universe@192: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@192: *
universe@192: * Copyright 2015 Olaf Wintermann. All rights reserved.
universe@192: *
universe@192: * Redistribution and use in source and binary forms, with or without
universe@192: * modification, are permitted provided that the following conditions are met:
universe@192: *
universe@192: * 1. Redistributions of source code must retain the above copyright
universe@192: * notice, this list of conditions and the following disclaimer.
universe@192: *
universe@192: * 2. Redistributions in binary form must reproduce the above copyright
universe@192: * notice, this list of conditions and the following disclaimer in the
universe@192: * documentation and/or other materials provided with the distribution.
universe@192: *
universe@192: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@192: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@192: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@192: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@192: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@192: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@192: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@192: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@192: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@192: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@192: * POSSIBILITY OF SUCH DAMAGE.
universe@192: */
universe@192:
universe@192:
universe@192: /**
universe@192: * @file avl.h
universe@192: *
universe@192: * AVL tree implementation.
universe@192: *
universe@192: * This binary search tree implementation allows average O(1) insertion and
universe@192: * removal of elements.
universe@192: *
universe@192: * @author Mike Becker
universe@192: * @author Olaf Wintermann
universe@192: */
universe@192:
universe@192: #ifndef UCX_AVL_H
universe@192: #define UCX_AVL_H
universe@192:
universe@193: #include "ucx.h"
universe@193:
universe@193:
universe@192: #ifdef __cplusplus
universe@192: extern "C" {
universe@192: #endif
universe@192:
universe@193: /**
universe@193: * UCX AVL Node type.
universe@193: *
universe@193: * @see UcxAVLNode
universe@193: */
universe@193: typedef struct UcxAVLNode UcxAVLNode;
universe@193:
universe@193: /**
universe@193: * UCX AVL Node.
universe@193: */
universe@193: struct UcxAVLNode {
universe@193: /**
universe@193: * The key for this node.
universe@193: */
universe@193: void *key;
universe@193: /**
universe@193: * Data contained by this node.
universe@193: */
universe@193: void *value;
universe@193: /**
universe@193: * The height of this (sub)-tree.
universe@193: */
universe@193: size_t height;
universe@193: /**
universe@193: * Parent node.
universe@193: */
universe@193: UcxAVLNode *parent;
universe@193: /**
universe@193: * Root node of left subtree.
universe@193: */
universe@193: UcxAVLNode *left;
universe@193: /**
universe@193: * Root node of right subtree.
universe@193: */
universe@193: UcxAVLNode *right;
universe@193: };
universe@193:
universe@193: /**
universe@193: * UCX AVL Tree.
universe@193: */
universe@193: typedef struct {
universe@193: /**
universe@193: * Root node of the tree.
universe@193: */
universe@193: UcxAVLNode *root;
universe@193: /**
universe@193: * Compare function that shall be used to compare the UcxAVLNode keys.
universe@193: * @see UcxAVLNode.key
universe@193: */
universe@193: cmp_func cmpfunc;
universe@193: /**
universe@193: * Custom user data.
universe@193: * This data will also be provided to the cmpfunc.
universe@193: */
universe@193: void *userdata;
universe@193: } UcxAVLTree;
universe@193:
universe@193: /**
universe@193: * Gets the value from the tree, that is associated with the specified key.
universe@193: * @param tree the UcxAVLTree
universe@193: * @param key the key
universe@193: * @return the value (or NULL
, if the key is not present)
universe@193: */
universe@193: void *ucx_avl_get(UcxAVLTree *tree, void *key);
universe@193:
universe@193: /**
universe@193: * Puts a key/value pair into the tree.
universe@193: * @param tree the UcxAVLTree
universe@193: * @param key the key
universe@193: * @param value the new value
universe@193: * @return the replaced value (or NULL
, if the key is new to the
universe@193: * tree)
universe@193: */
universe@193: void* ucx_avl_put(UcxAVLTree *tree, void *key, void *value);
universe@193:
universe@193: /**
universe@193: * Removes an element from the AVL tree.
universe@193: * @param tree the UcxAVLTree
universe@193: * @param key the key
universe@193: * @return the removed value (or NULL
, if the key was not present)
universe@193: */
universe@193: void* ucx_avl_remove(UcxAVLTree *tree, void *key);
universe@192:
universe@192:
universe@192:
universe@192: #ifdef __cplusplus
universe@192: }
universe@192: #endif
universe@192:
universe@192: #endif /* UCX_AVL_H */
universe@192: