TREE

Section: System Calls (2)
Updated: 23 June 1986
Index Return to Main Contents
 

NAME

tree_init, tree_mung, tree_srch, tree_add, tree_delete, tree_trav - balanced binary tree routines  

SYNOPSIS

void
tree_init(tree)
int **tree;

int *
tree_srch(tree, compare, data)
int **tree, (*compare)(), *data;

void
tree_add(tree, compare, data, del_uar)
int **tree, (*compare)(), *data, (*del_uar)();

int
tree_delete(tree, compare, data, del_uar)
int **tree, (*compare)(), *data, (*del_uar)();

int
tree_trav(tree, trav_uar)
int **tree, (*trav_uar)();

void
tree_mung(tree, del_uar)
int **tree, (*del_uar)();
 

DESCRIPTION

These functions create and manipulate a balanced binary (AVL) tree. Each node of the tree contains the expected left & right subtree pointers, a short-int balance indicator, and a pointer to the user-data. On a 32-bit system, this means an overhead of 4+4+2+4 bytes per node. There is no key data type enforced by this package; a caller-supplied compare routine is used to compare user-data blocks.

Tree_init creates an empty tree and binds it to tree (which for this and all other routines in this package should be declared as a pointer to integer and passed by reference), which can then be used by other routines in this package. Note that more than one tree variable can exist at once; thus multiple trees can be manipulated simultaneously.

Tree_srch searches a tree for a specific node and returns either NULL if no node was found, or the address of the user-data for that node if one was found. compare is the address of a function to compare two user-data blocks. This routine should work much the way strcmp2 does; in fact, strcmp could be used if the user-data was a null-terminated string. data is the address of a user-data block to be used via compare as the search criteria. The tree is searched for a node where compare returns 0.

Tree_add inserts or replaces a node in the specified tree. The tree specified by tree is searched as in tree_srch, and if a node is found to match data, then the del_uar function is called with the address of the user-data block for the node (this routine should deallocate any dynamic memory which is pointed to exclusively by the node); the user-data pointer for the node is then replaced by the value of data. If no node is found to match, a new node is added (which may or may not cause a transparent rebalance operation), with a user-data pointer equal to data.

Tree_delete deletes a node from tree. A rebalance may or may not occur, depending on where the node is removed from and what the rest of the tree looks like. Tree_delete returns TRUE if a node was deleted, FALSE otherwise.

Tree_trav traverses all of tree, calling trav_uar with the address of each user-data block. If trav_uar returns FALSE at any time, tree_trav will immediately return FALSE to its caller. Otherwise all nodes will be reached and tree_trav will return TRUE.

Tree_mung deletes every node in tree, calling del_uar with the user-data address from each node (see tree_add and tree_delete above). The tree is left in the same state that tree_init leaves it in - i.e., empty.  

AUTHOR

Paul Vixie, converted and augumented from Modula-2 examples in Algorithms & Data Structures, Niklaus Wirth, Prentice-Hall, ISBN 0-13-022005-1.


 

Index

NAME
SYNOPSIS
DESCRIPTION
AUTHOR

This document was created by man2html, using the manual pages.
Time: 06:26:42 GMT, January 05, 2023