#include
using namespace std;
=====================================================================
struct Node
{
int x;
Node * left;
Node * right;
Node(){left = NULL; right = NULL;}
Node(int d, Node* l = 0, Node* r = 0)
{
x = d;
left = l;
right = r;
}
};
=====================================================================
class Tree
{
private:
Node * root;
public:
XXXXXXXXXXTree()
{
XXXXXXXXXXroot = NULL;
}
-----------------------------------------------------------------
void insert(int x)
{
XXXXXXXXXXNode * ptr = new Node(x);
XXXXXXXXXXcout
"\n-------------------------------------"
endl;
XXXXXXXXXXcout
"Node Address: "
ptr
endl;
XXXXXXXXXXif (root == NULL)
XXXXXXXXXXroot = ptr;
XXXXXXXXXXelse
{
XXXXXXXXXXNode * temp = root;
XXXXXXXXXXwhile (true)
{
XXXXXXXXXXif (temp -> x > x)
{
XXXXXXXXXXif (temp -> left == NULL) {
XXXXXXXXXXcout
"Node Parent Address: "
temp
endl;
XXXXXXXXXXcout
"Node Parent Value: "
temp -> x
endl;
XXXXXXXXXXtemp -> left = ptr;
eak;
} else
XXXXXXXXXXtemp = temp -> left;
} else
{
XXXXXXXXXXif (temp -> right == NULL)
{
XXXXXXXXXXcout
"Node Parent Address: "
temp
endl;
XXXXXXXXXXcout
"Node Parent Value: "
temp -> x
endl;
XXXXXXXXXXtemp -> right = ptr;
eak;
} else
XXXXXXXXXXtemp = temp -> right;
}
}
}
XXXXXXXXXXcout
"Root: "
root
endl;
XXXXXXXXXXcout
"-------------------------------------"
endl;
}
-----------------------------------------------------------------
public: void inOrder(){ inOrder(root); }
private: void inOrder(Node * temp) {
XXXXXXXXXXif (temp != NULL)
{
XXXXXXXXXXinOrder(temp -> left);
XXXXXXXXXXcout
temp -> x
" ";
XXXXXXXXXXinOrder(temp -> right);
}
}
-----------------------------------------------------------------
public: void preOrder(){ inOrder(root); }
private: void preOrder(Node * temp)
{
XXXXXXXXXXif (temp != NULL) {
XXXXXXXXXXcout
temp -> x
" ";
XXXXXXXXXXpreOrder(temp -> left);
XXXXXXXXXXpreOrder(temp -> right);
}
}
-----------------------------------------------------------------
public: void postOrder(){ inOrder(root); }
private: void postOrder(Node * temp)
{
XXXXXXXXXXif (temp != NULL) {
XXXXXXXXXXpostOrder(temp -> left);
XXXXXXXXXXpostOrder(temp -> right);
XXXXXXXXXXcout
temp -> x
" ";
}
}
-----------------------------------------------------------------
public: void serial(){ serial(root); }
private: void serial(Node *n)
{
cout
'(';
if (n != 0)
{
cout
n->x
' ';
serial(n->left); cout
' ';
serial(n-
ight);
}
cout
')';
}
-----------------------------------------------------------------
public: Node * getRoot() {
XXXXXXXXXXreturn root;
}
-----------------------------------------------------------------
public: void display(){ display(root); }
private: void display(Node * temp)
{
XXXXXXXXXXif (temp != NULL) {
XXXXXXXXXXcout
endl;
XXXXXXXXXXcout
"Parent: "
temp -> x
endl;
XXXXXXXXXXcout
" Left Child of "
temp -> x
": ";
XXXXXXXXXXif (temp -> left == NULL)
XXXXXXXXXXcout
"NULL"
endl;
XXXXXXXXXXelse
XXXXXXXXXXcout
temp -> left -> x
endl;
XXXXXXXXXXcout
" Right Child of "
temp -> x
": ";
XXXXXXXXXXif (temp -> right == NULL)
XXXXXXXXXXcout
"NULL"
endl;
XXXXXXXXXXelse
XXXXXXXXXXcout
temp -> right -> x
endl;
XXXXXXXXXXdisplay(temp -> left);
XXXXXXXXXXdisplay(temp -> right);
}
}
-----------------------------------------------------------------
bool isEmpty()
{
XXXXXXXXXXif (root == NULL)
XXXXXXXXXXreturn true;
XXXXXXXXXXreturn false;
}
public:
-----------------------------------------------------------------
void removeAll(int value)
{
XXXXXXXXXXwhile (true)
{
XXXXXXXXXXNode * temp = root, * ptemp = root;
XXXXXXXXXXwhile (temp != NULL && temp -> x != value)
{
XXXXXXXXXXptemp = temp;
XXXXXXXXXXif (temp -> x < value)
XXXXXXXXXXtemp = temp -> right;
XXXXXXXXXXelse if (temp -> x > value)
XXXXXXXXXXtemp = temp -> left;
}
XXXXXXXXXXif (temp == NULL)
{