int main() { BST_t *pStrTree = newBST( (CmpFunc_t*)strcmp, NULL ); int n; while ( fgets( buffer, LEN_MAX, stdin ) != NULL ) // Read each line. { size_t len = strlen( buffer ); // Length incl. // newline character. if ( !BST_insert( pStrTree, buffer, len+1 )) // Insert the line in break; // the tree. } if ( !feof(stdin) ) { // If unable to read the entire text: fprintf( stderr, "sortlines: " "Error reading or storing text input.\n" ); exit( EXIT_FAILURE ); } n = BST_inorder( pStrTree, printStr ); // Print each line, in sorted order. fprintf( stderr, "\nsortlines: Printed %d lines.\n", n ); BST_clear( pStrTree ); // Discard all nodes. return 0; }
/* * Creates a new set that uses a binary search tree as its backing element representation. A set * will prevent the addition of duplicate items. * * Arguments: * comparisonFunction -- A function that will compare elements to determine equality and prevent * duplicates from being added. * * Returns: * An empty set */ Set *newSet( ComparisonFunction comparisonFunction ) { Set *set = malloc( sizeof(Set) ); set->elements = newBST(comparisonFunction); set->size = 0; return set; }
Map newMap() { Map map; map = newBlock(Map); enableIteration(map, newMapIterator); map->bst = newBST(string); return map; }