Search
 
SCRIPT & CODE EXAMPLE
 

C

binary search tree of strings in c

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 9

//struct for node
struct node {
    char *value;            // all void* types replaced by char*
    struct node *p_left;
    struct node *p_right;
};

//use typedef to make calling the compare function easier
typedef int (*Compare)(const char *, const char *);

//inserts elements into the 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 for key
        strcpy ((*leaf)->value, key);                   // copy the key
        (*leaf)->p_left = NULL;
        (*leaf)->p_right = NULL;
        //printf(  "
new node for %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 already exists
            printf ("Key '%s' already in tree
", key);
    }
}

//compares value of the new node against the previous node
int CmpStr(const char *a, const char *b)
{
    return (strcmp (a, b));     // string comparison instead of pointer comparison
}

char *input( void )
{
    static char line[MAXLEN+1];       // where to place key    
    printf("Please enter a string : ");
    fgets( line, sizeof line, stdin );
    return ( strtok(line, "
" ));    // remove trailing newline
}

//recursive function to print out the tree inorder
void in_order(struct node *root)
{
    if( root != NULL ) {
        in_order(root->p_left);
        printf("   %s
", root->value);     // string type
        in_order(root->p_right);
    }
}

//searches elements in the tree
void search(char* key, struct node* leaf, Compare cmp)  // no need for **
{
    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("
'%s' found!
", key);     // string type
    }
    else printf("
Not in tree
");
    return;
}

void delete_tree(struct node** leaf)
{
    if( *leaf != NULL ) {
        delete_tree(&(*leaf)->p_left);
        delete_tree(&(*leaf)->p_right);
        free( (*leaf)->value );         // free the key
        free( (*leaf) );
    }
}

//displays menu for user
void menu()
{
    printf("
Press 'i' to insert an element
");
    printf("Press 's' to search for an element
");
    printf("Press 'p' to print the tree inorder
");
    printf("Press 'f' to destroy current tree
");
    printf("Press 'q' to quit
");
}

int main()
{
    struct node *p_root = NULL;
    char *value;
    char option = 'x';

    while( option != 'q' ) {
        //displays menu for program
        menu();

        //gets the char input to drive menu
        option = getch();           // instead of two getchar() calls

        if( option == 'i') {
            value = input();
            printf ("Inserting %s
", value);
            insert(value,  &p_root, (Compare)CmpStr);
        }
        else if( option == 's' ) {
            value = input();
            search(value, p_root, (Compare)CmpStr);     // no need for **
        }
        else if( option == 'p' ) {
            in_order(p_root);
        }
        else if( option == 'f' ) {
            delete_tree(&p_root);
            printf("Tree destroyed");
            p_root = NULL;
        }
        else if( option == 'q' ) {
            printf("Quitting");
        }
    }
return 0;
Comment

PREVIOUS NEXT
Code Example
C :: c remove last charachter from string 
C :: addition of matrix 
C :: print to console in c 
C :: rust cross compile 
C :: mongo script to find collection size in database 
C :: C - program to create 1D array 
C :: Leap year using function in c 
C :: how to return array of char in c 
C :: sockaddr_in c 
C :: C program to input the month number and output the month name using switch statement 
C :: powershell search big files 
C :: c memcpy 
C :: refresh a chromebook terminal 
C :: how to use pointer in c to print char 
C :: get boolean from localstorage 
C :: what is the use of malloc in c 
C :: logical operators 
C :: how to declare a struct in c 
C :: C #ifdef Directive 
C :: XAudio2 C 
C :: Syntax for creating a node 
C :: <fileset joomla 
C :: instller acrobat ous ubuntu 
C :: execute asm in c 
C :: Uri/beecrowd problem no - 1131 solution in C 
C :: convert char to int ascii in c function 
C :: C Common mistakes when working with pointers 
C :: get multiple c 
C :: how to devowel string in c program 
C :: how to turn off bash 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =