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?

Couldn't you just call the mirror function from within the inorder function?

I can't... it says Illegal declaration.

Why Dont you embed the mirror function into the inorder fuction.

Or else you can try to create three functions ,

1)Inorder
2)Mirror
3)Inorderandmirror();

Basically i dont understand why you cannot call the mirror function through inorder. I think that it would be useful if you provide the code on how you tried to call the function.

Why Dont you embed the mirror function into the inorder fuction.

Or else you can try to create three functions ,

1)Inorder
2)Mirror
3)Inorderandmirror();

Basically i dont understand why you cannot call the mirror function through inorder. I think that it would be useful if you provide the code on how you tried to call the function.

I made this way... from BinaryTree.h

template<class NODETYPE>
void BinaryTree<NODETYPE>::inorderHelper(BinaryTreeNode<NODETYPE> *t) {

void BinaryTree<NODETYPE>::mirrorHelper(BinaryTreeNode<NODETYPE> *t) {


if (mirror=1&&t){

mirrorHelper(t->rChildPtr);
visit(t);   
        mirrorHelper(t->lChildPtr);

}
else{ 
        inorderHelper(t->lChildPtr);
visit(t);
        inorderHelper(t->rChildPtr);
    
}

}
}

but it results an error message...

it says

c:\documents and settings\aldrich uy\desktop\rbc\binarytree\binarytree\binarytree.h(100) : error C2601: 'BinaryTree<NODETYPE>::mirrorHelper' : local function definitions are illegal

here is my declaration from BinaryTree.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);
};
template<class NODETYPE>
void BinaryTree<NODETYPE>::inorder() {
    inorderHelper(rootPtr);
mirrorHelper(rootPtr);

}

I mean did you implement in this way ?

template<class NODETYPE>
void BinaryTree<NODETYPE>::inorder() {
    inorderHelper(rootPtr);
mirrorHelper(rootPtr);

}

I mean did you implement in this way ?

I did but it has errors:

LINK : C:\Documents and Settings\Aldrich Uy\Desktop\rbc\BinaryTree\Debug\BinaryTree.exe not found or not built by the last incremental link; performing full link
main.obj : error LNK2019: unresolved external symbol "public: void __thiscall BinaryTree<int>::mirror(void)" (?mirror@?$BinaryTree@H@@QAEXXZ) referenced in function _main
C:\Documents and Settings\Aldrich Uy\Desktop\rbc\BinaryTree\Debug\BinaryTree.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Documents and Settings\Aldrich Uy\Desktop\rbc\BinaryTree\BinaryTree\Debug\BuildLog.htm"
BinaryTree - 2 error(s), 0 warning(s)

template<class NODETYPE>
void BinaryTree<NODETYPE>::inorder() {
    inorderHelper(rootPtr);
mirrorHelper(rootPtr);

}

I mean did you implement in this way ?

pal thanks.... it runs!!!! I accidentally type it wrong thanks :)

for those who cannot understand Sky Diploma's last post reply... just add it in the header file of BinaryTree.h line number 63 and insert the info there and you may / may not delete the lines from line number 69 upto 72... cause you're just like declaring a waste of lines and codes :)

This article has been dead for over six months. Start a new discussion instead.