User Binary Tree sorting requires user registration. Enter the following registration information: {code ...} each time a new user is registered, the user enters the sorting rule. The new user must traverse from left to right under the recommender and from top to bottom, the following figure shows how to insert data into a blank space :... user Binary Tree sorting requirements
-
Enter the following registration information for user registration:
-Email-password-Confirm Password-recommender ID (this ID can be manually added to the database)
-
Each time a new user is registered, the user enters the sorting
-
Sorting rules
- The new user must be under the recommender
- Traverse from left to right, from top to bottom, and find the vacant space to insert data
The following is an illustration:
Assume that A is the root node (A is the first user manually added)
If A new user is registered (assuming the new user is B) and the recommender ID is the ID of A, the order is as follows:
A / B
Another C user is registered. The recommender ID is the ID of B, then:
A / B / C
There is a d user registration. The recommender ID is the ID of A, then:
A / \ B D /
C
There is an E-user registration. The recommender ID is the ID of B, then
A / \ B D / \ C E
There is an F user registration. The recommender ID is the ID of A, then
A / \ B D / \ / C E F
There is a G user registration. The recommender ID is the ID of E, then
A / \ B D / \ / C E F / G
There is an H user registration. The recommender ID is the ID of B, then
H is recommended to B, so H must be under B, and then search from left to right. C and E occupy the two positions above, so locate in the next row and find the vacancy, insert H. The following are the rules.
A / \ B D / \ / C E F / /H G
One I-user and J-user are registered one after another, and all entered are C IDs.
A / \ B D / \ / C E F / \ / H I G /J
One K-user and one L-user are registered one after another, and all entered is the ID of A, then
A / \ B D / \ / \ C E F K/ \ / \ H I G L /J
There is an M User, N and O users are registered again, fill in respectively B user, C user, and L user ID, then:
A / \ B D / \ / \ C E F K / \ / \ H I G L / \ J M
A / \ B D / \ / \ C E F K / \ / \ H I G L / \ / J M N
A / \ B D / \ / \ C E F K / \ / \ H I G L / \ / /J M N O
A p user, Q user, R user, and S user are registered with the ID of user A, user B, user E, and user A respectively. Then:
A / \ B D / \ / \ C E F K / \ / \ / H I G L P / \ / / J M N O
A / \ B D / \ / \ C E F K / \ / \ / H I G L P / \ / \ / J M N Q O
A / \ B D / \ / \ C E F K / \ / \ / H I G L P / \ / \ / / J M N Q R O
A / \ B D / \ / \ C E F K / \ / \ / \ H I G L P S / \ / \ / / J M N Q R O
Reply content:
User Binary Tree sorting requirements
-
Enter the following registration information for user registration:
-Email-password-Confirm Password-recommender ID (this ID can be manually added to the database)
-
Each time a new user is registered, the user enters the sorting
-
Sorting rules
- The new user must be under the recommender
- Traverse from left to right, from top to bottom, and find the vacant space to insert data
The following is an illustration:
Assume that A is the root node (A is the first user manually added)
If A new user is registered (assuming the new user is B) and the recommender ID is the ID of A, the order is as follows:
A / B
Another C user is registered. The recommender ID is the ID of B, then:
A / B / C
There is a d user registration. The recommender ID is the ID of A, then:
A / \ B D /
C
There is an E-user registration. The recommender ID is the ID of B, then
A / \ B D / \ C E
There is an F user registration. The recommender ID is the ID of A, then
A / \ B D / \ / C E F
There is a G user registration. The recommender ID is the ID of E, then
A / \ B D / \ / C E F / G
There is an H user registration. The recommender ID is the ID of B, then
H is recommended to B, so H must be under B, and then search from left to right. C and E occupy the two positions above, so locate in the next row and find the vacancy, insert H. The following are the rules.
A / \ B D / \ / C E F / /H G
One I-user and J-user are registered one after another, and all entered are C IDs.
A / \ B D / \ / C E F / \ / H I G /J
One K-user and one L-user are registered one after another, and all entered is the ID of A, then
A / \ B D / \ / \ C E F K/ \ / \ H I G L /J
There is an M User, N and O users are registered again, fill in respectively B user, C user, and L user ID, then:
A / \ B D / \ / \ C E F K / \ / \ H I G L / \ J M
A / \ B D / \ / \ C E F K / \ / \ H I G L / \ / J M N
A / \ B D / \ / \ C E F K / \ / \ H I G L / \ / /J M N O
A p user, Q user, R user, and S user are registered with the ID of user A, user B, user E, and user A respectively. Then:
A / \ B D / \ / \ C E F K / \ / \ / H I G L P / \ / / J M N O
A / \ B D / \ / \ C E F K / \ / \ / H I G L P / \ / \ / J M N Q O
A / \ B D / \ / \ C E F K / \ / \ / H I G L P / \ / \ / / J M N Q R O
A / \ B D / \ / \ C E F K / \ / \ / \ H I G L P S / \ / \ / / J M N Q R O
Not to mention why such a strange demand should be realized.
To simply implement this function, you can solve the problem by storing binary trees in the data structure in sequence.
Here, an array in PHP is used to simulate the sequential storage of Binary Trees. The key in the array is equivalent to the storage address, and the value is equivalent to the stored data. Of course, the key has a structure similar to the following:
0 / \ 1 2
That is, if the key value of the parent node is n, the key of the left child node is 2n + 1, and the key of the right child node is 2 (n + 1 ). Below is a simple PHP code implementation (the home has no PHP runtime environment, and the code is not tested completely correct @#@):
phpClass Tree {private $ data = []; public function _ construct () {}/*** @ param mixed $ val * @ param int $ key value of the pid parent node in $ data * @ return int returns the key value of the inserted node in $ data. */public function add ($ val, $ pid = 0) {// Insert the root node if (! Count ($ this-> data) {$ this-> data [0] = $ val; return 0 ;}// if the left subnode is empty, insert the left subnode if (! Isset ($ this-> data [2 * $ pid + 1]) {$ this-> data [2 * $ pid + 1] = $ val; return 2 * $ pid + 1;} // if the right subnode is empty, insert the right subnode if (! Isset ($ this-> data [2 * $ pid + 2]) {$ this-> data [2 * pid + 2] = val; return 2 * pid + 2;} // obtain the key value of the last node in $ data $ maxKey = max (array_keys ($ this-> data ); // if it is a complete binary tree (that is, $ data has no empty nodes), insert the value if (count ($ this-> data) at the end of $ data) ===$ maxKey + 1) {$ this-> data [$ maxKey + 1] = $ val; return $ maxKey + 1 ;} // if it is not a complete Binary Tree, insert the value for ($ I = 0; $ I <= $ maxKey; ++ $ I) {if (! Isset ($ this-> data [$ I]) {$ this-> data [$ I] = $ val; return $ I ;}}}} // simple test $ tree = new Tree (); $ aid = $ tree-> add ('A', 0 ); $ bid = $ tree-> add ('B', $ aid); $ tree-> add ('C', $ bid );
Hello, will this happen to you? I have also encountered this requirement and cannot implement it. Please help, qq316832727