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

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




root is working, but seach function crashed the program

 
Reply to this topicStart new topic

root is working, but seach function crashed the program, thanks to Martyr2 for fixing the root problem

r10lover10
6 Dec, 2007 - 09:30 AM
Post #1

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 32


My Contributions
It is a binary search tree project I am working on for a while. The only problem that got me stuck is the root (1st item inserted) will not be display and crashed the search function.

any help would be greatly apprectiated, tomorrow friday is the deadline.

header file:

CODE
typedef int itemType;

class tree
{  
    private:
        struct TNode
        {
            itemType item;
            TNode *lchild;
            TNode *rchild;
        };

        int size;

        TNode *root;
        TNode *p;
        TNode *q;


    public:

        tree();                        // constructor

        bool isEmpty() const;
        //goal:test whether tree is empty
        //pre:none
        //post:return true if tree is empty else return false

        bool isFull() const;
        //goal:test whether tree is full
        //pre:none
        //post:return true if tree is full else return false

        void insert(TNode *&p, itemType newItem);
        //goal:insert item
        //pre:tree exists
        //post:item inserted
        
        itemType search(TNode *&p, itemType newItem);
        //goal:search item
        //pre:tree exists
        //post:item being search
        
        int getSize();
        //goal:get the size of tree if is not empty
        //pre:tree exists
        //post:return tree size
    
        void preorder(TNode *&p);
        //goal:display tree in preoder
        //pre:tree exists
        //post:if tree is not empty

        void inorder(TNode *&p);
        //goal:display tree in inorder
        //pre:tree exists
        //post:if tree is not empty

        void postorder(TNode *&p);
        //goal:display tree in postorder
        //pre:tree exists
        //post:if tree is not empty
};



implementation file:
CODE
#include "tree.h"
#include <iostream>
using namespace std;


tree::tree()                            //constructor
{
    size = 0;
    root = NULL;
    
}

bool tree::isEmpty() const
{
    return (size == 0);
}

bool tree::isFull() const
{
    return false;
}

void tree::insert(TNode *&p, itemType newItem)
{    
    if(isEmpty())
    {
        root = new TNode;
        root->item = newItem;
        root->lchild = NULL;
        root->rchild = NULL;
        size++;
    }
    else if(p == NULL)
    {
        p = new TNode;
        p->item = newItem;
        p->lchild = NULL;
        p->rchild = NULL;
        size++;      
    }

    else if (newItem < p->item)
    {
        insert(p->lchild, newItem);
    }
    else
    {
        insert(p->rchild, newItem);
    }
  
}

itemType tree::search(TNode *&p, itemType newItem)
{
    if(!isEmpty())
    {
        if(p->item == newItem)
        {
            return p->item;
        }
        else if(newItem < p->item)
        {
            search(p->lchild, newItem);
        }
        else
        {
            search(p->rchild, newItem);
        }
    }
    else
    {
        cout << "Tree is empty." << endl;
    }
}
int tree::getSize()
{
    return size;
}

void tree::preorder(TNode *&p)
{
    if(!isEmpty())
    {

    if(p!=NULL)
    {
        cout << p->item<< " ";
        preorder(p->lchild);
        preorder(p->rchild);
    }
    }
    else
    {
        cout << "\nTree is Empty\n" << endl;
    }
}

void tree::inorder(TNode *&p)
{
    if(!isEmpty())
    {

    if(p!=NULL)
    {
        inorder(p->lchild);  
        cout << p->item << " ";
        inorder(p->rchild);
    }
    }
    else
    {
        cout << "\nTree is Empty\n" << endl;
    }
}

void tree::postorder(TNode *&p)
{
    if(!isEmpty())
    {

    if(p!=NULL)
    {
        postorder(p->lchild);
        postorder(p->rchild);
        cout << p->item<< " ";
    }
    }
    else
    {
        cout << "\nTree is Empty\n" << endl;
    }
}



main program:
CODE
#include "tree.h"
#include <conio.h>
#include <iostream>
using namespace std;


int main()
{
    int option;
    itemType anItem;
    struct tree::TNode *root;
    root = NULL;

    tree runtree;

    // user menu

    do{      
        cout << "\n 1.Insert Item";
        cout << "\n 2.Search Item";
        cout << "\n 3.Check Size";
        cout << "\n 4.Display Tree In Preorder";
        cout << "\n 5.Display Tree In Inorder";
        cout << "\n 6.Display Tree In Postorder";
        cout << "\n 7.Exit Program" << endl;

        cin >> option;

        switch(option)
        {
        case 1:                                    // Insert Item
            cout << "\nEnter an integer: ";
            cin >> anItem;
            runtree.insert(root,anItem);
            break;
            //end case 1

        case 2:                                    // Search Item
            cout << "\nEnter an integer: ";
            cin >> anItem;
            if(runtree.search(root, anItem) == anItem)
            {
                cout << "A " << runtree.search(root, anItem) << " has been found in the list!";
            }
            else
            {
                cout << "Unable to find " << anItem << endl;
            }
            break;
            //end case 2

        case 3:                                    // Check Size
            cout << "\n\nThe size of your tree is " << runtree.getSize() << "." << endl;
            break;
            //end case 3
        
        case 4:                                    // Display Preorder
            runtree.preorder(root);
            break;
            //end case 4

        case 5:                                    // Display Inorder
            runtree.inorder(root);
            break;
            //end case 5

        case 6:                                    // Display Postorder
            runtree.postorder(root);
            break;
            //end case 6
         }
  }while(option != 7);                            // Exit Program

    getch();
    
    return 0;
}


