/**********************************************************************/ /* Howard Eng 9/93 */ /* */ /* Functions that adds, retrieves, and walks a binary tree. */ /* */ /**********************************************************************/ #include #include #include #include #include #include "tree.h" void ada_thd_add_to_tree ( tree **node, char *data, int (*comp) ( tree *, void * ) ) { int i; if ( *node == (tree *) NULL ) { *node = (tree *) malloc( sizeof( tree ) ); assert( *node != (tree *) NULL ); memset( *node, 0, sizeof( tree ) ); (*node)->data = data; } else if( ( i = (*comp)( *node, (void *) data )) > 0 ) { if( (*node)->left != (tree *) NULL ) ada_thd_add_to_tree( &(*node)->left, data, comp ); else { (*node)->left = (tree *)malloc( sizeof( tree ) ); assert( (*node)->left != (tree *) NULL ); memset( (*node)->left, 0, sizeof( tree )); (*node)->left->data = data; } } else if ( i < 0 ) { if( (*node)->right != (tree *) NULL ) ada_thd_add_to_tree( &(*node)->right, data, comp ); else { (*node)->right = (tree *)malloc( sizeof( tree ) ); assert( (*node)->right != (tree *) NULL ); memset( (*node)->right, 0, sizeof( tree )); (*node)->right->data = data; } } else fprintf( stderr, "Duplicate Entry\n" ); } char *ada_thd_get_from_tree ( tree *node, char *data, int (*comp)( tree *, void *) ) { char *item; int i; item = (char *) NULL; if( node != (tree *) NULL ) { if( ( i = (*comp)( node, (void *) data )) > 0 ) item = ada_thd_get_from_tree( node->left, data, comp ); else if ( i < 0 ) item = ada_thd_get_from_tree( node->right, data, comp ); else item = node->data; } return ( item ); } void ada_thd_walk_tree ( tree *node, void (*func)( tree *, char *), char *data ) { tree *right; tree *left; if( node != (tree *) NULL ) { left = node->left; right = node->right; ada_thd_walk_tree( left, func, data ); (*func)( node, data ); ada_thd_walk_tree( right, func, data ); } }