#include<iostream>
#include<stack>
#include<queue>
using namespace std;
template<class Elem>
struct BinNode
{
Elem data;
BinNode <Elem>* left;
BinNode <Elem>* right;
int h;
BinNode(Elem x) {
data = x;
h = -1;
left = right = NULL;
}
};
template<class Elem>
class BinTree {
protected:
BinNode<Elem>* root;
void rpreprint(BinNode<Elem>* r) {
if (r == NULL) return;
cout << r->data << " ";
rpreprint(r->left);
rpreprint(r->right);
}
BinNode<Elem>* rfindx(Elem x, BinNode<Elem>* r) {
if (!r) return NULL;
if (r->data == x) return r;
BinNode<Elem>* found;
found = rfindx(x, r->left);
return found ? found : rfindx(x, r->right);
}
void rprint(BinNode<Elem>* r, int depth) {
for (int i = 0; i < depth; i++) {
cout << " ";
}
if (!r) {
cout << "[/]" << endl;
}
else
{
cout << r->data << endl;
rprint(r->left, depth + 1);
rprint(r->right, depth + 1);
}
}
void rinprint(BinNode<Elem>* r) {
if (r == NULL) return;
rpreprint(r->left);
cout << r->data << " ";
rpreprint(r->right);
}
void rbackprint(BinNode<Elem>* r) {
if (r == NULL) return;
rpreprint(r->left);
rpreprint(r->right);
cout << r->data << " ";
}
void ipreprint(BinNode<Elem>* r) {
stack<BinNode<Elem>*> st;
if (!r) return;
while (r)
{
cout << r->data << ' ';
st.push(r);
r = r->left;
while (r == NULL && !st.empty()) {
r = st.top();
st.pop();
r = r->right;
}
}
}