开发者

How do I implement a display function for a binary search tree?

开发者 https://www.devze.com 2023-01-31 00:45 出处:网络
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?

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社区
0

精彩评论

暂无评论...
验证码 换一张
取 消