The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Tree with UNIX gunbol Shell Programming and Scripting 5 04-09-2008 08:51 AM
error: initializer expression list treated as compound expression arunchaudhary19 High Level Programming 12 11-16-2007 06:44 AM
Binary Tree Creation Using fork() learneros UNIX for Dummies Questions & Answers 1 11-02-2007 02:58 PM
tree command wip_vasikaran UNIX for Dummies Questions & Answers 1 04-10-2007 09:43 AM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 10:59 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-08-2009
MrUser's Avatar
MrUser MrUser is offline
Registered User
  
 

Join Date: Oct 2009
Location: Earth
Posts: 23
tree creation for an expression

i have written a program for creating an infix tree for the given expression.
the program is working fine if we give the expression completlely binded with operators and operands but when we ignore the brackets we need to consider the priority of the operators nad build the tree.

i am not getting the idea how to do that.

i have tried with a pen and paper but my efforts are in vain.

any suggestions on how can i change my program so that it works for both the inputs.

you can have a look at my code.



Code:
 
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "BST.h"
#define EMPTY 0
void push_exp(int item);
int pop_exp();
int is_stk_exp_empty();
void push_tree();
BST_t *pop_tree();
int is_stk_tree_empty();
BST_t * infixtree(char* infix);
struct stack_exp
{
        int data[MAX];
        int top;
};
struct stack_exp ex;
struct stack_tree
{
        BST_t *nodes[100];
  int top;
};
struct stack_tree tree;
int emptystacks()
{
        ex.top =-1;
        tree.top =-1;
}
void push_exp(int item)
{
                ++ex.top;
                ex.data[ex.top]=item;
}
int pop_exp()
{
        int ret =0;
        if(ex.top != -1)
        {
                ret= ex.data[ex.top];
                --ex.top;
        }
        return ret;
}
int is_stk_exp_empty()
{
 if(ex.top == -1)
                return 1;
        else
                return 0;
}
int isoperator(char e)
{
        if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%'|| e == '^')
                return 1;
        else
                return 0;
}
BST_t *create_node(int n1)
{
        BST_t *newnode;
        newnode = (BST_t *)malloc(sizeof(newnode));
        newnode->data=n1;
        newnode->left=NULL;
        newnode->right=NULL;
        return newnode;
}
int isoper(char ch)
{
  char op;
        switch(ch)
        {
        case '+':
        case '-':
        case '/':
        case '*':
        case '^':
                op =1;
                break;
        default:
                op=0;
                break;
        }
return op;
}
 
BST_t * infixtree(char* infix)
{
        char *i,*p;
        char n1;
        BST_t *temp,*r,*l,*root,*t;
        i = &infix[0];
        while(*i)
        {
 
                while(isblank(*i) && *i != '\0')
   {
                        i++;
                }
 
                if( isdigit(*i) || isalpha(*i))
                {
                        while( isdigit(*i) || isalpha(*i))
                        {
                                push_exp(*i);
                                i++;
                        }
                }
                if(isoper(*i))
                {
                        push_exp(*i);
                }
                if( *i == '(' )
                {
                        push_exp(*i);
                }
                if( *i == ')')
                {
                while( (n1 = pop_exp()) != '(')
                {
                  if(isoperator(n1))
                   {
                     temp = create_node(n1);
      t = pop_tree();
                     if(t)
                       temp->right = t;
                   }
                  else
                  {
                    r=create_node(n1);
                    push_tree(r);
                  }
                }
                l=pop_tree();
                if(l){
                        temp->left=l;
                        root=temp;
                        push_tree(root);
                }
}
i++;
}
return root;
}
void push_tree(BST_t *t)
{
        tree.top++;
        tree.nodes[tree.top]=(BST_t *)malloc(sizeof(BST_t));
        tree.nodes[tree.top]=t;
}
 
BST_t *pop_tree()
{
        if(tree.top > -1 )
         return tree.nodes[tree.top--];
        else
         return NULL;
}
int is_stk_tree_empty()
{
        if(tree.top == -1)
                return 1;
        else
                return 0;
}
void inorder(BST_t * root)
{
        if(root != NULL )
        {
            inorder(root->left);
            printf("%c", root->data);
     inorder(root->right);
        }
}
 
int main()
{
      char in1[70],in2[70],post[50],ch;
      BST_t * root1, *root2;root1=root2=NULL;
      int i=0;
      printf(" Exp: \"( a + ( ( ( ( b * c ) ^ d ) / ( e + f ) ) * g) )\"\n");
      strcpy(in1," ( a + ( ( ( ( b * c ) ^ d ) / ( e + f ) ) * g))"); '
      emptystacks();
      root1 = infixtree(in1);
      inorder(root1);
      printf("\n");
      printf("Exp : \"a + b * c ^ d / ( e + f ) * g\" \n");
      strcpy(in2,"a + b * c ^ d / ( e + f ) * g"); 
      emptystacks();
      root2 = infixtree(in2);
      inorder(root2);
return 0;
}
  #2 (permalink)  
Old 10-08-2009
binlib binlib is offline
Registered User
  
 

Join Date: Aug 2009
Location: New Jersey
Posts: 56
"The AWK Programming Language" book page 146 may offer some hint:
Code:
# calc3 - infix calculator

NF > 0 {
    f = 1
    e = expr()
    if (f <= NF) printf("error at %s\n", $f)
    else printf("\t%.8g\n", e)
}

function expr(  e) {        # term | term [+-] term
    e = term()
    while ($f == "+" || $f == "-")
        e = $(f++) == "+" ? e + term() : e - term()
    return e
}

function term(  e) {        # factor | factor [*/] factor
    e = factor()
    while ($f == "*" || $f == "/")
        e = $(f++) == "*" ? e * factor() : e / factor()
    return e
}

function factor(  e) {      # number | (expr)
    if ($f ~ /^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)$/) {
        return $(f++)
    } else if ($f == "(") {
        f++
        e = expr()
        if ($(f++) != ")")
            printf("error: missing ) at %s\n", $f)
        return e
    } else {
        printf("error: expected number or ( at %s\n", $f)
        return 0
    }
}
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0