C++ Tree Calculator

This is a discussion on C++ Tree Calculator within the C++ Programming forums, part of the General Programming Boards category; In destHelper: Code: if(root==NULL) return; should check thisroot instead of root. In general, when you have a segfault on a ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In destHelper:
    Code:
    if(root==NULL) return;
    should check thisroot instead of root.

    In general, when you have a segfault on a line with a pointer on it, check to see if you're somehow dereferencing a NULL pointer.

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    yeah i changed that, it's still not working tho. I dont know what else to do with this code

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by tabstop View Post
    The Department of Redundancy Department wants to speak with you. (This will work, but you're checking against NULL twice for no good reason that I can see.) And you should be setting type when you set everything else.
    Actually it's worse than that...
    Each pointer (except the head) is being compared to NULL 3 times if you count the recursive call.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    .......Sorry to be such a pain but can u guys show me what your talking about, im just trying to get this code debugged before it's due tuesday . i mean overall the code looks mostly done correct? right?

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your SetRoot needs some work (I don't know what your copy looks like). You need to (1) set up temp the way the root should look, and then (2) set root to equal temp. Nothing else sticks out on first glance; that's why we test.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Oh, and I said it earlier about CopyHelper, which you still haven't fixed:
    Quote Originally Posted by tabstop View Post
    And you should be setting type when you set everything else.

  7. #22
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    I changed the set root, and then it pointed to the desthelper....i dunno why?

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have three issues that have come up in this thread: (1) setroot using root after it's freed; (2) desthelper checking root==NULL instead of thisroot; and (3) copyHelper not setting the type of the new node.

    I will tell you that when I made these three changes in your posted code, I got a working program. Reread everything (I know that the threading is confusing to me, but everything's in there somewhere) and double check you've made those fixes.

  9. #24
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    when u say working, did it actually calculate the numbers?

    and when u say copyhelper not setting the type.
    cause i fixed the thisroot and setroot
    but im not sure what u mean by copyhelper

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MercFh View Post
    when u say working, did it actually calculate the numbers?

    and when u say copyhelper not setting the type.
    cause i fixed the thisroot and setroot
    but im not sure what u mean by copyhelper
    Yes.

    When you create a node (as in copyhelper) you should set all the fields. You are never setting the type field, so it is not being set (consequently, when you try to print it or calculate with it, it doesn't know what type it is).

  11. #26
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    Like I seriously am not sure what type even is, so I should set operand to null.....or?
    like i hate to ask can u just show me what u mean?

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    struct CTNode
    {
    	NodeType type;
    	int operand;
    	CTNode *left, *right;
    };
    I haven't looked at the code at all [more than find what I think is the correct data structure], but I guess what iMalc is talking about is this type.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    Registered User
    Join Date
    Dec 2007
    Posts
    20

    here is what

    here is what i have so far, i believe I fixed the set root. im not getting any more errors when i run it, but it still doesnt do the equation

    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->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;
            }
        }
    }
    im pleading, I seriously dont know what u meant by the type....like add? or operand. if anyone could just show me the line, or at least explain their way to it, since apparently the program works with just a line of code misisng,?

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    struct CTNode
    {
    	NodeType type;
    	int operand;
    	CTNode *left, *right;
    };
    Four variables.

    Code:
    CTNode* CalcTree::copyHelper(CTNode *thatroot)
    {
    if(thatroot==NULL) return NULL;
    
    
    CTNode *pwned;
    pwned = new CTNode;
    pwned->operand = thatroot->operand;
    pwned->left=NULL; pwned->right=NULL;
    Three get set.
    You need to copy type when you copy the node.

  15. #30
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    oh so it would be pwned->type= thatroot->type;?

Page 2 of 3 FirstFirst 123 LastLast
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