#include <iostream>
#include <fstream>
#include <queue>
// @copyright by beautifulsoul84hung
using namespace std;
typedef class node* Node;
class node
{
private:
int data;
node *left;
node *right;
public:
node (int data, Node left, Node right)
{
this->data = data;
this->left = left;
this->right = right;
}
void setData (int data)
{
this->data = data;
}
int getData () const
{
return data;
}
void setLeft (Node left)
{
this->left = left;
}
Node getLeft () const
{
return left;
}
void setRight (Node right)
{
this->right = right;
}
Node getRight () const
{
return right;
}
void print ()
{
cout << getData
() << " "; }
friend class binaryTreeADT;
};
typedef class binaryTreeADT* BinaryTreeADT;
class binaryTreeADT
{
private:
Node root;
public:
void setRoot (Node root)
{
this->root = root;
}
Node getRoot ()const
{
return root;
}
binaryTreeADT ()
{
root = NULL;
}
binaryTreeADT (Node root)
{
this->root = root;
}
void creatTree (const char* filename)
{
fstream _readFile;
_readFile.open (filename, ios::in);
int data;
Node tmp;
queue <Node> q;
_readFile >> data;
tmp = new node (data, NULL, NULL);
root = tmp;
q.push (tmp);
while (!_readFile.eof ())
{
_readFile >> data;
tmp = new node (data, NULL, NULL);
Node n = q.front ();
q.pop ();
//n->print ();
n->setLeft(tmp);
q.push (tmp);
if (!_readFile.eof ())
{
_readFile >> data;
tmp = new node (data, NULL, NULL);
n->setRight(tmp);
//n->print ();
q.push (tmp);
}
}
}
void printPre ()
{
if (getRoot() != NULL)
{
getRoot()->print ();
BinaryTreeADT tmp = new binaryTreeADT (getRoot()->getLeft());
tmp->printPre ();
tmp = new binaryTreeADT (getRoot()->getRight());
tmp->printPre ();
}
}
void printPost ()
{
if (getRoot() != NULL)
{
BinaryTreeADT tmp = new binaryTreeADT (getRoot()->getLeft());
tmp->printPost ();
tmp = new binaryTreeADT (getRoot()->getRight());
tmp->printPost ();
getRoot()->print ();
}
}
bool hasLeft ()
{
if (getRoot()->getLeft()->getRight()!= NULL|| getRoot()->getLeft()->getLeft()!= NULL)
return true;
else
false;
}
bool hasRight ()
{
if (getRoot()->getRight()->getRight() != NULL|| getRoot()->getRight()->getLeft()!= NULL)
return true;
else
false;
}
BinaryTreeADT Left ()
{
if (hasLeft() == true)
{
BinaryTreeADT tree = new binaryTreeADT (getRoot()->getLeft());
return tree;
}
else return NULL;
}
BinaryTreeADT Right ()
{
if (hasRight() == true)
{
BinaryTreeADT tree = new binaryTreeADT (getRoot()->getRight());
return tree;
}
else return NULL;
}
void TraversalLevel ()
{
if (getRoot() != NULL)
{
queue <Node> q;
Node tmp = getRoot();
q.push (tmp);
while (1)
{
if (q.empty ())
return;
tmp = q.front ();
tmp->print ();
q.pop ();
if (tmp->getLeft() != NULL)
q.push (tmp->getLeft());
if (tmp->getRight() != NULL)
q.push (tmp->getRight());
}
}
}
};
int main ()
{
BinaryTreeADT Tree = new binaryTreeADT ();
Tree->creatTree("hung.txt");
Tree->printPre ();
cout << endl
<< "-*-"<< endl
; Tree->printPost ();
cout << endl
<< endl
<< "---" << endl
; BinaryTreeADT newTree = Tree->Left ();
newTree->printPost ();
newTree->printPre();
cout << endl
<< "***" << endl
; newTree->TraversalLevel ();
return 0;
}