Is my solution good style?

ok so I am supposed to define a couple of simple recursive functions to operate on a binary search tree. the class definition has been givin to us in two seperate header files the first TreeNode.hpp


class TreeNode {
public:
TreeNode(): left(0), right(0), data(0) {}
TreeNode( int n ): left(0), right(0), data(n) {}

TreeNode *leftSubtree() { return left; }
TreeNode *rightSubtree() { return right; }

void leftSubtree( TreeNode *left ) { this->left = left; }
void rightSubtree(TreeNode *right) { this->right = right; }

int& value() { return data; }

TreeNode local_insert( TreeNode *root, int v );
private:
TreeNode *left, *right;
int data;
};

and the second BinarySearchTree.cpp

class BinSearchTree {
public:
BinSearchTree();
void insert( int v ) { root = local_insert( root, v ); }
bool find( int v );
bool iterFind( int v );
int size();
void inOrderDump();
void levelOrderDump();
int kthSmallest( int k );
void valuesAtLevel( int k );
void iterValuesAtLevel( int k );
int maxDepth();
int iterMaxDepth();
bool hasRootToLeafSum( int sum );
bool areIdentical( BinSearchTree *bst );
BinSearchTree *intersectWith( BinSearchTree *bst );
BinSearchTree *unionWith( BinSearchTree *bst );
BinSearchTree *differenceOf( BinSearchTree *bst );

private:
TreeNode *local_insert( TreeNode *, int );
TreeNode *root;
};

I am also provided with this

TreeNode *BinSearchTree::local_insert( TreeNode *root, int v ) {
if( root == NULL )
return new TreeNode( v );
if( root->value() < v )
root->rightSubtree( local_insert( root->rightSubtree(), v ) );
else
root->leftSubtree( local_insert( root->leftSubtree(), v ) );
return root;
}

the code for the insert function to the binary search tree.
I have two questions the first is what specifically is the underlined line above doing, i have never seen a pointer followed by the scope operator.
and Second is the function find missing any perameters, perhaps maybe a node in the tree, or a pointer to the node in the tree?
Last edited on
It is basically telling you are defining a function like this:
1
2
3
TreeNode* //return type
BinSearchTre::local_insert //class and name of function
(...) //parameters 
ok so the asterisck is accociated with the type that is being returned and not the actual class name?
also what about find sould it need to be passed anything else in order to recursively find one of the data values stored in the nodes of the tree?
Yes.

Try making a find() function that only takes an int that you want to find and see if you can find the value.
After working with the data structure i came up with this to find a value in the tree.

1
2
  bool find( int v );
  bool find( int v, TreeNode *root);


have been added to the header that is shown above, and I have defined those functions in my cpp as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool BinSearchTree :: find ( int v, TreeNode *rootSub)
{
   if (rootSub == NULL)
    return false;
   else if (rootSub->value() == v)
    return true;
  // cout << root->value();
   else if(v < rootSub->value())
    {
      return find(v, rootSub->leftSubtree());
    }
    else
        {
         return find(v, rootSub->rightSubtree());
        }
}

bool BinSearchTree :: find(int v)
{

  return find(v, root);

}


Is this a legitamate solution, making two versions of the find function, the one that passes the node is in the private section of the class, should overloaded functions be declared private? is that bad style? If so any hints on a better solution would be appreciated
Last edited on
Topic archived. No new replies allowed.