In order, the result is an ascending sequence,
Pre-order, same as input
#include <iostream>
using namespace Std;
struct Node//two fork tree structure
{
Node*lchild;
Node*rchild;
int C; Save numbers
} tree[110]; Static array
int Loc; Number of elements used in a static array
Node *creat ()//apply for unused nodes
{
Tree[loc].lchild = Tree[loc].rchild = NULL;
Return &Tree[loc++];
}
void Postorder (NODE*T)
{
if (t->lchild! = NULL)
{
Postorder (T->lchild);
}
if (t->rchild! = NULL)
{
Postorder (T->rchild);
}
cout << t->c<< "";
}
void Inorder (NODE*T)
{
if (t->lchild! = NULL)
{
Inorder (T->lchild);
}
cout << t->c << "";
if (t->rchild! = NULL)
{
Inorder (T->rchild);
}
}
void Preorder (NODE*T)
{
cout << t->c << "";
if (t->lchild! = NULL)
{
Preorder (T->lchild);
}
if (t->rchild! = NULL)
{
Preorder (T->rchild);
}
}
Node *insert (node*t, int x)
{
if (T = = NULL)
{
T = creat ();
T->c = x;
return T;
}
else if (x < t->c)
{
T->lchild = insert (t->lchild, x);
}
else if (x>t->c)
{
T->rchild = insert (t->rchild, x);
}
return T;
}
int main ()
{
int n;
while (CIN >> N)
{
loc = 0;
Node*t = NULL;
for (int i = 0; i < n; i++)
{
int x;
CIN >> X;
t = insert (t, x);
}
Preorder (T);
cout<< Endl;
Inorder (T);
cout << Endl;
Postorder (T);
cout << Endl;
}
}
Binary sort Tree