Sunday 15 April 2012

Binary tree for strings c -



Binary tree for strings c -

i'm trying implement binary tree capable of holding strings in c. after having code work ints, tried altering handle char arrays. seem have totally broke code , don't know how. help appreciated.

#include <stdio.h> #include <stdlib.h> //struct node struct node { void *value; struct node *p_left; struct node *p_right; }; //use typedef create calling compare function easier typedef int (*compare)(const void *, const void *); //inserts elements tree void insert(void* key, struct node** leaf, compare cmp) { if( *leaf == null ) { *leaf = (struct node*) malloc( sizeof( struct node ) ); (*leaf)->value = key; (*leaf)->p_left = null; (*leaf)->p_right = null; printf( "\nnew node " ); } else if( cmp(key, (*leaf)->value) < 0) { printf( "\ngoing left " ); insert( key, &(*leaf)->p_left, cmp); } else if( cmp(key, (*leaf)->value) > 0) { printf( "\ngoing right " ); insert( key, &(*leaf)->p_right, cmp); } //else {free(key);} } //compares value of new node against previous node int cmpint(const int *a, const int *b) { if(*a < *b){return -1;} else if(*a > *b){return 1;} else homecoming 0; } char *input( void ) { char line[10]; printf("please come in string : "); if( fgets( line, sizeof line, stdin ) ) { char *ret; ret = line; homecoming ret; } homecoming null; } //recursive function print out tree inorder void in_order(struct node *root) { if( root != null ) { in_order(root->p_left); printf("%d ", *(char*)root->value); in_order(root->p_right); } } //searches elements in tree void search(void* key, struct node** leaf, compare cmp) { if( *leaf != null ) { if( cmp(key, (*leaf)->value) == 0) { printf("\n%d found!\n", *(char*)key); } //else if( cmp(key, (*leaf)->value ) < 0) else if( key < (*leaf)->value ) { //printf( "\nnot here \n" ); search( key, &(*leaf)->p_left, cmp); } else if( cmp(key, (*leaf)->value) > 0) { //printf( "\nor here \n" ); search( key, &(*leaf)->p_right, cmp); } } else printf("\nnot in tree\n"); return; } void delete_tree(struct node** leaf) { if( *leaf != null ) { delete_tree(&(*leaf)->p_left); delete_tree(&(*leaf)->p_right); free( (*leaf) ); } } //displays menu user void menu() { printf("\npress 'i' insert element\n"); printf("press 's' search element\n"); printf("press 'p' print tree inorder\n"); printf("press 'f' destroy current tree\n"); printf("press 'q' quit\n"); } int main(void) { struct node *p_root = null; void *value; char alternative = 'x'; while( alternative != 'q' ) { //displays menu programme menu(); //gets char input drive menu alternative = getchar(); getchar(); if( alternative == 'i') //if 'i' integer input { value = input(); insert(value, &p_root, (compare)cmpint); } else if( alternative == 's' ) //if 's' input value , phone call search function { value = input(); search(value, &p_root, (compare)cmpint); } else if( alternative == 'p' ) //if 'p' phone call print table function { in_order(p_root); } else if( alternative == 'f' ) //if 'f' destroy tree , initailse new 1 { delete_tree(&p_root); printf("tree destroyed"); p_root = null; } else if( alternative == 'q' ) { printf("quitting"); } } homecoming 0; }

op did half job converting string keys. have commented changed, 2 main points a) input string local input() function , not available elsewhere, b) changed void* pointers char*, there absloutely no reason work void pointers.

#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define maxlen 9 //struct node struct node { char *value; // void* types replaced char* struct node *p_left; struct node *p_right; }; //use typedef create calling compare function easier typedef int (*compare)(const char *, const char *); //inserts elements tree void insert(char* key, struct node** leaf, compare cmp) { int res; if( *leaf == null ) { *leaf = (struct node*) malloc( sizeof( struct node ) ); (*leaf)->value = malloc( strlen (key) +1 ); // memory key strcpy ((*leaf)->value, key); // re-create key (*leaf)->p_left = null; (*leaf)->p_right = null; //printf( "\nnew node %s" , key); } else { res = cmp (key, (*leaf)->value); if( res < 0) insert( key, &(*leaf)->p_left, cmp); else if( res > 0) insert( key, &(*leaf)->p_right, cmp); else // key exists printf ("key '%s' in tree\n", key); } } //compares value of new node against previous node int cmpstr(const char *a, const char *b) { homecoming (strcmp (a, b)); // string comparing instead of pointer comparing } char *input( void ) { static char line[maxlen+1]; // place key printf("please come in string : "); fgets( line, sizeof line, stdin ); homecoming ( strtok(line, "\n" )); // remove trailing newline } //recursive function print out tree inorder void in_order(struct node *root) { if( root != null ) { in_order(root->p_left); printf(" %s\n", root->value); // string type in_order(root->p_right); } } //searches elements in tree void search(char* key, struct node* leaf, compare cmp) // no need ** { int res; if( leaf != null ) { res = cmp(key, leaf->value); if( res < 0) search( key, leaf->p_left, cmp); else if( res > 0) search( key, leaf->p_right, cmp); else printf("\n'%s' found!\n", key); // string type } else printf("\nnot in tree\n"); return; } void delete_tree(struct node** leaf) { if( *leaf != null ) { delete_tree(&(*leaf)->p_left); delete_tree(&(*leaf)->p_right); free( (*leaf)->value ); // free key free( (*leaf) ); } } //displays menu user void menu() { printf("\npress 'i' insert element\n"); printf("press 's' search element\n"); printf("press 'p' print tree inorder\n"); printf("press 'f' destroy current tree\n"); printf("press 'q' quit\n"); } int main() { struct node *p_root = null; char *value; char alternative = 'x'; while( alternative != 'q' ) { //displays menu programme menu(); //gets char input drive menu alternative = getch(); // instead of 2 getchar() calls if( alternative == 'i') { value = input(); printf ("inserting %s\n", value); insert(value, &p_root, (compare)cmpstr); } else if( alternative == 's' ) { value = input(); search(value, p_root, (compare)cmpstr); // no need ** } else if( alternative == 'p' ) { in_order(p_root); } else if( alternative == 'f' ) { delete_tree(&p_root); printf("tree destroyed"); p_root = null; } else if( alternative == 'q' ) { printf("quitting"); } } homecoming 0;

}

c tree binary

No comments:

Post a Comment