Revision binary Tree

Hey guys I am doing some revision on binary trees I found in an old book of mine,this is quite embarrassing but I guess no question is too small

so with my code,the tree works fine but I am getting an reviseAGainagain\main.cpp|145|error: no match for 'operator=' (operand types are 'binNode' and 'binNode*')

error message

I mean why would I have to overload the == operator to use this simple notation?
All I am doing is declaring a binNode n on the stack and then setting it equal to the whatever the binNode function returns

now I know the addNode function returns an address,so you can't set an object equal to another object like this? the object or the address in which addNode returns is an address of a binNode in other words it is pretty much a binNode

so I probably already answered my own question lol but why do I get a error: no match for 'operator=' (operand types are 'binNode' and 'binNode*')? I thought I would get a type mis match error that is what confuses me

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  class binNode{

 public:

     int number;
     binNode* right;
     binNode* left;

};


class Tree{

  public:
      Tree(){}

       binNode* addNode(binNode* tree,int number){

         if(tree == NULL){

            binNode *newNode = new binNode;
            newNode->number = number;
            newNode->left = NULL;
            newNode->right = NULL;
            return newNode;
         }
         if(number > tree->number){

            tree->right = addNode(tree->right,number);

         }else{

            tree->left = addNode(tree->left,number);
         }

         return tree;
       }
       binNode* searchTree(binNode* tree,int number){

           if(tree == NULL){
             return NULL;
           }
           if(tree->number == number){
             return tree;
           }
           if(number > tree->number){

               searchTree(tree->right,number);
           }else{
               searchTree(tree->left,number);
           }
       }

};


int main()
{

       Tree p;
       // binNode* t = new binNode; this will work
       // t = p.addNode(t,3);  this will work
       binNode n; 
       n = p.addNode(&n,3); // won't work
       p.addNode(t,4);
       p.addNode(t,7);
       p.addNode(t,2);
       p.addNode(t,2);

       binNode* b = p.searchTree(t,);
       if(b == NULL){

         cout << "could not find node" << endl;
       }else{

         cout << b->number << endl;

       }
}
no match for 'operator=' (operand types are 'binNode' and 'binNode*')

You're trying to assign a binNode pointer to a binNode. This doesn't make sense, semantically.
The compiler is trying to find a way make it the assignment operation work, so it says it can't find an operator=(binNode, binNode*);. Don't actually make that operator, though.

1
2
       binNode n; 
       n = p.addNode(&n,3); // won't work 

n is a binNode. p.addNode is trying to return a pointer-to-binNode.

Perhaps try
1
2
binNode* n = nullptr;
n = p.addNode(n, 3);


You also never initialize your binNode values, so they contain undefined values.

Last, in your searchTree function, not all code paths return anything. You probably meant to do
return searchTree(tree->right, number);
Last edited on
thanks Ganado,yeah I thought that may have been the problem but was wondering why it gave me that error was expecting something different lol

and yes good point I should have said return search(tree->right,number);

although the code still works for some reason even without the keyword return before it which is strange

although the code still works for some reason even without the keyword return before it which is strange

Without the return it will always return the first node added (the root). So maybe you were searching for that?

You aren't really using the classes properly. The node class could benefit from a constructor. The Tree class could hold the root instead of just being a namespace for its methods.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
using namespace std;

struct Node{
    int data;
    Node* right;
    Node* left;
    Node(int n) : data(n), right(nullptr), left(nullptr) {}
};

class Tree{
private:
    Node *root;

    static Node* searchTree_r(Node *tree, int data) {
        if (tree && tree->data != data)
            return searchTree_r(data > tree->data ? tree->right : tree->left, data);
        return tree;
    }

    static Node* addNode_r(Node *tree, int data){
        if (!tree)
            return new Node(data);
        if (data > tree->data)
            tree->right = addNode_r(tree->right, data);
        else
            tree->left = addNode_r(tree->left, data);
        return tree;
    }

    static void deleteTree(Node *t) {
        if (!t) return;
        deleteTree(t->left);
        deleteTree(t->right);
        delete t;
    }

    static void print_r(Node *t, ostream& os) {
        if (!t) return;
        print_r(t->left, os);
        cout << t->data << ' ';
        print_r(t->right, os);
    }

public:
    Tree() : root(nullptr) { }

    ~Tree() { deleteTree(root); }

    void addNode(int data) {
        root = addNode_r(root, data);
    }

    Node* searchTree(int data) {
        return searchTree_r(root, data);
    }
    
    ostream& print(ostream& os) {
        print_r(root, os);
        return os << '\n';
    }
};

ostream& operator<<(ostream& os, Tree& t) { return t.print(os); }

int main() {
    Tree p;
    
    for (auto n: {4, 6, 3, 8, 7, 0, 7, 1})
        p.addNode(n);

    for (auto n: { -1,0,1,2,3,4,5,6,7,8,9,10 }) {
        Node* b = p.searchTree(n);
        if (!b)
            cout << n << " not found\n";
        else
            cout << "found " << b->data << '\n';
    }
    
    cout << p << '\n';
}

Last edited on
Topic archived. No new replies allowed.