Basic operations for building a binary tree (Linked List)
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
# Include
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 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
*/