Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

binary trees in C programming?

I'm new to binary trees and am confused with how to do the following. If you could offer some advice or guidance (in the simplest way to understand), that would be appreciated greatly.

I have the following

20(10(5(*)(*))(17(*)(*)))(30(21(*)(*))(*))

I want to write a program that reads in a binary tree in the above form and displays it in a 'friendlier' style

20----30

| |

10-17 21

|

5

The binary tree is read in using argv[1](this is the string itself, NOT a filename), for example :

% treevis "20(*)(*)"

Could you explain what I have to do for this and how I should go about doing it. My knowledge of trees is very raw at the moment so any advice would be great. Thanks!

2 Answers

Relevance
  • 7 years ago
    Favorite Answer

    You can try something like the following. It won't print it out exactly the way you want. I'll leave that for you to deal with as you need it. (It's simply not clear how to implement what you showed, in all cases.) But this should get things across as a starting approach.

    #include <stdio.h>

    #include <stdlib.h>

    #include <ctype.h>

    typedef struct tree tree;

    typedef struct tree {

        unsigned int id;

        tree *left, *right;

    } tree;

    int match( const char **s, char c ) {

        if ( s == 0 || *s == 0 || **s != c ) return 0;

        ++*s;

        return 1;

    }

    int digit( const char **s ) {

        return match( s, '0' ) || match( s, '1' ) || match( s, '2' ) ||

            match( s, '3' ) || match( s, '4' ) || match( s, '5' ) ||

            match( s, '6' ) || match( s, '7' ) || match( s, '8' ) ||

            match( s, '9' );

    }

    tree * create( const char **s ) {

        char c;

        unsigned int value;

        tree *node, *left, *right;

        if ( s == 0 || *s == 0 ) return 0;

        if ( match( s, '*' ) ) return 0;

        if ( c= **s, !digit( s ) ) return 0;

        for ( value= c - '0'; c= **s, digit( s ); ) value= 10*value + c - '0';

        if ( !match( s, '(' ) ) return 0;

        left= create( s );

        if ( !match( s, ')' ) || !match( s, '(' ) ) return 0;

        right= create( s );

        if ( !match( s, ')' ) ) return 0;

        node= (tree *) malloc( sizeof( tree ) );

        node->id= value;

        node->left= left;

        node->right= right;

        return node;

    }

    void destroy( tree * t ) {

        if ( t == 0 ) return;

        destroy( t->left );

        destroy( t->right );

        free( t );

    }

    void print( tree * t, int tab ) {

        if ( t == 0 ) { printf( "%*s(%c)\n", tab, "", '*' ); return; }

        printf( "%*s%d\n", tab, "", t->id );

        print( t->left, tab+4 );

        print( t->right, tab+4 );

    }

    int main( int argc, char *argv[] ) {

        char *p= argv[1];

        tree *mytree= create( &p );

        if ( *p == '\0' ) {

            printf( "%s parsed correctly! Parse tree follows:\n", argv[1] );

            print( mytree, 0 );

        } else

            printf( "%s failed to parse correctly.\n", argv[1] );

        destroy( mytree );

        return 0;

    }

    EDIT: I added tree printing code for you. See link below:

  • 7 years ago

    Hey Jonathan,

    Very interesting approach, I tried to get my head around it, Can you explain what have you done a bit,please?

    thanks,

Still have questions? Get your answers by asking now.