Due to job practice requirements, the basic operations of a tree are written, including first, middle, back-order recursion and non-recursion, calculation height, calculation of the number of left subtree, no other use, alert yourself ..
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <stack>
Using namespace std;
// Tree node
Typedef struct TreeNode {
Char data;
Struct TreeNode * lChild;
Struct TreeNode * rChild;
} Node;
Int n;
Stack <Node *> s;
// Create tree in the first order
Void createTree (Node ** t, char * c)
{
N ++;
If (n> strlen (c)-1) return;
If (c [n] = '0') * t = NULL;
Else
{
(* T) = (Node *) malloc (sizeof (Node ));
(* T)-> data = c [n];
CreateTree (& (* t)-> lChild, c );
CreateTree (& (* t)-> rChild, c );
}
}
// First-order recursive access
Void preVisit (Node * t)
{
If (t)
{
Printf ("% c", t-> data );
PreVisit (t-> lChild );
PreVisit (t-> rChild );
}
}
// Recursive sequential access
Void midVisit (Node * t)
{
If (t)
{
MidVisit (t-> lChild );
Printf ("% c", t-> data );
MidVisit (t-> rChild );
}
}
// Recursive post-Order Access
Void lastVisit (Node * t)
{
If (t)
{
LastVisit (t-> lChild );
LastVisit (t-> rChild );
Printf ("% c", t-> data );
}
}
// Non-recursive first-order access
Void uPre (Node * head)
{
While (! S. empty () s. pop ();
While (head |! S. empty ())
{
While (head)
{
Printf ("% c", head-> data );
S. push (head );
Head = head-> lChild;
}
If (! S. empty ())
{
Head = s. top ();
S. pop ();
Head = head-> rChild;
}
}
}
// Non-recursive sequential access
Void uMid (Node * head)
{
While (! S. empty () s. pop ();
While (head |! S. empty ())
{
While (head)
{
S. push (head );
Head = head-> lChild;
}
If (! S. empty ())
{
Head = s. top ();
Printf ("% c", head-> data );
S. pop ();
Head = head-> rChild;
}
}
}
// Non-recursive post-Order Access
Void uLast (Node * head)
{
Node * p = head;
Head = NULL;
While (! S. empty () s. pop ();
Do
{
While (p)
{
S. push (p );
P = p-> lChild;
}
P = s. top ();
P = p-> rChild;
If (head = p | p = NULL)
{
Head = s. top ();
S. pop ();
Printf ("% c", head-> data );
P = NULL;
}
} While (! S. empty ());
}
// Computing height
Int calH (Node * head)
{
If (! Head) return 0;
Int left = calH (head-> lChild );
Int right = calH (head-> rChild );
Int h;
If (left> right ){
H = left + 1;
}
Else
{
H = right + 1;
}
Return h;
}
// Calculate the number of left sub-Leaves
Int calLeftChild (Node * t)
{
If (! T) return 0;
Else if (t-> lChild &&! T-> lChild &&! T-> lChild-> rChild) return 1;
Else return calLeftChild (t-> lChild) + calLeftChild (t-> rChild );
}
Int main ()
{
Char a [1000];
Int c;
Scanf ("% d", & c );
While (c --)
{
N =-1;
Scanf ("% s", );
Node * head;
CreateTree (& head, );
PreVisit (head );
Putchar ('\ n ');
MidVisit (head );
Putchar ('\ n ');
LastVisit (head );
Putchar ('\ n ');
Printf ("non-recursive First Order \ n ");
UPre (head );
Putchar ('\ n ');
Printf ("\ n non-recursive Middle Order \ n ");
UMid (head );
Putchar ('\ n ');
Printf ("\ n non-recursive post-order \ n ");
ULast (head );
Putchar ('\ n ');
Printf ("\ n % d \ n", calH (head ));
Printf ("% d \ n", calLeftChild (head ));
}
Return 0;
}