Binary Tree _ C language implementation (I)

Source: Internet
Author: User
Original article: http://www.longtengwang.com/Article/soft/C/sfa/200702/5229.html

# Include <stdio. h>

# Include <stdlib. h>

# Define stack_max_size 30

# Define queue_max_size 30

# Ifndef elemtype

Typedef char elemtype;

# Endif
/*************************************** *********************************/
/* The following are 11 simple algorithms for Binary Tree operations */
/*************************************** *********************************/

Struct btreenode {

Elemtype data;

Struct btreenode * left;

Struct btreenode * right;

};

/* 1. initialize the binary tree */

Void initbtree (struct btreenode ** BT)

{

* Bt = NULL;

Return;

}

/* 2. Create a binary tree (based on the binary tree generalized table string pointed to by )*/

Void createbtree (struct btreenode ** BT, char *)

{

Struct btreenode * P;

Struct btreenode * s [stack_max_size];/* defines the S array for the stack used to store the root node pointer */

Int Top =-1;/* defines top as the top pointer of S stack. The initial value is-1, indicating empty stack */

Int K;/* use K as the left and right subtree of the processing node, k = 1 to process the left subtree, K = 2 to process the right subtree */

Int I = 0;/* use I to scan the binary tree generalized table string stored in array A. The initial value is 0 */

* Bt = NULL;/* set the tree root pointer to null, that is, create a binary tree from the empty tree */
/* One character is processed every cycle until the string Terminator \ 0 is scanned */

While (A [I]! = '\ 0 '){

Switch (A [I]) {

Case '':

Break;/* No space Processing */

Case '(':

If (Top = stack_max_size-1 ){

Printf ("the stack space is too small! \ N ");

Exit (1 );

}

Top ++;

S [Top] = P;

K = 1;

Break;

Case ')':

If (Top =-1 ){

Printf ("binary tree generalized table string error! \ N ");

Exit (1 );

}

Top --;

Break;

Case ',':

K = 2;

Break;

Default:

P = malloc (sizeof (struct btreenode ));

P-> DATA = A [I];

P-> left = p-> right = NULL;

If (* bt = NULL ){

* Bt = P;

} Else {

If (k = 1 ){

S [Top]-> left = P;

} Else {

S [Top]-> right = P;

}

}

}

I ++;/* modify the I value for the next character to be scanned */

}

Return;

}

/* 3. Check whether the binary tree is empty. If it is null, 1 is returned. Otherwise, 0 */is returned */

Int emptybtree (struct btreenode * BT)

{

If (bt = NULL ){

Return 1;

} Else {

Return 0;

}

}

/* 4. Determine the binary tree depth */

Int btreedepth (struct btreenode * BT)

{

If (bt = NULL ){

Return 0;/* for an empty tree, return 0 to end recursion */

} Else {

Int dep1 = btreedepth (BT-> left);/* calculate the depth of the Left subtree */

Int dep2 = btreedepth (BT-> right);/* calculate the depth of the right subtree */

If (dep1> dep2 ){

Return dep1 + 1;

} Else {

Return dep2 + 1;

}

}

}

/* 5. Search for the node with the value of X from the binary tree. If yes, the storage location of the element is returned. Otherwise, a null value is returned */

Elemtype * findbtree (struct btreenode * BT, elemtype X)

{

If (bt = NULL ){

Return NULL;

} Else {

If (BT-> DATA = x ){

Return & (BT-> data );

} Else {/* Recursively search for left and right subtree respectively */

Elemtype * P;

If (P = findbtree (BT-> left, x )){

Return P;

}

If (P = findbtree (BT-> right, x )){

Return P;

}

Return NULL;

}

}

}

/* 6. output binary tree (Forward traversal )*/

Void printbtree (struct btreenode * BT)

{
/* End recursion when the tree is empty. Otherwise, perform the following operations */

If (BT! = NULL ){

Printf ("% C", BT-> data);/* output the value of the root node */

If (BT-> left! = NULL | Bt-> right! = NULL ){

Printf ("(");

Printbtree (BT-> left );

If (BT-> right! = NULL ){

Printf (",");

}

Printbtree (BT-> right );

Printf (")");

}

}

Return;

}

