Instructor Feng,Hua Qing vision embedded college lecturer.
I. First Implementation of Binary Trees
The following program implements a Complete Binary Tree, where the node of the tree structure is 1, 2 ,......, N.
Typedef char dtype;
Typedef struct Node
{
Dtype data;
Struct node * left;
Struct node * right;
} Bitree;
Bitree * bitree_create (int I, int N)
{
Bitree * R;
If (r = (bitree *) malloc (sizeof (bitree) = NULL)
{
Printf ("malloc failed \ n ");
Return NULL;
}
R-> DATA = I;
R-> left = 2 * I <= n? Bitree_create (2 * I, n): NULL;
R-> right = 2 * I + 1 <= n? Bitree_create (2 * I + 1, n): NULL;
Return R;
}
The Calling procedure is as follows:
If (r = bitree_create (1, 8) = NULL)
Return 0;
Ii. Binary Tree implementation
Because the node of the first program tree structure is fixed to 1, 2 ,......, N. If you want to set the node data to the value you need, you can make the following adjustments:
Typedef char dtype;
Typedef struct Node
{
Dtype data;
Struct node * left;
Struct node * right;
} Bitree;
Bitree * bitree_create (int I, int N, dtype A [])
{
Bitree * R;
If (r = (bitree *) malloc (sizeof (bitree) = NULL)
{
Printf ("malloc failed \ n ");
Return NULL;
}
R-> DATA = A [I];
R-> left = 2 * I <= n? Bitree_create (2 * I, n, a): NULL;
R-> right = 2 * I + 1 <= n? Bitree_create (2 * I + 1, n, a): NULL;
Return R ;}
The Calling procedure is as follows:
Dtype A [] = {'', 'A', 'B', 'C '};
Char s [] = {'D', 'C', 'F '};
If (r = bitree_create (1, sizeof (a)/sizeof (dtype)-1, A) = NULL)
Return 0;
That is, the node number in the tree is used as the subscript of an array, and the content of the array element is defined by the user. Such a Complete Binary Tree is more flexible than the first one.
Iii. Third Implementation of Binary Trees
The above two methods are all completely Binary Trees. To create a tree of any shape, refer to the following procedure
Bitree * bitree_create ()
{
Bitree * R;
Char ch;
Scanf ("% C", & Ch );
If (CH = '#')
Return NULL;
If (r = (bitree *) malloc (sizeof (bitree) = NULL)
{
Printf ("malloc failed \ n ");
Return R;
}
R-> DATA = CH;
R-> left = bitree_create ();
R-> right = bitree_create ();
Return R;
}
The Calling procedure is as follows:
Bitree * r = NULL;
If (r = bitree_create () = NULL)
Return 0;
When executing a program, you must input it in the First Order of the tree structure. For example, if you want to create the tree shown above, you must enter the following when executing the program:
ABC ## de # G ## F ###
This program uses the Left or Right child that is not present in the tree node and uses # to represent it. As a recursive termination condition, a tree of any shape can be created.
IV. The fourth implementation of Binary Trees
Since the node in the tree structure of the above program is fixed as a variable of the character line, if you want to make the node data the value you need, you can make the following adjustments:
Bitree * bitree_create2 (dtype B [])
{
Bitree * R;
Int ch;
Scanf ("% d", & Ch );
If (CH = 4)
Return NULL;
If (r = (bitree *) malloc (sizeof (bitree) = NULL)
{
Printf ("malloc failed \ n ");
Return R;
}
R-> DATA = B [CH];
R-> left = bitree_create2 (B );
R-> right = bitree_create2 (B );
Return R;
}
The Calling procedure is as follows:
Dtype B [] = {'', 'A', 'B', 'C', 'D', 'E', 'F', 'G ', '#'};
If (r = bitree_create2 (B) = NULL)
Return 0;
When executing the program, place the nodes in the tree structure in the B array in advance, and enter the corresponding node input. You only need to enter the subscript of the corresponding value in the B array. For example, the Tree of the third method should also be set up. Enter:
1 2 3 8 8 4 5 8 7 8 6 8 8
Source:Huaqing vision embedded College,Original article address:Http://www.embedu.org/Column/Column899.htm
For more information about embedded systems, seeHua Qing visionInstructor blog>
Four implementations of Binary Trees