This post has been edited by r10lover10: 6 Dec, 2007 - 12:08 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Root Is Working, But Seach Function Crashed The Program
6 Dec, 2007 - 10:08 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi r10lover10,

Cutting it a bit close with the deadline are we? Luckily DIC is here to help you out! The problem you are having is how you insert your nodes. I am not sure why, but you checked if the tree is empty and attempted to add your node to root. You passed root to the function as "p" so enter it as p!

CODE

void tree::insert(TNode *&p, itemType newItem)
{    
    if(isEmpty() || p == NULL)
    {
        // Here we create a node and assign it to p, the pointer we passed into the function.
        p = new TNode;
        p->item = newItem;
        p->lchild = NULL;
        p->rchild = NULL;
        size++;
    }
    else if (newItem < p->item)
    {
        insert(p->lchild, newItem);
    }
    else
    {
        insert(p->rchild, newItem);
    }
  
}


The second thing I noticed was that you put your structure of TNode inside your definition of Tree and then when you created *root you attempted to access it using tree::TNode. Problem with this is that it was set as private. Either make it public or move it outside the class of tree and define your root pointer as...

CODE

struct TNode *root;


Personally I would move it outside of your tree class since a node is a structure which can stand on its own and be used in various things other than a tree (like a linkedlist, queue, stack etc). I consider it on its own as an acceptable implementation that will allow other programmers to use your TNode structure to enhance your program later.

After you make these subtle changes, you will notice root is being entered and your search will come online and detect the root in your search.

Hopefully that gets you to the point of near completion for Friday.

Enjoy!

"At DIC we be TNode adding code ninjas!" decap.gif
User is online!Profile CardPM
+Quote Post

r10lover10
RE: Root Is Working, But Seach Function Crashed The Program
6 Dec, 2007 - 10:41 AM
Post #3

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 32


My Contributions
that was so kind of you, Martyr2 smile.gif
a hundred thank you to Martyr2 of released me out of bond.

If you have time, would you mind to check the java section and take a look at world clock project that I am having trouble with adding radiobutton. The purpose of the radiobutton is to provide optional time zone for user to play around with.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Root Is Working, But Seach Function Crashed The Program
6 Dec, 2007 - 12:06 PM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Wow, there's a whole lot of recursion going on there. wink2.gif

In main, the line struct tree::TNode *root; strikes me as odd. You made the struct private, how can you even do this? In any case, you shouldn't have nodes getting passed around in public. I'm using your tree to store my data, I shouldn't need to know anything about the structure of the storage to use it.

While you're recursing, you can probably simplify your code by never checking isEmpty again. You're starting at a node, you don't really need to check beyond the is null.

Hope this helps.
User is offlineProfile CardPM
+Quote Post

r10lover10
RE: Root Is Working, But Seach Function Crashed The Program
6 Dec, 2007 - 12:14 PM
Post #5

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 32


My Contributions
Now the problem I am facing is that the search function will crashed the program after an interger is inserted. If the search does not match the interger that was stored, the program will crashed.

thanks for pointing out the private root issue. I moved it to public now.

here's my code again

header:
CODE
typedef int itemType;

class tree
{  
    private:
        struct TNode
        {
            itemType item;
            TNode *lchild;
            TNode *rchild;
        };
        int size;
        TNode *p;
        TNode *q;

    public:

        tree();                        
        //constructor

        bool isEmpty() const;
        //goal:test whether tree is empty
        //pre:none
        //post:return true if tree is empty else return false

        bool isFull() const;
        //goal:test whether tree is full
        //pre:none
        //post:return true if tree is full else return false

        void insert(TNode *&p, itemType newItem);
        //goal:insert item
        //pre:tree exists
        //post:item inserted
        
        itemType search(TNode *&p, itemType newItem);
        //goal:search item
        //pre:tree exists
        //post:item being search
        
        int getSize();
        //goal:get the size of tree if is not empty
        //pre:tree exists
        //post:return tree size
    
        void preorder(TNode *&p);
        //goal:display tree in preoder
        //pre:tree exists
        //post:if tree is not empty

