Like Tree5Likes
  • 1 Post By tabstop
  • 1 Post By tabstop
  • 1 Post By tabstop
  • 2 Post By tabstop

BST program terminating abnormally

This is a discussion on BST program terminating abnormally within the C++ Programming forums, part of the General Programming Boards category; I am creating a BST class that has insertion and traversal function (USING RECURSION)..please tell me why this program is ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    73

    BST program terminating abnormally

    I am creating a BST class that has insertion and traversal function (USING RECURSION)..please tell me why this program is termination abnormally..

    Code:
    #include <iostream>#include <conio.h>
    
    
    using namespace std;
    class Node//BST node
    {    
        friend class BST;
        private:
                int Data;
                Node *L;
                Node *R; 
    };
    class BST
    {
        private:
                Node *Start;
        public:
                BST()
                {Start=NULL;}
                void insertNode(int X);
                void inorder();
                    
    };
    
    
    void BST::insertNode(int X)
    {
        if(Start==NULL)
        {
            Node *New=new Node;
            New->Data=X;
            New->L=NULL;
            New->R=NULL;
            Start=New;
            return;
        }
        else if(X<Start->Data)
        {
            Start=Start->L;
            BST::insertNode(X);
        }
        else
        {
            Start=Start->R;
            BST::insertNode(X);
        }
        
    }
    void BST::inorder()
    {
        if(Start!=NULL)
        {
            Start=Start->L;
            BST::inorder();
            cout<<Start->Data<<endl;
            Start=Start->R;
            BST::inorder();
        }
        else
        return;
    }
    int main()
    {
        BST T;
        T.insertNode(3);    
        T.insertNode(5);
        T.insertNode(65);
        T.insertNode(32);
        T.insertNode(12);
        T.inorder();
        getch();
        return 0;
    }
    Last edited by progmateur; 01-26-2014 at 01:28 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you really destroying your tree when you do an inorder traversal? Why?

    You've got some options, but changing the data member that keeps track of the root of the tree each time is not one of them. (Perhaps use a helper function that takes a Node argument, and this helper function can be recursive?)
    progmateur likes this.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    73
    Well,but when I was checking whether the tree is properly inserted or not..I see that the insert() is malfuctioning..please help

    I was incorporating functions like...

    Code:
    void Display()
                {
                    cout<<(Start->R)->Data<<endl;
                }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yeah you're destroying your tree in your insert function too. You cannot change Start, once you've built your tree. Create a new Node* variable and walk your tree appropriately until you find a NULL spot to place your new item, then (using that new variable) set the link from the currently-existing tree to your new item.
    progmateur likes this.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    73
    well ,i get what you said and did this accordingly...but without results
    please enlighten @tabstop

    Code:
    #include <iostream>#include <conio.h>
    
    
    using namespace std;
    class Node//BST node
    {	
    	friend class BST;
    	private:
    			int Data;
    			Node *L;
    			Node *R; 
    };
    class BST
    {
    	private:
    			Node *Start;
    	public:
    			BST()
    			{Start=NULL;}
    			void insertNode(Node *Head,int X);
    			void inorder(Node *Head);
    			void letInsert(int X);
    			void letInorder();
    			
    //			void Display()
    //			{
    //				cout<<(Start->R)->Data<<endl;
    //			}
    				
    };
    void BST::letInsert(int X)
    {
    	insertNode(Start,X);
    }
    void BST::letInorder()
    {
    	inorder(Start);
    }
    void BST::insertNode(Node *Head,int X)
    {
    	if(Head==NULL)
    	{
    		Node *New=new Node;
    		New->Data=X;
    		New->L=NULL;
    		New->R=NULL;
    		Head=New;
    		return;
    	}
    	else if(X<Start->Data)
    	{
    		Head=Head->L;
    		BST::insertNode(Head,X);
    	}
    	else
    	{
    		Head=Head->R;
    		BST::insertNode(Head,X);
    	}
    	
    }
    void BST::inorder(Node *Head)
    {
    	if(Start!=NULL)
    	{
    		BST::inorder(Head->L);
    		cout<<Head->Data<<endl;
    		BST::inorder(Head->R);
    	}
    	else
    	return;
    }
    int main()
    {
    	BST T;
    	T.letInsert(3);	
    	T.letInsert(5);
    	//T.Display();
    	
    	T.letInsert(65);
    	T.letInsert(32);
    	T.letInsert(12);
    	T.letInorder();
    	getch();
    	return 0;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. You do need to change Start the very first time.

    2. Using Head as a temporary node that walks through the tree is perhaps the worst variable name in the history of C.

    2. When you are walking through the tree, you need to compare to Head, not to Start. You also need to set the link from the previously-existing node to the new node, and as such insert is often not written as a recursive function.
    progmateur likes this.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    73
    Still not working... :'(

    You are right about the variable name..

    A piece of code would be very helpful

    Code:
    #include <iostream>
    #include <conio.h>
    
    
    using namespace std;
    class Node//BST node
    {	
    	friend class BST;
    	private:
    			int Data;
    			Node *L;
    			Node *R; 
    };
    class BST
    {
    	private:
    			Node *Start;
    	public:
    			BST()
    			{Start=NULL;}
    			void insertNode(Node *Head,int X);
    			void inorder(Node *Head);
    			void letInsert(int X);
    			void letInorder();
    			
    //			void Display()
    //			{
    //				cout<<(Start->R)->Data<<endl;
    //			}
    				
    };
    void BST::letInsert(int X)
    {
    	if(Start==NULL)
    	{
    		Node *New=new Node;
    		New->Data=X;
    		New->L=New->R=NULL;
    		Start=New;
    		return;
    	}
    	else
    	BST::insertNode(Start,X);
    }
    void BST::letInorder()
    {
    	BST::inorder(Start);
    }
    void BST::insertNode(Node *Head,int X)
    {
    	if(Head==NULL)
    	{
    		Node *New=new Node;
    		New->Data=X;
    		New->L=NULL;
    		New->R=NULL;
    		Head=New;
    		return;
    	}
    	else if(X<Head->Data)
    	{
    		Head=Head->L;
    		BST::insertNode(Head,X);
    	}
    	else
    	{
    		Head=Head->R;
    		BST::insertNode(Head,X);
    	}
    	
    }
    void BST::inorder(Node *Head)
    {
    	if(Head!=NULL)
    	{
    		BST::inorder(Head->L);
    		cout<<Head->Data<<endl;
    		BST::inorder(Head->R);
    	}
    	else
    	return;
    }
    int main()
    {
    	BST T;
    	T.letInsert(3);	
    	T.letInsert(5);
    	//T.Display();
    	
    	T.letInsert(65);
    	T.letInsert(32);
    	T.letInsert(12);
    	T.letInorder();
    	getch();
    	return 0;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm pretty sure you need to stop typing and start thinking. What needs to happen when you insert a node? Write it down on a piece of paper. Once you know what needs to happen, then you can worry about writing code to make it happen.
    oogabooga and progmateur like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating Pipe Program
    By Mcdom34 in forum C Programming
    Replies: 1
    Last Post: 06-28-2012, 12:50 AM
  2. Terminating Program
    By millsy2000 in forum C Programming
    Replies: 11
    Last Post: 04-22-2010, 11:31 PM
  3. How can I get my program to stop terminating?
    By mklickman in forum C Programming
    Replies: 3
    Last Post: 09-19-2006, 11:06 AM
  4. Terminating a GLUT program?
    By glo in forum Game Programming
    Replies: 4
    Last Post: 07-05-2006, 02:37 AM
  5. Terminating a program
    By whisper11 in forum C++ Programming
    Replies: 1
    Last Post: 02-03-2005, 11:59 AM

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