filename: main.cpp
#include <iostream>
using namespace std;
int main();
using namespace std;
#include "BinaryTree.h"
int main()
{
BinaryTree<int> tree[8];
tree[1].makeTree(1, tree[0], tree[0]);
tree[3].makeTree(3, tree[0], tree[0]);
tree[2].makeTree(2, tree[1], tree[3]);
tree[5].makeTree(5, tree[0], tree[0]);
tree[4].makeTree(4, tree[2], tree[5]);
tree[7].makeTree(7, tree[0], tree[0]);
tree[6].makeTree(6, tree[4], tree[7]);
cout << "Preorder: ";
tree[6].preorder();
cout << "\n";
cout << "Inorder: ";
tree[6].inorder();
cout << "\n";
cout << "Postorder: ";
tree[6].postorder();
cout << "\n";
cout<<"After Mirror: ";
tree[6].mirror();
cout<<"\n";
return 0;
}
filename:BinaryTree.h
#ifndef BINARYTREE_H_INCLUDED
#define BINARYTREE_H_INCLUDED
#include <cassert>
#include "BinaryTreeNode.h"
template<class NODETYPE>
class BinaryTree {
public:
BinaryTree();
bool isEmpty();
void makeTree(const NODETYPE &, BinaryTree<NODETYPE> &, BinaryTree<NODETYPE> &);
void preorder();
void inorder();
void postorder();
void mirror();
private:
BinaryTreeNode<NODETYPE> *rootPtr;
BinaryTreeNode<NODETYPE>* getNewNode(const NODETYPE &info);
void visit(BinaryTreeNode<NODETYPE> *t);
void preorderHelper(BinaryTreeNode<NODETYPE> *t);
void inorderHelper(BinaryTreeNode<NODETYPE> *t);
void postorderHelper(BinaryTreeNode<NODETYPE> *t);
void mirrorHelper(BinaryTreeNode<NODETYPE> *t);
};
// Default constructor
template<class NODETYPE>
BinaryTree<NODETYPE>::BinaryTree() {
rootPtr = NULL;
}
// Checks if the binary tree is empty
template<class NODETYPE>
bool BinaryTree<NODETYPE>::isEmpty() {
return rootPtr ? false : true;
}
// Create a binary tree node with element and make the left and
// right as its left and right child, respectively
template<class NODETYPE>
void BinaryTree<NODETYPE>::makeTree(const NODETYPE &info,
BinaryTree<NODETYPE>& leftSubtree, BinaryTree<NODETYPE>& rightSubtree) {
rootPtr = getNewNode(info);
rootPtr->lChildPtr = leftSubtree.rootPtr;
rootPtr->rChildPtr = rightSubtree.rootPtr;
leftSubtree.rootPtr = NULL;
rightSubtree.rootPtr = NULL;
}
// Preorder traversal on the tree
// (calls the preorderHelper function to perform the
// main preorder traversal)
template<class NODETYPE>
void BinaryTree<NODETYPE>::preorder() {
preorderHelper(rootPtr);
}
template<class NODETYPE>
void BinaryTree<NODETYPE>::inorder() {
inorderHelper(rootPtr);
}
template<class NODETYPE>
void BinaryTree<NODETYPE>::postorder() {
postorderHelper(rootPtr);
}
template<class NODETYPE>
void BinaryTree<NODETYPE>::mirror() {
mirrorHelper(rootPtr);
}
// return a pointer to a newly allocated node
template<class NODETYPE>
BinaryTreeNode<NODETYPE> *BinaryTree<NODETYPE>::getNewNode(const NODETYPE &value) {
BinaryTreeNode<NODETYPE> *ptr = new BinaryTreeNode<NODETYPE>(value);
assert(ptr != NULL);
return ptr;
} // end getNewNode
// Perform a "visit" to the binary tree node
// In this program, simply display the data in the node
template<class NODETYPE>
void BinaryTree<NODETYPE>::visit(BinaryTreeNode<NODETYPE> *t) {
cout << t->data << " ";
}
// Main preorder traversal function
template<class NODETYPE>
void BinaryTree<NODETYPE>::preorderHelper(BinaryTreeNode<NODETYPE> *t) {
if (t) {
visit(t);
preorderHelper(t->lChildPtr);
preorderHelper(t->rChildPtr);
}
}
template<class NODETYPE>
void BinaryTree<NODETYPE>::inorderHelper(BinaryTreeNode<NODETYPE> *t) {
if (t) {
inorderHelper(t->lChildPtr);
visit(t);
inorderHelper(t->rChildPtr);
}
}
template<class NODETYPE>
void BinaryTree<NODETYPE>::postorderHelper(BinaryTreeNode<NODETYPE> *t) {
if (t) {
postorderHelper(t->lChildPtr);
postorderHelper(t->rChildPtr);
visit(t);
}
}
template<class NODETYPE>
void BinaryTree<NODETYPE>::mirrorHelper(BinaryTreeNode<NODETYPE> *t) {
if (t) {
mirrorHelper(t->rChildPtr);
visit(t);
mirrorHelper(t->lChildPtr);
}
}
#endif // BINARYTREE_H_INCLUDED
here is my solution that I presented it to my teacher... although the output is right... my teacher told me... NOT to COUT the Mirror function... what my teacher wants is when my inorder function was called there will be AUTOMATICALLY solving the Mirror function at the back and then it posts the results... like solving both at the same time... and my teacher does not want to see calling mirror function at the main.cpp... can anyone help me or suggest what to do?