8.4 Lab: BT - Traversals (in-, post-, and pre-Order) - Version 2
This commit is contained in:
parent
6f5d2f2d5e
commit
41c45e09fb
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
};
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user