0

A simple binary search tree class declaration:

#include <vector> #include <stdio.h> // Provides various structures utilized by search algorithms. // Represents an generalized node with integer value and a set of children. class Node { protected: std::vector<Node*> children; int value; public: //Creates a new instance of a Node with a default value=-1. Node(){value = -1;} //Creates a new instance of a Node with a specified value. explicit Node(int value){this->value = value;} virtual ~Node(){delete children;} //Adds new Node with specified value to the list of child nodes. Multiple //children with the same value are allowed. //Returns added node. virtual Node* Insert(int value); //Removes first occurrence of a Node with specified value among children. virtual void Remove(int value); }; // Represents a binary search tree node with at most two children. class BTNode: public Node { public: //Creates a new instance of a BTNode with a default value=-1. BTNode():Node(){} //Creates a new instance of a BTNode with a specified value. explicit BTNode(int value):Node(value){} //Adds new BTNode with specified value to the list of child nodes in an //ordered manner, that is right child value is >= value of this node and //left child value < value of this node. virtual BTNode* Insert(int value); //Removes first occurrence of a Node with specified value from the tree. virtual void Remove(int value); //Returns a node with specified value. virtual BTNode* Search(int value); }; 

And eclipse complains about it's definition:

BTNode* BTNode::Search(int value){ if (this->value == value) return *this; //Determines whether value is in left(0) or right(1) child. int child = this->value > value ? 0 : 1; if (children[child] != NULL) return children[child]->Search(value); return NULL; } 

exactly where the call children[child]->Search(value) takes place with a message "method Search could not be resolved". Build runs just fine (no compilation errors whatsoever). What's the problem with that?

P.S.:Haven't tried running the code,yet. Working on it.

2
  • You say Eclipse complains. Is that during compilation or just when it tries to parse your code to analyze it for auto-completion and the like? Commented Apr 12, 2013 at 13:23
  • @JohnZwinck, added more details. So it's the second option. Commented Apr 12, 2013 at 13:26

1 Answer 1

2

Search is part of the BTNode interface but it is not part of Nodes interface, children is a vector of Node* so it is not valid to call Search on a Node *. If it makes sense for Node to have a Search method then adding it to Node would fix that issue. If not then you need to rethink your design and that is probably beyond the scope of this question.

There are also a few other issues. You have:

virtual ~Node(){delete children;} 

but children is not a pointer it is a std::vector<Node*>. You need to iterate over the vector and call delete each element. In Search you have this:

if (this->value == value) return *this; 

but Search returns a BTNode* so it should be:

if (this->value == value) return this ; 
Sign up to request clarification or add additional context in comments.

3 Comments

Makes sense. Can you please suggest design improvements or best practices for my purposes? (I'm rather new to c++)
Do I get it correctly that I need to iterate over the vector of pointers and call delete for each pointer?
@DenysS. Just updated answer, you need to iterate over the vector and call delete on each element

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.