diff --git a/05-trees/BinaryTree.cpp b/05-trees/BinaryTree.cpp index bc5934c..3275a0f 100644 --- a/05-trees/BinaryTree.cpp +++ b/05-trees/BinaryTree.cpp @@ -18,61 +18,68 @@ BinaryTree::BinaryTree() { /**~*~* This function calls a recursive function to traverse the - tree in postorder (the wrapper function) + tree in postorder *~**/ -void BinaryTree::postOrder() const { - _postOrder(root); +void BinaryTree::postOrder(void visit(const Data &)) const { + _postOrder(root, visit); } /**~*~* Postorder Traversal of the Binary Tree: - Left-Right-Root (recursive) + Left-Right-Root *~**/ -void BinaryTree::_postOrder(BinaryTree::Node *root) const { +void BinaryTree::_postOrder(BinaryTree::Node *root, void visit(const Data &)) const { if (root == nullptr) return; - _postOrder(root->left); - _postOrder(root->right); - cout << root->data.num << " "; + _postOrder(root->left, visit); + _postOrder(root->right, visit); + visit(root->data); } /**~*~* This function calls a recursive function to traverse the - tree in preorder (the wrapper function) + tree in preorder *~**/ -void BinaryTree::preOrder() const { - _preOrder(root); +void BinaryTree::preOrder(void visit(const Data &)) const { + _preOrder(root, visit); } /**~*~* - Preorder Traversal of the Binary Tree: - Root-Left-Right (recursive) + Postorder Traversal of the Binary Tree: + Left-Right-Root *~**/ -void BinaryTree::_preOrder(BinaryTree::Node *root) const { +void BinaryTree::_preOrder(BinaryTree::Node *root, void visit(const Data &)) const { if (root == nullptr) return; - cout << root->data.num << " "; - _preOrder(root->left); - _preOrder(root->right); + visit(root->data); + _preOrder(root->left, visit); + _preOrder(root->right, visit); } /**~*~* This function calls a recursive function to traverse the - tree in inorder (the wrapper function) + tree in inorder *~**/ -void BinaryTree::inOrder() const { - _inOrder(root); +void BinaryTree::inOrder(void visit(const Data &)) const { + _inOrder(root, visit); } /**~*~* Inorder Traversal of the Binary Tree: - Left-Root-Right (recursive) + Left-Root-Right *~**/ -void BinaryTree::_inOrder(Node *root) const { +void BinaryTree::_inOrder(Node *root, void visit(const Data &)) const { if (root) { - _inOrder(root->left); - cout << root->data.num << " "; - _inOrder(root->right); + _inOrder(root->left, visit); + //cout << root->data.num << " "; + visit(root->data); + // cout has been replaced with a call for visit + // What is visit? + // visit is a generic name for a display function + // in main(), when inOrder is called, it is decided what function address to assign to visit + // here, you just use visit the way you would use/call a function + _inOrder(root->right, visit); + // ------------------^^^^^^ visit as an argument } } diff --git a/05-trees/BinaryTree.h b/05-trees/BinaryTree.h index 40ef39e..1d7520d 100644 --- a/05-trees/BinaryTree.h +++ b/05-trees/BinaryTree.h @@ -28,18 +28,18 @@ public: // Binary Tree operations void insert(Data dataIn); - void inOrder() const; + void inOrder(void visit(const Data &)) const; - void preOrder() const; + void preOrder(void visit(const Data &)) const; - void postOrder() const; + void postOrder(void visit(const Data &)) const; private: - void _inOrder(Node *root) const; + void _inOrder(Node *root, void visit(const Data &)) const; - void _preOrder(Node *root) const; + void _preOrder(Node *root, void visit(const Data &)) const; - void _postOrder(Node *root) const; + void _postOrder(Node *root, void visit(const Data &)) const; void _destroy(Node *root); }; diff --git a/05-trees/BinaryTree_Demo.cpp b/05-trees/BinaryTree_Demo.cpp index e02359e..75f0527 100644 --- a/05-trees/BinaryTree_Demo.cpp +++ b/05-trees/BinaryTree_Demo.cpp @@ -22,6 +22,9 @@ using namespace std; void build_BT(BinaryTree &tree, int n); +void hDisplay(const Data &item); // horizontal display: all items on one line +void vDisplay(const Data &item); // vertical display: one item per line + int main(void) { BinaryTree tree; int n; // number of nodes @@ -34,17 +37,21 @@ int main(void) { build_BT(tree, n); cout << " Inorder: "; - tree.inOrder(); + tree.inOrder(hDisplay); // hDisplay is the inOrder's argument cout << endl; if (option == 'T' || option == 't') { cout << "Postorder: "; - tree.postOrder(); + tree.postOrder(hDisplay); // passing hDisplay to postOrder + cout << endl; + tree.postOrder(vDisplay); // passing vDisplay to postOrder cout << endl; } if (option == 'E' || option == 'e') { cout << " Preorder: "; - tree.preOrder(); + tree.preOrder(hDisplay); // passing hDisplay to preOrder + cout << endl; + tree.preOrder(vDisplay); // passing vDisplay to preOrder cout << endl; } return 0; @@ -66,3 +73,18 @@ void build_BT(BinaryTree &tree, int n) { tree.insert(data); } } + +// The following two functions are used as arguments to other functions +/**~*~* + horizontal display: all items on one line +*/ +void hDisplay(const Data &item) { + cout << item.num << " "; +} + +/**~*~* + // vertical display: one item per line +*/ +void vDisplay(const Data &item) { + cout << item.num << endl; +}