Thread: C++ BST segmentation fault

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    2

    Lightbulb C++ BST segmentation fault

    Hi,

    I am trying to build a BST. I have so far this code and I must have made a mistake somewhere with the pointers, but I could not figure out where. So when I run it it says: "Segmentation fault (core dumped).

    Here is the code:

    Code:
    //BTree#include <iostream>
    using namespace std;
    
    
    //BTree struct
    struct BTree{
        int data;
        BTree* left;
        BTree* right;
    };
    
    
    //BTree new node
    BTree* BTree_NewNode(int inputData){ //inputData = new entry in console
        BTree* node=new BTree;
        node->data=inputData;
        node->left=NULL;
        node->right=NULL;
    
    
        return(node);
    };//BTree new node end
    
    
    BTree* BTree_Insert(BTree* node, int inputData){
        if(node==NULL){
            return(BTree_NewNode(inputData));
        }else{
            if(inputData<=(node->data)){
                node->left=BTree_Insert(node->left,inputData);
            }else{
                node->right=BTree_Insert(node->right,inputData);
            }
    
    
            return(node); //else statemnet return
        }
    };//BTree_Insert end
    
    
    //Print out BTree
    void BTree_Print(BTree* node){
        if((node->left)==NULL && (node->right)==NULL){
            return;
        }else{
            if((node->left)!=NULL){
                BTree_Print(node->left);
                cout<<"Data: "<<node->data<<endl;
            }
            else{
                BTree_Print(node->right);
                cout<<"Data: "<<node->data<<endl;
            }
        }
    }//end BTree_Print()
    
    
    int main(){
        BTree* root;
        BTree_Insert(root,5);
        BTree_Insert(root,2);
        BTree_Insert(root,7);
        BTree_Insert(root,8);
        BTree_Insert(root,1);
    
    
        BTree_Print(root);
    
    
        return 0;//main return
    }//main() end

  2. #2
    Registered User
    Join Date
    Oct 2015
    Posts
    2
    Quote Originally Posted by Roland Kajatin View Post
    Code:
    int main(){
        BTree* root;
        BTree_Insert(root,5);
        BTree_Insert(root,2);
        BTree_Insert(root,7);
        BTree_Insert(root,8);
        BTree_Insert(root,1);
    
    
        BTree_Print(root);
    
    
        return 0;//main return
    }//main() end
    EDIT to:

    Code:
    int main(){
        BTree* root=BTree_NewNode(5);
        BTree_Insert(root,4);
        BTree_Insert(root,8);
        BTree_Insert(root,6);
        BTree_Insert(root,1);
    
    
        BTree_Print(root);
    
    
        return 0;//main return
    }//main() end
    So this way it works to some extent. Still it only displays some of the values..

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    BTree_Insert does not connect the new node to the tree (i.e. does not set parent->left, parent->right).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    (I'm having a deja-vu here)
    you code is terribly C-like. all of the functions you wrote should be some member functions. you should have a "Tree" class which inserts "Node" objects. both "Tree" and "Node" should have constructors and destructors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation fault 11
    By Chanakya in forum C++ Programming
    Replies: 7
    Last Post: 06-30-2012, 03:01 PM
  3. segmentation fault
    By deepseawolf in forum C Programming
    Replies: 18
    Last Post: 03-13-2011, 08:55 PM
  4. Segmentation fault! Why?
    By cvrcak in forum C Programming
    Replies: 4
    Last Post: 06-25-2008, 07:24 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread