0

I am trying to create a binary tree that when you try to add a new node to that, it adds the node to first position that is nullptr.

actually making a complete binary tree.

Look at the code below:

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

class node{
private:
    char data;
    node* right;
    node* left;
public:
    node(char n){
        data = n;
        left = nullptr;
        right = nullptr;
    }
    char getdata(){
        return data;
    }
friend class binTree;
};

class binTree{
private:
    node *root;
public:
    binTree(){
        root = nullptr;
    }
    binTree(node *root){
    this->root = root;
    }

    node* getRoot(){
    return this->root;
    }


    void addNode(char data){
        cout << "adding " << data << endl;

        if(root == nullptr) {
            root = new node(data);
            return;
        }


        queue<node*> Q;
        Q.push(root);
        node* toadd;
        while(true) {
            node* toadd = Q.front();
            Q.pop();
            Q.push(toadd->left);
            Q.push(toadd->right);

            if(toadd->left == nullptr)  break;
            if(toadd->right == nullptr) break;
        }

        if((toadd->left) == nullptr)
        {
            cout << "add data to the left of " << toadd -> data << endl;
            toadd->left = new node(data);
        } else if((toadd -> right) == nullptr){
            cout << "add data to the right of " << toadd -> data << endl;
            toadd->right = new node(data);
        } else {
            cout << "toadd left and right are not nullptr" << endl;
        }

    }
};

int main()
{
    binTree bin;
    string s = "abcdefg";
    cout << s << endl << endl;
    for(int i = 0; i < s.size(); i++)
    {
        bin.addNode(s[i]);
    }
}

when I run this code the output is:

abcdefg

adding a
adding b
toadd left and right are not nullptr
adding c
toadd left and right are not nullptr
adding d
toadd left and right are not nullptr
adding e
toadd left and right are not nullptr
adding f
toadd left and right are not nullptr
adding g
toadd left and right are not nullptr

the strange part is when printing "toadd left and right are not nullptr" because there is a while(true) and the only exit conditions are:

            if(toadd->left == nullptr)  break;
            if(toadd->right == nullptr) break;

so one of these conditions were true that we could break the loop; so we should enter one of if or else if part of the following code (after the while) but surprisingly we enter the else part and end of printing "toadd left and right are not nullptr". can anyone explain this behavior?

1 Answer 1

7

You've defined toadd twice, once before the loop and once inside. Easy mistake to make.

Sign up to request clarification or add additional context in comments.

1 Comment

-Werror=shadow on gcc and clang avoids this category of error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.