2

I just wanted to implement a TreeNode class that works similar to that of a struct node for a tree implementation.

Everything is working fine except for the output that is including 0 at the beginning of the inOrder traversal. Can anyone please explain it to me why is that happening?

Input:

22,1,2,3,5,4,11,20,19,24,21

Output:

0 1 2 3 4 5 11 19 20 21 22 24 
#include <bits/stdc++.h>

using namespace std;

class TreeNode{

    public:

        int data;
        TreeNode* left;
        TreeNode* right;

        TreeNode(){
            left = NULL;
            right = NULL;
        }

        TreeNode(int val){
            data = val;
            left = NULL;
            right = NULL;
        }

};

void insertInorder(TreeNode* cur, int d){

    if(d <= cur->data){
        if(cur->left == NULL)
            cur->left = new TreeNode(d);
        else
            insertInorder(cur->left,d);
    }
    else{
        if(cur->right == NULL)
            cur->right = new TreeNode(d);
        else
            insertInorder(cur->right,d);
    }
}

TreeNode* makeTree(vector<int> v){

    TreeNode* root = NULL;

    for(int start = 0; start <= v.size(); start++){
        if(start == 0){
            root = new TreeNode();
            root->data = v[0];
        }
        insertInorder(root,v[start]);
    }

    return root;
}

void printInorder(TreeNode* node) 
{ 
    if (node == NULL) 
        return; 
  
    /* first recur on left child */
    printInorder(node->left); 
  
    /* then print the data of node */
    cout << node->data << " "; 
  
    /* now recur on right child */
    printInorder(node->right); 
} 

int main(){

    vector<int> x = {22,1,2,3,5,4,11,20,19,24,21};

    TreeNode* r = makeTree(x);

    printInorder(r);


    return 0;

}

Edit:

To the people visiting this questions at a future date. Better practices states that we shouldn't use

#include <bits/stdc++.h>
using namespace std;

Using namespace std can result into future namespace collisions in the code. For reference here. I did the same mistake but I won't be doing this from now on.

Please refer to the link provided by @Jabberwocky here

tblaze
  • 138
  • 10
  • 1
    OT: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – Jabberwocky Mar 18 '21 at 09:04
  • 3
    maybe `start <= v.size()` -> `start < v.size()` in your `makeTree` function. – anastaciu Mar 18 '21 at 09:05
  • 1
    `start` is definitely going out of range, therefore you have undefined behaviour. Compile in debug mode and run it with your debugger and you should get a diagnostic – Jabberwocky Mar 18 '21 at 09:08
  • 1
    @user12112359 you're welcome, aside from Jabberwocky's comment, `using namespace std;` is also not a good practice, take a look at [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/6865932) for details and alternatives. – anastaciu Mar 18 '21 at 09:13
  • 2
    When `start` is 0, you first create a root containing `v[0]`, then you also insert `v[0]` in the tree. That doesn't seem right. (Start with small and systematic test cases, like `vector x = {1};` - which is also broken - not large and arbitrary ones.) – molbdnilo Mar 18 '21 at 09:14
  • I wanna help - a random debugger – gkhaos Mar 18 '21 at 09:21

0 Answers0