Binary Tree creation and recursive and non-recursive Traversal
# Define destdio. h
# Define demalloc. h
# Define destdlib. h
Typedef struct lNode
{
Char data;
Struct lNode * lchild;
Struct lNode * rchild;
} LNODE, * Tree;
Typedef struct Node
{
Tree data;
Struct Node * Next;
} NODE, * PNODE;
Typedef struct Stack
{
PNODE pBottom;
PNODE pTop;
} STACK, * PSTACK;
// Function declaration
PSTACK initStack (void); // create a stack
Void push (pstack s, Tree val); // press the stack
Void pop (pstack s); // output Stack
Bool stackEmpty (PSTACK pS );
Tree CreateBiTree (); // Tree Creation
Void pretraverse (Tree T); // first-order recursive Traversal
Void Intraverse (Tree T); // recursive traversal in the middle order
Void posttraverse (Tree T); // recursive post-order traversal
Void NoIntraverse (Tree T); // non-recursive traversal in the middle order
Int main ()
{
Tree T;
T = CreateBiTree ();
Pretraverse (T); printf ();
Intraverse (T); printf ();
Posttraverse (T); printf ();
NoIntraverse (T); printf ();
Return 0;
}
// Build
Tree CreateBiTree ()
{
Char ch;
Tree T;
Scanf (% c, & ch );
If (ch = '#') T = NULL;
Else {
T = (Tree) malloc (sizeof (LNODE ));
T-> data = ch;
T-> lchild = CreateBiTree ();
T-> rchild = CreateBiTree ();
}
Return T; // return the root node
}
// Recursive traversal in the First Order (traversal order: left and right of the root)
Void pretraverse (Tree T)
{
If (T! = NULL)
{
Printf (% c, T-> data); // output the root node data
Pretraverse (T-> lchild); // traverses the left subtree
Pretraverse (T-> rchild); // traverses the right subtree
}
}
// Recursive traversal in the middle order (traversal order: Left root and right)
Void Intraverse (Tree T)
{
If (T! = NULL)
{
Intraverse (T-> lchild); // traverses the left subtree
Printf (% c, T-> data); // output the root node data
Intraverse (T-> rchild); // traverses the right subtree
}
}
// Recursive post-order traversal (traversal order: Left and Right root)
Void posttraverse (Tree T)
{
If (T! = NULL)
{
Posttraverse (T-> lchild); // traverses the left subtree
Posttraverse (T-> rchild); // traverses the right subtree
Printf (% c, T-> data); // output the root node data
}
}
// Ordinal traversal (non-recursive traversal)
Void NoIntraverse (Tree T)
{
PSTACK s;
S = initStack ();
Tree p = T;
While (p! = NULL |! StackEmpty (s ))
{
While (p)
{
Push (s, p );
P = p-> lchild;
}
If (! StackEmpty (s ))
{
Pop (s );
P = p-> rchild;
}
}
}
PSTACK initStack (void)
{
PSTACK pS;
PS-> pTop = (PNODE) malloc (sizeof (NODE ));
If (pS-> pTop = NULL)
{
Printf (dynamic memory allocation failed !);
Exit (-1 );
}
Else
{
PS-> pBottom = pS-> pTop;
PS-> pBottom-> Next = NULL;
}
Return pS;
}
Void push (PSTACK pS, Tree val)
{
PNODE q;
Q = (PNODE) malloc (sizeof (NODE ));
If (q = NULL)
{
Printf (dynamic memory allocation failed !);
Exit (-1 );
}
Else
{
Q-> data = val;
Q-> Next = pS-> pTop;
PS-> pTop = q;
}
}
Void pop (PSTACK pS)
{
PNODE q;
If (pS-> pTop = NULL)
{
Printf (dynamic memory allocation failed !);
Exit (-1 );
}
Else
{
Q = pS-> pTop;
Printf (% c, q-> data );
PS-> pTop = q-> Next;
Free (q );
}
}
Bool stackEmpty (PSTACK pS)
{
If (pS-> pTop = pS-> pBottom)
{
Return true;
}
Return false;
}