Thread: Segmentation Fault-Binary Search Tree

  1. #1
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25

    Segmentation Fault-Binary Search Tree

    Code:
    #include<iostream> 
    #include<string> 
    using namespace std; 
    class node 
    { 
    private: 
        int data; 
        node *left; 
        node *right; 
    public: 
        node(int d) 
        { 
            data=d; 
            cout<<data<<endl; 
            left=NULL; 
            right=NULL; 
        } 
        friend class list; 
    }; 
    class list 
    { 
        public: 
            node *root; 
            node *newnode; 
            list() 
            { 
              root=NULL; 
            } 
            void insert(node *r, int data) 
            { 
                 
                if(r==NULL) 
                { 
                    r=new node(data); 
                } 
                else if(data>r->data) 
                { 
                    insert(r->right,data); 
                } 
                else if(data<r->data) 
                { 
                    insert(r->left,data); 
                } 
            } 
            /*void insert(node *nd,int data) 
            { 
                    if(nd==NULL) 
                { 
                    nd=new node(data); 
                } 
                else if(data>nd->data) 
                { 
                    insert(nd->right,data); 
                } 
                else if(data<nd->data) 
                { 
                    insert(nd->left,data); 
                } 
            }*/ 
            void display(node *temp) 
            { 
                cout<<"sdff"<<endl; 
                cout<<temp->data; 
                if(temp) 
                { 
                    cout<<"dsfs"<<temp->data<<endl; 
                    display(temp->left); 
                    display(temp->right);     
                     
                } 
                 
            } 
     
            void display() 
            { 
                 
                if(root) 
                { 
                    cout<<root->data<<endl; 
                    display(root->right); 
                    display(root->left); 
                         
                     
                } 
                 
            } 
             
    }; 
    int main() 
    { 
        list l; 
        l.insert(l.root,9); 
        l.insert(l.root,6); 
        l.insert(l.root,10); 
         
        l.display(l.root); 
         
    }

    what is wrong with my code??
    Attached Images Attached Images Segmentation Fault-Binary Search Tree-screenshot-2012-11-08-09-58-11-jpg 

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Although it's not your problem, if you follow standard good practice and don't have any public variables in your class, the changes you will have to make will likely remove your problem. If they do, think about it and find out what you changed and what the problem was
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    At a quick glance, it will crash on line 63 when dereferencing a NULL pointer because you never actually insert anything.
    That's because your insert method is broken. It has no effect outside of the function call except that it leaks memory.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. help with segmentation fault, binary tree
    By archibald627 in forum C Programming
    Replies: 7
    Last Post: 02-10-2011, 12:30 PM
  3. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  4. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM