C++ Tree Calculator

This is a discussion on C++ Tree Calculator within the C++ Programming forums, part of the General Programming Boards category; i set it.....but it's still not giving me a number after i put in a equation, altho i get no ...

  1. #31
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    i set it.....but it's still not giving me a number after i put in a equation, altho i get no error. was there anything else u saw wrong with mine?

  2. #32
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    ugh this program is driving me up a wall. u said u got it to actually calculate numbers and display the answer correct. but I set type now........and it's not giving errors but it still wont work AGGHGGH
    here is what i have thus far.

    Code:
    //Matt Smith
    //Cs 215 Section 2
    //Program 5
    #include "CalcTree.h"
    
    
    CalcTree::CalcTree()
    {
    root = NULL;
    }
    
    CalcTree::~CalcTree()
    {
    destHelper(root); 
    }
    
    
    CalcTree::CalcTree(CalcTree& otherTree) //copy constructor
    {
    root = copyHelper(otherTree.root);                      
    }
    
    CTNode* CalcTree::copyHelper(CTNode *thatroot)
    {
    if(thatroot==NULL) return NULL;
    
    
    CTNode *pwned;
    pwned = new CTNode;
    pwned->type = thatroot->type;
    pwned->operand = thatroot->operand; 
    pwned->left=NULL; pwned->right=NULL; 
    
    
    if(( thatroot->left != NULL ) || ( thatroot->right != NULL ))
        {     
        if( thatroot->left != NULL ) 
            pwned->left = copyHelper(thatroot->left); 
        if( thatroot->right != NULL ) 
            pwned->right = copyHelper(thatroot->right); 
    }
    return pwned;
    }
    
    
    void CalcTree::destHelper( CTNode *thisroot )
    {     
    	if(thisroot==NULL) return;
    
    
    	else if(( thisroot->left != NULL ) || ( thisroot->right != NULL ))
    	{     
        if( thisroot->left != NULL ) 
            {destHelper(thisroot->left); 
             thisroot->left=NULL;} 
        if( thisroot->right != NULL ) 
            {destHelper(thisroot->right); 
             thisroot->right=NULL;} 
        }
    
    
    delete thisroot; 
    }
    
    
    
    void CalcTree::setRoot(NodeType type_, int operand_)
    {
    destHelper (root);  
    CTNode *temp = NULL;
    temp= new CTNode;
    temp-> type=type_;
    temp-> operand=operand_;
    temp->left=NULL;
    temp->right=NULL;
    root=temp;
    delete temp;
    
    
    
    
    }
    
    
    
    
    void CalcTree::setLeft(CalcTree &otherTree)
    {
    // delete the old branch first
    destHelper(root->left);
    
    // copy the new tree as left branch
    root->left= copyHelper(otherTree.root);
    }
    
    
    void CalcTree::setRight(CalcTree &otherTree)
    {
    // delete the old branch first
    destHelper(root->right);
    
    // copy the new tree as left branch
    root->right= copyHelper(otherTree.root);
    }
    
    
    
    
    void CalcTree::printIN()
    {
    
    printINhelper(root);
       
    }
    
    
    void CalcTree::printINhelper(CTNode* thisroot)
    {
    if (thisroot == NULL)
            return;
    
        else if (thisroot->type == OPR)        
            cout << thisroot->operand;    
           
        
        else {
            cout << "( ";                    
            printINhelper (thisroot->left);        
            switch (thisroot->type){            
                case ADD: cout << " + ";
                    break;
                case SUB: cout << " - ";
                    break;
                case MUT: cout << " * ";
                    break;
                case DIV: cout << " / ";
                    break;
            }
            printINhelper (thisroot->right);    
            cout << " )";                    
        }
    }  
    
    
    
    void CalcTree::printPOST( )
    {
        cout << "(";
        printPOSThelper(root);
        cout << ")";
       
    }
    void CalcTree::printPOSThelper(CTNode *thisroot){
    
        
        if (thisroot->type == OPR)        
            cout << thisroot->operand;    
           
     
        else {
           
            printPOSThelper (thisroot->left);       
            printPOSThelper (thisroot->right);    
            switch (thisroot->type){            
                case ADD: cout << " + ";
                    break;
                case SUB: cout << " - ";
                    break;
                case MUT: cout << " * ";
                    break;
                case DIV: cout << " / ";
                    break;
            }       
        }
    }
    
    
    float CalcTree::evaluate( ){
    
        return evalHelper(root);
    }
    
    float CalcTree::evalHelper( CTNode *thisroot ){
       
        if (thisroot == NULL)
        return 0;
       
        
        if (thisroot->type == OPR)
            return (float (thisroot->operand));
    
        
        else {
            switch (thisroot->type){          
                case ADD: return (float (evalHelper(thisroot->left) + evalHelper(thisroot->right)));
                case SUB: return (float (evalHelper(thisroot->left)- evalHelper(thisroot->right)));
                case MUT: return (float (evalHelper(thisroot->left) * evalHelper(thisroot->right)));
                case DIV:return (float (evalHelper(thisroot->left)/ evalHelper(thisroot->right)));
                default: return 0;
            }
        }
    }
    i was hoping to get this done by tonight.

  3. #33
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't have delete temp; at the bottom of setRoot.

  4. #34
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    OMG IT WORKS NOW, but why?

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Search
    By C++Newbie in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2011, 02:17 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21