-1

My BST code is not showing any output and there is no error? can someone please explain what I'm doing wrong? I'm using Visual Code I ran the code on multiple online compilers as well but just got a segmentation error and on vs code it's not showing anything at all.

#include <iostream>
#include <type_traits>
using namespace std;

class node 
{
 public:
  int data;
  node *left, *right;

  node(int data){
  this->data = data;
  this->left = NULL;
  this->right = NULL;
 }
 };

 class BST{
 public:
 node* addNode(int data){
    node* newNode = new node(data);
    return newNode;
}

void Inorder(node* root){
    if(root == NULL)
        return;
        
    Inorder(root -> left);
    
    cout << root -> data << "\t";
    
    Inorder(root -> right);
}

node* insert(node* newNode, int data){
    if(newNode == NULL)
        return addNode(data);
        
    if(data < newNode -> data)
        newNode -> left = insert(newNode, data);
        
    else if(data > newNode -> data)
        newNode -> right = insert(newNode, data);
    
    
    return newNode;
}

};

int main(){
node *root = NULL;
BST objbst;
root = objbst.insert(root, 8);
root = objbst.insert(root, 3);
root = objbst.insert(root, 1);
root = objbst.insert(root, 6);
root = objbst.insert(root, 7);
root = objbst.insert(root, 10);
root = objbst.insert(root, 14);
root = objbst.insert(root, 4);

cout << "Inorder traversal: ";
objbst.Inorder(root);

}

if I'm doing something wrong then can someone please tell me what I'm doing wrong?

ks1322
  • 33,961
  • 14
  • 109
  • 164
sniffer
  • 11
  • 2
  • 2
    What did you observe when stepping through your code line by line with the [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?r=Saves_AllUserSaves)? – πάντα ῥεῖ Dec 15 '22 at 21:02
  • segmentation error – sniffer Dec 15 '22 at 21:04
  • Well did you check the stack trace? At which code line does that occur, when you tried stepping over? – πάντα ῥεῖ Dec 15 '22 at 21:06
  • Handy tool: https://godbolt.org/z/GPnrnzhYe Note the compiler arguments that crank up the warnings and turn on runtime sanitizers. The depth of the stacktrace produced strongly suggests you got a ... wait for it! Stack Overflow! – user4581301 Dec 15 '22 at 21:11
  • I don't know what a stack trace is – sniffer Dec 15 '22 at 21:11
  • Did you step through your code, as i mentioned? – πάντα ῥεῖ Dec 15 '22 at 21:13
  • A stack trace is a stack of all the functions (well, most of them. Some could be inlined). When the function is called, onto the stack. When the function returns, it's popped off the stack. It's an excellent tool for seeing how a program got into it's current mess. – user4581301 Dec 15 '22 at 21:13
  • @sniffer it shows the functions that were called, until the error occurred. – πάντα ῥεῖ Dec 15 '22 at 21:15
  • If you look at the stack trace I posted you can see that line 41 is called over and over until the system ran out of automatic memory. This means that `if(newNode == NULL)` is never true. You probably have a loop in the code, so step through the `insert` function with the debugger to make sure, and then restart the program again to keep a close eye on how the loop got there. – user4581301 Dec 15 '22 at 21:17
  • @sniffer -- The purpose of a debugger is so that you can run your code a line-at-a-time, set breakpoints, look at the values of variables, see the flow of the program, etc. amd from there, see what needs to be changed or fixed. This is a required skill for any programmer. – PaulMcKenzie Dec 15 '22 at 21:18
  • 1
    in your code `data` never equals `newNode -> data` hence `insert(root,x)` always calls `insert(root,x)` and then `data` again does not equal `newNode -> data` hence it calls `insert(root,x)` and then .... – 463035818_is_not_an_ai Dec 15 '22 at 21:20
  • The trick to using a debugger is knowing what you should expect from each line of code. As you step through the program, you watch for the unexpected. The unexpected means you made a bad assumption about what the code was supposed to do and you've got a bug. Which you have now found. Understanding how to fix the bug might take a bit longer, but at least you know where it is. – user4581301 Dec 15 '22 at 21:20
  • @sniffer Also, you should have started with only two nodes, not 8 nodes. You would have seen that even two nodes causes the problem. If two nodes doesn't work correctly, then certainly 8 nodes won't work. Always start with the *minimal* amount of data that reproduces the problem. This makes the problem easier to diagnose. – PaulMcKenzie Dec 15 '22 at 21:25
  • IMHO, using `this->` syntax when not necessary is wrong. If you use a coding style where parameter names are different than member names, you don't need to use `this->`. – Thomas Matthews Dec 15 '22 at 21:25
  • Please ditch the habit of putting spaces between strongly-bound operators such as `->`. Nobody in the professional world puts spaces there, and doing so reduces the readability of your code: write `newNode->data`, _not_ `newNode -> data` – paddy Dec 15 '22 at 22:27

1 Answers1

0

modify the insert function as given below and then run your code, you need to call left or right instances of node(based on condition) when traversing the tree recursively!

node* insert(node* Node, int data){
   if(Node == NULL)
       return addNode(data);
        
   if(data < Node -> data)
       Node -> left = insert(Node->left, data);
        
   else if(data > Node -> data)
       Node -> right = insert(Node->right, data);
    
    
   return Node;
}
Akshat
  • 27
  • 3