Just now, I want to say goodbye-in the tree, except for the binary tree, nothing else has been said. Why can we summarize it? Because we have already covered two basic uses of the tree, and if we talk about B + and B-, we can't help but mention search. If it is the winner tree, we can't help but mention sorting. To this end, put this part behind. My previous efforts only gave you a basic idea about how to use the tree.
The two basic uses of the tree can be compared by material and spirit.
One purpose is to store data with tree structures, such as directories and genealogy. To store a non-linear tree structure on a linear storage carrier (memory or disk), a flag must indicate the structure of the output tree. Therefore, as long as the root and sub-tree can be distinguished, the tree can be stored in a variety of ways-Multi-fork linked list, left child-right brother binary linked list, generalized table, and multi-dimensional array. Because of operation requirements, the storage method is not randomly selected. For example, the dual-parent linked list is selected for the implementation of the parallel query set.
One purpose is to make a logical decision. At this time, we often hear a term called decision tree. The most common structure is binary tree. A child represents true, and a child represents false. For the multi-Cross decision tree, an example is to find all the solutions of the eight Queens-This Gaussian is wrong (a total of 92 groups of solutions, Gauss first said 76 groups of solutions ), we are not comparable to Gauss, but we will make computers do our jobs.
Just as the philosophical field is still entangled in the issues of material and spiritual origins, the same is true in the tree. In some cases, it is not possible to distinguish whether it is used as storage or judgment. For example, the search tree stores both data and contains judgment.
Compared with the subsequent graph, the tree is more basic and commonly used. You don't know how to find the shortest path, but you are dealing with trees all the time-look at the folders on your computer.
Finally, it comes with a program for finding all the solutions of Queen n.
# Include <stdio. h>
# Define N 8
Int layout [N]; // Layout
Int key = 0;
Int judge (INT row, int col) // determines whether the data can be put down in (row, col)
{
Int I;
For (I = 0; I <row; I ++)
{
If (layout [I] = col) return 0; // Same Column
If (I-layout [I] = row-col) return 0; // the same primary diagonal
If (I + layout [I] = row + col) return 0; // The same diagonal line
}
Return 1;
}
Void lay (INT row) // put queen on the row
{
Int I;
If (ROW = N) // end n queen output la s
{
Printf ("/n % 02d", ++ key );
For (I = 0; I <n; I ++) printf ("% C % d", layout [I] + 'A', I + 1 );
}
Else
{
For (I = 0; I <n; I ++) // put queen on the I Column
{
Layout [row] = I;
If (Judge (row, I) Lay (row + 1 );
}
}
}
Int main ()
{
Lay (0 );
Return 0;
}