Binary Tree Code exercises

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.