Binary Tree Operation Review

Source: Internet
Author: User

[Cpp]
/* Binary Tree operation review */
# Include <stdio. h>
 
# Define BACK_ODER-1
# Define IN_ODER 0
# Define PRE_ODER 1
# Define LEVEL_ODER 2 // hierarchical Traversal
Typedef struct _ Node {
Char data;
Struct _ Node * lchild;
Struct _ Node * rchild;
} Node, * Tree;
 
/* Common method for generating Binary Trees
* Enter the node values in the binary tree in the FIFO order.
* Construct the binary tree T represented by the binary linked list. Enter a space to indicate the empty subtree. */
Node * CreateTree ()
{
Char ch;
Scanf ("% c", & ch );
Node * T;
 
If (ch = '')/* null */
Return NULL;
Else
{
T = (Node *) malloc (sizeof (Node ));
If (! T)
Exit (0 );
T-> data = ch;/* generate the root node */
T-> lchild = CreateTree ();/* construct the left subtree */
T-> rchild = CreateTree ();/* construct the right subtree */
}
Return T;
}
/* A binary tree is generated by the first root sequence and the middle root sequence.
* Recursion. Pre is the first followed sequence, and in is the middle root sequence.
* Pre_s is the start of the first root sequence, and pre_e is the end of the first root sequence.
* In_s is the start of the root sequence, and in_e is the end of the root sequence.
*/
Node * Convert (char pre [], int pre_s, int pre_e,
Char in [], int in_s, int in_e)
{
If (in_s> in_e) return NULL;
 
Int I = in_s;
For (I = in_s; I <in_e & in [I]! = Pre [pre_s]; I ++ );
 
 
Node * p = (Node *) malloc (sizeof (Node ));
P-> data = pre [pre_s];
P-> lchild = Convert (pre, pre_s + 1, pre_s + i-in_s,
In, in_s, I-1 );
P-> rchild = Convert (pre, pre_s + i-in_s + 1, pre_e,
In, I + 1, in_e );
 
Return p;
}
/* Calculate the degree of a binary tree */
Int GetDegree (const Tree head)
{
Int degree = 0;
Int m, n;
If (! Head) return 0;
 
If (head-> lchild & head-> rchild) return 2;
Else if (! Head-> lchild &&! Head-> rchild) return 0;
Else {
M = GetDegree (head-> lchild );
N = GetDegree (head-> rchild );
If (2 = m | 2 = n) return 2;
Else return 1;
}
Return degree;
}
/* Calculate the height of a binary tree */
Int GetHight (const Tree head)
{
Int m, n;
 
If (! Head) return 0;
 
M = GetHight (head-> lchild );
N = GetHight (head-> rchild );
 
Return 1 + (m> n? M: n );
 
}
/* Output the grandfather node of a specified element in a binary tree (including itself)
* Recursive thinking: If this node is in its subtree, It is a grandfather node.
* Return value: 1 indicates that there is a subtree, and 0 indicates no */
Int GetGrandPa (const Tree head, const char e)
{
If (! Head) return 0;
 
If (GetGrandPa (head-> lchild, e) | GetGrandPa (head-> rchild, e) | e = head-> data) // This node exists in the subtree
{
Printf ("% c", head-> data );
Return 1;
}
Else return 0;
}
/* Traverse a binary tree. The parameter oder controls the traversal mode */
Void Tranverse (Node * head, int oder)
{
If (! Head) return;
If (LEVEL_ODER = oder)
{
LevTranverse (& head, 1 );
Return;
}
 
If (PRE_ODER = oder) printf ("% c \ t", head-> data );
Tranverse (head-> lchild, oder );
If (IN_ODER = oder) printf ("% c \ t", head-> data );
Tranverse (head-> rchild, oder );
If (BACK_ODER = oder) printf ("% c \ t", head-> data );
Return;
}
/* Hierarchical traversal, uses recursion instead of queue.
* Recursive thinking: store the next layer while traversing the current Layer
* Nodes [] indicates the nodes of the current layer stored. count indicates the number of elements in the current layer */
Void LevTranverse (const Node * nodes [], int count)
{
Int I = 0, j = 0;
 
If (0 = count) return;
 
Node * nextNodes [100] = {0 };
For (I = 0, j = 0; I <count; I ++)
{
Printf ("% c \ t", nodes [I]-> data );
If (nodes [I]-> lchild) nextNodes [j ++] = nodes [I]-> lchild;
If (nodes [I]-> rchild) nextNodes [j ++] = nodes [I]-> rchild;
}
LevTranverse (nextNodes, j );
Return;
}
 
Int main (int argc, char * argv [])
{Www.2cto.com
Char pre [] = "abcde ";
Char in [] = "bcade ";
Node * head = NULL;
 
Head = Convert (pre, 0, strlen (pre)-1,
In, 0, strlen (in)-1 );
 
Printf ("Hight: % d \ n", GetHight (head ));
Printf ("Degree: % d \ n", GetDegree (head ));
If (! GetGrandPa (head, 'C') printf ("No grandpa! "); Printf (" \ n ");
Tranverse (head, LEVEL_ODER); printf ("\ n ");

System ("PAUSE ");
}

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.