Learning the data structure, I have never understood binary trees, and I have no idea how to call pointers. I learned a little about binary tree over the past two days. I first wrote the code and then learned and improved it.
# Include <stdlib. h>
# Include <stdio. h>
Typedef char datatype;
Typedef struct Node
{
Datatype data;
Struct node * lchild;
Struct node * rchild;
} Bintnode, * bintree;
Void createb (bintree & T)
{
Datatype ch;
Scanf ("% C", & Ch );
If (CH = '.')
T = NULL;
Else
{
T = (bintnode *) malloc (sizeof (bintnode ));
T-> DATA = CH;
Createb (t-> lchild );
Createb (t-> rchild );
}
}
Void inorder (bintree & T)
{
If (T! = NULL)
{
Inorder (t-> lchild );
Printf ("% 3C", T-> data );
Inorder (t-> rchild );
}
}
Int search (bintree & T, datatype ch)
{/* Find the node CH and return 1; otherwise, return 0 */
If (t = NULL)
Return 0;
If (t-> DATA = CH)
Return 1;
Return
Search (t-> lchild, CH );
Return
Search (t-> rchild, CH );
}
Void swaplr (bintree & T)
{/* Switch the left and right branches of all nodes x */
Bintree T;
If (T! = NULL)
{
Swaplr (t-> lchild );
Swaplr (t-> rchild );
If (t-> lchild = NULL & T-> rchild)
{
T-> lchild = T-> rchild;
T-> rchild = NULL;
}
Else
If (t-> lchild & T-> rchild = NULL)
{
T-> rchild = T-> lchild;
T-> lchild = NULL;
}
Else
If (t-> lchild & T-> rchild)
{
T = T-> lchild;
T-> lchild = T-> rchild;
T-> rchild = T;
}
}
}
Int sortbt (bintree & T)
{/* Determine whether it is a binary sorting tree */
If (t = NULL)
Return 1;
If (t-> lchild & T-> lchild-> data <t-> data) | (t-> rchild & T-> rchild-> DATA> T-> data ))
Return 0;
Return
Sortbt (t-> lchild );
Return
Sortbt (t-> rchild );
}/* Binary sort tree or an empty tree; or a binary tree with the following properties:
(1) If the left subtree is not empty, the value of all nodes on the left subtree is smaller than the value of its root node;
(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
(3) Left and Right decision trees are also Binary Decision Trees ;*/
Void countdef (bintree T, Int & N)
{/* Count the number of leaf nodes */
If (T! = NULL)
{
If (t-> lchild = NULL & T-> rchild = NULL)
N ++;
Countdef (t-> lchild, N );
Countdef (t-> rchild, N );
}
}
Void depthbt (bintree T, int D, int * H)
{/* Calculate the depth of a binary tree */
If (t)
{
D ++;
If (D> * H)
* H = D;
Depthbt (t-> lchild, D, H );
Depthbt (t-> rchild, D, H );
}
}
Void gradebt (bintree & T, datatype CH, int D, int * n)
{/* Find the layer of the CH node */
If (t)
{
D ++;
If (t-> DATA = CH)
* N = D;
Gradebt (t-> lchild, CH, d, n );
Gradebt (t-> rchild, CH, d, n );
}
}
Void main ()
{
Datatype ch;
Bintree root, T;
Int D = 0, H = 0, L = 1, n = 0;
Root = (bintnode *) malloc (sizeof (bintnode ));
Printf ("Please input the characters to be traversed in the ascending order: \ n ");
Createb (Root );
Inorder (Root );
Printf ("\ n ");
Depthbt (root, D, & H );
Printf ("depth = % d \ n", H );
Gradebt (root, 'C', 0, & L );
Printf ("grade = % d \ n", L );
Countdef (root, N );
Printf ("Count = % d \ n", N );
Printf ("\ n ");
}
/* After the program runs, the result is:
Please input the characters to be traversed in the ascending order:
Abc..de. G.. F...
C B e g d F
Depth = 5
Grade = 3
Count = 3
Press any key to continue
*/
Basic operations for building a binary tree (Linked List)