/* 7. Clear the binary tree to make it an empty tree */

Void clearbtree (struct btreenode ** BT)

{

If (* BT! = NULL ){

Clearbtree (& (* BT)-> left ));

Clearbtree (& (* BT)-> right ));

Free (* BT );

* Bt = NULL;

}

Return;

}

/* 8. Forward traversal */

Void preorder (struct btreenode * BT)

{

If (BT! = NULL ){

Printf ("% C", BT-> data);/* access the root node */

Preorder (BT-> left);/* traverse the left subtree in ascending order */

Preorder (BT-> right);/* traverse the right subtree in ascending order */

}

Return;

}


/* 9. Forward traversal */

Void inorder (struct btreenode * BT)

{

If (BT! = NULL ){

Inorder (BT-> left);/* traverse the left subtree In The Middle Order */

Printf ("% C", BT-> data);/* access the root node */

Inorder (BT-> right);/* traverse the right subtree in ascending order */

}

Return;

}

/* 10. Post-order traversal */

Void postorder (struct btreenode * BT)

{

If (BT! = NULL ){

Postorder (BT-> left);/* traverse the left subtree in descending order */

Postorder (BT-> right);/* traverse the right subtree in descending order */

Printf ("% C", BT-> data);/* access the root node */

}

Return;

}

/* 11. Traverse by layer */

Void levelorder (struct btreenode * BT)

{

Struct btreenode * P;

Struct btreenode * Q [queue_max_size];

Int front = 0, rear = 0;
/* Put the root pointer into the queue */

If (BT! = NULL ){

Rear = (Rear + 1) % queue_max_size;

Q [rear] = Bt;

}

While (front! = Rear) {/* the queue is not empty */

Front = (front + 1) % queue_max_size;/* place the first pointer to the first element */

P = Q [Front];

Printf ("% C", p-> data );
/* If the node has a left child, the left child node pointer enters the queue */

If (p-> left! = NULL ){

Rear = (Rear + 1) % queue_max_size;

Q [rear] = p-> left;

}
/* If the node has the right Child, the right child node pointer enters the queue */

If (p-> right! = NULL ){

Rear = (Rear + 1) % queue_max_size;

Q [rear] = p-> right;

}

}

Return;

}

/*************************************** *********************************/

Int main (INT argc, char * argv [])

{

Struct btreenode * Bt;/* pointer to the root node of a binary tree */

Char * B;/* string used to store the binary tree generalized table */

Elemtype X, * PX;

Initbtree (& BT );

Printf ("string of the generalized table of the input binary tree: \ n ");

/* Scanf ("% s", B );*/

B = "A (B (C), D (E (f, g), H (, I )))";

Createbtree (& BT, B );

If (BT! = NULL)

Printf ("% C", BT-> data );

Printf ("output in the form of generalized tables: \ n ");

Printbtree (BT);/* Outputs a binary tree in the form of a generalized table */

Printf ("\ n ");

Printf ("Preface:");/* Forward traversal */

Preorder (BT );

Printf ("\ n ");

Printf ("Middle Order:");/* middle order traversal */

Inorder (BT );

Printf ("\ n ");

Printf ("Suffix:");/* suffix traversal */

Postorder (BT );

Printf ("\ n ");

Printf ("by layer:");/* traverse by layer */

Levelorder (BT );

Printf ("\ n ");
/* Search for an element node from a binary tree */

Printf ("enter a character to be searched: \ n ");

Scanf ("% C", & X);/* the space in the format string skips the blank character */

Px = findbtree (BT, X );

If (PX ){

Printf ("search successful: % C \ n", * px );

} Else {

Printf ("failed to search! \ N ");

}

Printf ("binary tree depth :");

Printf ("% d \ n", btreedepth (BT ));

Clearbtree (& BT );

Return 0;

}

The following is the implementation of Binary Tree _ C language (II).

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.