c++ help with implementing binary search tree functions

I have been given binary search tree code by my professor and I need to implement "int smallest_key()" and "int largest_key()" functions. I am not allowed to "brute force" by traversing the entire tree to find these keys.
binary_tree.h and binary_tree.cpp files:

https://gist.github.com/anonymous/0847f4ddb09d9a3e5ca893715636cce3

On line 257 of binary_tree.h is where I tried implementing my own smallest_key function, and declare it on line 39 in the public section of btree class (but should it be in private instead, and if so, why?)

In binary_tree.cpp I am trying to print out the result on line 26. This is obviously incorrect as there are no values being passed, but I'm curious as to what I actually type in here as parameters?

Thanks a lot in advance!
I am not allowed to "brute force" by traversing the entire tree to find these keys.


So presumably, the smallest would be found by always following the left child, and largest by always following the right child?

This is obviously incorrect as there are no values being passed, but I'm curious as to what I actually type in here as parameters?


Well, looking at the parameters, you need to pass the tree and 2 ints.

(but should it be in private instead, and if so, why?)


If it was private, it would have to be called by another member function, and it would no longer be part of the public interface.

On line 257 of binary_tree.h is where I tried implementing my own smallest_key function, and declare it on line 39 in the public section of btree class


I think you need to reorganise your files:

1. The file with the main function would be named main.cpp , I like to do this because when there are lots of files it's easy to find.
2. It's evil to put implementation code in header files. If they are included in multiple places .......
3. Put the class definition only into the header file, named directly after the class name.
4. Put the implementation (the function definitions) into the cpp file, named directly after the class name.

Some other things, avoid using NULL , use nullptr instead - it was invented to get around problems associated with using NULL and pointers.

Thoroughly pedantically, a btree is a different thing to a binary tree. It can have more than 2 children.
https://en.wikipedia.org/wiki/B-tree
thanks TheIdeasMan,
slogging away at it, it's getting there
Topic archived. No new replies allowed.