How do I implement a display function for a binary search tree of strings if display is passed as a function pointer to function inorder?
typedef void (*FunctionType)(TreeItemType& anItem);
void display(TreeItemType& anItem);
void BinarySearchTree::inorder(TreeNode *treePtr,
FunctionType visit)
{ if (treePtr != NULL)
{ inorder(treePtr->leftChildPtr, visit);
visit(treePtr->item);
inorder(treePtr->rightChildPtr, visit);
} // end if
} // end inorder
I've tried to write something like:
cout << anItem;
in the body of display but it doesn't work. TreeItemType is a string so do I need to overload the << operator or = operator to convert from TreeItemType to string? I have researched function pointers and can't figure out how to use:
visit(treePtr->item);
to display the tree's item (which is a string).
int main()
{
BinarySearchTree tree;
TreeItemType item = "20";
tree.searchTreeInsert(item);
tree.inorderTraverse(display); // call to inorderTraverse calls inorder
return 0; // indicates successful completion
} // end function main
void display(TreeItemType& anItem)
{
cout << anItem;
}
Definition of TreeItemType:
typedef string TreeItemType;
开发者_StackOverflow社区
精彩评论