View plaincopy to clipboardprint? # Include <stdio. h>
# Include <stdlib. h>
# Include <assert. h>
# Define STACK_SIZE 100
Typedef struct TREE
{
Char data;
Struct TREE * left;
Struct TREE * right;
} * Tree, Node;
Typedef struct STACK
{
Node node [STACK_SIZE];
Int top;
} Stack;
Void init_stack (Stack * stack)
{
Stack-> top = 0;
}
Int is_empty (Stack * stack)
{
Return stack-> top = 0;
}
Int push (Stack * stack, Node * node)
{
If (stack-> top = STACK_SIZE)
Return 0;
Stack-> node [stack-> top ++] = * node;
Return 1;
}
Int top (Stack * stack, Node * node)
{
If (stack-> top = 0)
Return 0;
* Node = stack-> node [stack-> top-1];
Return 1;
}
Int pop (Stack * stack, Node * node)
{
If (stack-> top = 0)
Return 0;
* Node = stack-> node [-- stack-> top];
Return 1;
}
Void creat_tree (Tree * tree)
{
Char data [1];
Scanf ("% s", data );
// Char data;
// Scanf ("% c", data); // enter the return key! Why?
If (data [0] = 'A ')
{
* Tree = NULL;
}
Else
{
* Tree = malloc (sizeof (Node ));
If (* tree = NULL)
{
Exit (0 );
}
(* Tree)-> data = data [0];
Creat_tree (& (* tree)-> left ));
Creat_tree (& (* tree)-> right ));
}
Return;
}
Void pre_order (Tree tree)
{
If (tree = NULL)
Return;
Printf ("% c", tree-> data );
Pre_order (tree-> left );
Pre_order (tree-> right );
}
Void in_order (Tree tree)
{
If (tree = NULL)
Return;
In_order (tree-> left );
Printf ("% c", tree-> data );
In_order (tree-> right );
}
Void post_order (Tree tree)
{
If (tree = NULL)
Return;
Post_order (tree-> left );
Post_order (tree-> right );
Printf ("% c", tree-> data );
}
Void in_order_stack (Tree tree)
{
If (tree = NULL)
Return;
Stack stack;
Init_stack (& stack );
Node * node = malloc (sizeof (Node ));
While (! Is_empty (& stack) | tree)
{
If (tree! = NULL)
{
Push (& stack, tree );
Tree = tree-> left;
}
Else
{
Pop (& stack, node );
Printf ("% c", node-> data );
If (node-> right! = NULL)
Tree = node-> right;
}
}
Printf ("\ n ");
}
Int tree_heigh (Tree tree)
{
If (tree = NULL)
Return 0;
Else
{
Int left = tree_heigh (tree-> left );
Int right = tree_heigh (tree-> right );
Return left> right? Left + 1: right + 1;
}
}
// 1 2 3 a 4
// 1
// 2 3
// A 4
//
Int main ()
{
Tree tree = NULL;
Creat_tree (& tree );
Printf ("pre: \ n ");
Pre_order (tree );
Printf ("\ nin: \ n ");
In_order (tree );
Printf ("\ npost: \ n ");
Post_order (tree );
Printf ("\ n ");
Printf ("Tree heigh is % d \ n", tree_heigh (tree ));
In_order_stack (tree );
Return 0;
}
# Include <stdio. h>
# Include <stdlib. h>
# Include <assert. h>
# Define STACK_SIZE 100
Typedef struct TREE
{
Char data;
Struct TREE * left;
Struct TREE * right;
} * Tree, Node;
Typedef struct STACK
{
Node node [STACK_SIZE];
Int top;
} Stack;
Void init_stack (Stack * stack)
{
Stack-> top = 0;
}
Int is_empty (Stack * stack)
{
Return stack-> top = 0;
}
Int push (Stack * stack, Node * node)
{
If (stack-> top = STACK_SIZE)
Return 0;
Stack-> node [stack-> top ++] = * node;
Return 1;
}
Int top (Stack * stack, Node * node)
{
If (stack-> top = 0)
Return 0;
* Node = stack-> node [stack-> top-1];
Return 1;
}
Int pop (Stack * stack, Node * node)
{
If (stack-> top = 0)
Return 0;
* Node = stack-> node [-- stack-> top];
Return 1;
}
Void creat_tree (Tree * tree)
{
Char data [1];
Scanf ("% s", data );
// Char data;
// Scanf ("% c", data); // enter the return key! Why?
If (data [0] = 'A ')
{
* Tree = NULL;
}
Else
{
* Tree = malloc (sizeof (Node ));
If (* tree = NULL)
{
Exit (0 );
}
(* Tree)-> data = data [0];
Creat_tree (& (* tree)-> left ));
Creat_tree (& (* tree)-> right ));
}
Return;
}
Void pre_order (Tree tree)
{
If (tree = NULL)
Return;
Printf ("% c", tree-> data );
Pre_order (tree-> left );
Pre_order (tree-> right );
}
Void in_order (Tree tree)
{
If (tree = NULL)
Return;
In_order (tree-> left );
Printf ("% c", tree-> data );
In_order (tree-> right );
}
Void post_order (Tree tree)
{
If (tree = NULL)
Return;
Post_order (tree-> left );
Post_order (tree-> right );
Printf ("% c", tree-> data );
}
Void in_order_stack (Tree tree)
{
If (tree = NULL)
Return;
Stack stack;
Init_stack (& stack );
Node * node = malloc (sizeof (Node ));
While (! Is_empty (& stack) | tree)
{
If (tree! = NULL)
{
Push (& stack, tree );
Tree = tree-> left;
}
Else
{
Pop (& stack, node );
Printf ("% c", node-> data );
If (node-> right! = NULL)
Tree = node-> right;
}
}
Printf ("\ n ");
}
Int tree_heigh (Tree tree)
{
If (tree = NULL)
Return 0;
Else
{
Int left = tree_heigh (tree-> left );
Int right = tree_heigh (tree-> right );
Return left> right? Left + 1: right + 1;
}
}
// 1 2 3 a 4
// 1
// 2 3
// A 4
//
Int main ()
{
Tree tree = NULL;
Creat_tree (& tree );
Printf ("pre: \ n ");
Pre_order (tree );
Printf ("\ nin: \ n ");
In_order (tree );
Printf ("\ npost: \ n ");
Post_order (tree );
Printf ("\ n ");
Printf ("Tree heigh is % d \ n", tree_heigh (tree ));
In_order_stack (tree );
Return 0;
}
Author's "kangquan2008 column"