What I want to do is to traverse the node in order, so that I can print the node in binary tree in order.
void inorder_traverse(node* root, function<void(node*)> visit)
{
if (root == nullptr) return;
cout << root->data << ", ";
inorder_traverse(root->left, visit);
visit(root);
inorder_traverse(root->right, visit);
}
I saw this code for inorder traversing a binary tree. Traversing goes all the nodes so I thought I could print all the data of all visited nodes using traversing function. Would it work? I am very confused what to pass for the polymorphic function parameter.
If I construct binary tree like the following and try to traverse and print all the data in tree, what should I pass to the function inorder_traverse above?
struct node* root = new node(NULL);
root->data = 10;
root = insertion(root, 1);
root = insertion(root, 11);
root = insertion(root, 2);
root = insertion(root, 12);
root = insertion(root, 3);
root = insertion(root, 13);
root = insertion(root, 5);
root = insertion(root, 20);
root = insertion(root, 7);
root = insertion(root, 15);
THanks, I would greatly appreciate it.