        void inorder(TNode *&p);
        //goal:display tree in inorder
        //pre:tree exists
        //post:if tree is not empty

        void postorder(TNode *&p);
        //goal:display tree in postorder
        //pre:tree exists
        //post:if tree is not empty

        struct TNode *root;
};


implementation
CODE
/***************************************************
data struc 8
binary search tree
visual c++
Wei Chan Lee
11/23/07
****************************************************/


#include "tree.h"
#include <iostream>
using namespace std;


tree::tree()                            // constructor
{
    size = 0;
    root = NULL;
    
}

bool tree::isEmpty() const                // if its empty?
{
    return (size == 0);
}

bool tree::isFull() const                // if its full?
{
    return false;
}
  
void tree::insert(TNode *&p, itemType newItem)    // insert item into tree
{    
    if(isEmpty() || p == NULL)
    {      
        p = new TNode;
        p->item = newItem;
        p->lchild = NULL;
        p->rchild = NULL;
        size++;
    }

    else if (newItem < p->item)                    
    {
        insert(p->lchild, newItem);                // new item < than root
    }
    else
    {
        insert(p->rchild, newItem);                // new item > than root
    }
  
}

itemType tree::search(TNode *&p, itemType newItem)    // searh item in tree
{
    if(!isEmpty())
    {
        if(p->item == newItem)
        {
            return p->item;                            // item found
        }
        else if(newItem < p->item)
        {
            search(p->lchild, newItem);                // search at smaller section
        }
        else
        {
            search(p->rchild, newItem);                // search at larger section
        }
    }
    else
    {
        cout << "\nERROR!! Tree is EMPTY." << endl;            
    }
}

int tree::getSize()                                    // check the size of tree
{
    return size;
}

void tree::preorder(TNode *&p)                        // display in preorder
{    
    if(!isEmpty())
    {

    if(p!=NULL)
    {
        cout << p->item<< " ";
        preorder(p->lchild);
        preorder(p->rchild);
    }
    }
    else
    {
        cout << "\nERROR!! Tree is EMPTY\n" << endl;
    }
}

void tree::inorder(TNode *&p)                        // display in inorder
{
    if(!isEmpty())
    {

    if(p!=NULL)
    {
        inorder(p->lchild);  
        cout << p->item << " ";
        inorder(p->rchild);
    }
    }
    else
    {
        cout << "\nERROR!! Tree is EMPTY\n" << endl;
    }
}

void tree::postorder(TNode *&p)                        // display in postorder
{
    if(!isEmpty())
    {

    if(p!=NULL)
    {
        postorder(p->lchild);
        postorder(p->rchild);
        cout << p->item<< " ";
    }
    }
    else
    {
        cout << "\nERROR!! Tree is EMPTY\n" << endl;
    }
}


main:
CODE

/***************************************************
data struc 8
binary search tree
visual c++
Wei Chan Lee
11/23/07
****************************************************/


#include "tree.h"
#include <conio.h>
#include <iostream>
using namespace std;


int main()
{
    itemType anItem;
    int option;
    struct tree::TNode *root;
    root = NULL;

    tree runtree;

    // user menu

    do{    
        cout << "\n Binary Search Tree";
        cout << "\n 1.Insert Item";
        cout << "\n 2.Search Item";
        cout << "\n 3.Tree Size";
        cout << "\n 4.Display Tree In Preorder";
        cout << "\n 5.Display Tree In Inorder";
        cout << "\n 6.Display Tree In Postorder";
        cout << "\n 7.Exit" << endl;

        cin >> option;

        switch(option)
        {
        case 1:                                    // Insert Item
            cout << "\nEnter an integer: ";
            cin >> anItem;
            runtree.insert(root,anItem);
            break;
            //end case 1

        case 2:                                    // Search Item
            cout << "\nEnter an integer: ";
            cin >> anItem;
            if(runtree.search(root, anItem) == anItem)
            {
                cout << runtree.search(root, anItem) << " founded in tree!!" << endl;
            }
            else
            {
                cout << anItem << " is not in tree " << endl;
            }
            break;
            //end case 2

        case 3:                                    // Check Size
            cout << "\nThe tree size is " << runtree.getSize() << "." << endl;
            break;
            //end case 3
        
        case 4:                                    // Display Preorder
            cout << endl;
            runtree.preorder(root);
            cout << endl;
            break;
            //end case 4

        case 5:                                    // Display Inorder
            cout << endl;
            runtree.inorder(root);
            cout << endl;
            break;
            //end case 5

        case 6:                                    // Display Postorder
            cout << endl;
            runtree.postorder(root);
            cout << endl;
            break;
            //end case 6
         }
  }while(option != 7);                            // Exit Program

    getch();
    
    return 0;
}


Thank you for your effort and concern of helping me.


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:11PM

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