I am working on this binary search tree, and I am having trouble understanding/coding how I can use the iterator class my partner and I made. begin() is supposed to return an iterator to the first element in the list (the left-most element) and end() is supposed to go to the end-most + 1. Its the opposite with rbegin() and rend() where rbegin is the right-most element and rend is the left-most + 1.
I think I need to use recursion in some of these methods dealing with the iterator somewhere, or a loop, but I'm not exactly sure how. Can anyone help me?
Pre-order
Display the data part of root element (or current element).
Traverse the left subtree by recursively calling the pre-order function.
Traverse the right subtree by recursively calling the pre-order function.
In-order (symmetric)
Traverse the left subtree by recursively calling the in-order function.
Display the data part of root element (or current element).
Traverse the right subtree by recursively calling the in-order function.
Post-order
Traverse the left subtree by recursively calling the post-order function.
Traverse the right subtree by recursively calling the post-order function.
Display the data part of root element (or current element).
This basically tells you 100% how to do your recursion too.
Hint: The 3 traversal function bodies will each be 3 lines long. Well..actually they may be a few lines more if you check to see if they are valid so probably about 5 lines long.
A BST tree could have a typical iterator ( one with ++ and -- ) if each node contained a pointer to its parent. Otherwise a "for_each" function on a BST would take a functor as an argument and recurse into the tree, calling the functor for each node's value.