0





18

Advertisement

This is the code for implementation of Binary trees.In the Push function I get an Error the address in p->right is not equal to root->right where clearly I have assigned p the value of root.Can someone please explain that to me.

#include<iostream>
using namespace std;
struct node{  
    int value;
    node *right,*left;
    node(int value){
        this->value=value;
        right=NULL;
        left=NULL;
    }
};
class tree{
    public:
    node *root; 
        tree(){
            this->root=NULL;
        }
        tree(int value){
            root=new node(value);
        }
        void push(int value){  //To insert into the binary tree
            if(root==NULL){
            root=new node(value);
                return ;
            }
            node *p=this->root;
            cout<<root<<endl<<p<<endl;
            while(p!=NULL){
                if(value>=p->value){                    
                    p=p->right;
                }
                else if(value<p->value){                    
                    p=p->left;
                }
            }
            p=new node(value);
        }
};
int main(){
    tree t(30);
    t.push(20);
}
Question author Ali-jawad | Source

Advertisement


0


struct node{  
int value;
node *right,*left;
node(int value){
    this->value=value;
    this->right=NULL;
    this->left=NULL;
}

right and left variables are pointers.

Answer author Rjmimi38