Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,573 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,918 people online right now. Registration is fast and FREE... Join Now!




Build a Tree from a fully parenthesised expression

 
Reply to this topicStart new topic

Build a Tree from a fully parenthesised expression, I want to be able to read an expression from a file a represent using

enissay9891
7 May, 2008 - 03:12 PM
Post #1

New D.I.C Head
*

Joined: 10 Oct, 2007
Posts: 2


My Contributions
Here is what I have so far for the structures that I will use:

CODE

typedef struct
{
    char key;
    // Some other data
} TreeEntry;

typedef struct node_q
{
    struct node_s
    {
        struct node_q *element;
        struct node_s *next;
    };

    TreeEntry entry;
    struct node_s *childs;
    struct node_q *parent;
} Vertex;

struct node_s;
typedef struct node_s node_t;

typedef struct
{
    Vertex *root;
} Tree;



And this is the code for the function that will read an expression from a file and represent as a tree, for example:
(A(B(E(K,L),F),C(G),D(H(M),I,J))) where the siblings are separated with commas, and the children are put in nested parenthesis.

Here what I came up with for the function that will do this job:

CODE

void Load(char *dir, Tree *t)
{
    FILE *inp;
    char str[MAXSIZE];
    int i;
    Vertex *w;
    node_t *c;

    inp = fopen(dir, "r");
    fgets(str, MAXSIZE, inp);

    for (i = 0; i < strlen(str); ++i)
    {
        if (t->root == NULL)
        {
            if (str[i] == '(')
            {
                t->root = (Vertex *)malloc(sizeof(Vertex));
                t->root->childs = NULL;
                t->root->parent = NULL;
                w = t->root;
            }
        }

        else
        {
            if (str[i] == '(')
            {
                c = w->childs;
                c = (struct node_t *)malloc(sizeof(node_t));
                c->next = NULL;
                c->element = (Vertex *)malloc(sizeof(Vertex));
                c->element->childs = NULL;
                c->element->parent = w;
                w = c->element;
            }

            else if (isalpha(str[i]))
            {
                w->entry.key = str[i];
            }

            else if (str[i] == ',')
            {
                w = w->parent;
                c = w->childs;
                while (c->next)
                    c = c->next;
                c->next = (struct node_t *)malloc(sizeof(node_t));
                c = c->next;
                c->next = NULL;
                c->element = (Vertex *)malloc(sizeof(Vertex));
                c->element->childs = NULL;
                c->element->parent = NULL;
                w = c->element;
            }

            else if (str[i] == ')')
            {
                w = w->parent;
            }
        }
    }
}


Thanks in advance for your help. icon_up.gif
User is offlineProfile CardPM
+Quote Post

enissay9891
RE: Build A Tree From A Fully Parenthesised Expression
7 May, 2008 - 03:35 PM
Post #2

New D.I.C Head
*

Joined: 10 Oct, 2007
Posts: 2


My Contributions
Sorry I forget to mention my problem.
I have used printf() to track what's happening in my program. I seems like everything is working fine except the while loop within the else if (str[i] == ','). The program crashes right after c = w->parent.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:00AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month