Data structures and algorithms-how PHP implements user two fork tree sorting requirements

Source: Internet
Author: User
User two fork tree sorting requirements

    1. User registration, enter the following registration information:

      - 电子邮箱- 密码- 确认密码- 推荐人ID(此ID可以在数据库中手动增加一个)
    2. Every time a new user is registered, the user goes into the sort

    3. Sorting rules

      1. The new user must be below the referrer.
      2. Traverse from left to right, top to bottom, find empty insert data

The following are illustrations:

Suppose a is the root node (A is the first user added manually)
A new user is registered (assuming the new user is B), the referrer ID is filled in with the ID of a, then the sort is as follows:

    A    /   

Another C user registration, the referrer ID is filled with the ID of B, then:

     A    /   B  / C

There is a D user registration, the referrer ID is filled in the ID of a, then:

   A  /  \ B   D  /

C

There is an e-user registration, the referrer ID is filled with the ID of B, then

       A      /  \     B   D      /  \   C   E

There is an F-user registration, the referrer ID is filled in with the ID of a, then

       A     /   \    B     D    /  \   /  C   E F

There is a G user registration, the referrer ID is filled in the ID of E, then

        A     /     \    B      D       /  \    /  C   E  F      /     G

There is an H user registration, the referrer ID is filled with the ID of B, then

H is the recommended person is B, so, h must be under B, and then follow the way from left to right, C and E accounted for the above two positions, so to the next row to find, find empty, then insert H, the following are the rules

        A     /     \    B      D       /  \    /  C   E  F /    /H   G

There is an I user, and J users are registered in succession, filled in is the ID of C, then

           A        /       \       B       D         /     \   /    C     E  F   /   \   /  H    I G /J

There is a K user, and L users have been registered in succession, filled in is the ID of a, then

          A     /         \    B          D      /     \      /   \ C       E   F   K/   \    /   \   H    I  G   L  /J

There is an M user, n users and O users are registered in, respectively, the 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

There is a P user, Q user, R user, s user and registered in, respectively, is a user, B user, E user, a user ID. The

                  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 two fork tree sorting requirements

    1. User registration, enter the following registration information:

      - 电子邮箱- 密码- 确认密码- 推荐人ID(此ID可以在数据库中手动增加一个)
    2. Every time a new user is registered, the user goes into the sort

    3. Sorting rules

      1. The new user must be below the referrer.
      2. Traverse from left to right, top to bottom, find empty insert data

The following are illustrations:

Suppose a is the root node (A is the first user added manually)
A new user is registered (assuming the new user is B), the referrer ID is filled in with the ID of a, then the sort is as follows:

    A    /   

Another C user registration, the referrer ID is filled with the ID of B, then:

     A    /   B  / C

There is a D user registration, the referrer ID is filled in the ID of a, then:

   A  /  \ B   D  /

C

There is an e-user registration, the referrer ID is filled with the ID of B, then

       A      /  \     B   D      /  \   C   E

There is an F-user registration, the referrer ID is filled in with the ID of a, then

       A     /   \    B     D    /  \   /  C   E F

There is a G user registration, the referrer ID is filled in the ID of E, then

        A     /     \    B      D       /  \    /  C   E  F      /     G

There is an H user registration, the referrer ID is filled with the ID of B, then

H is the recommended person is B, so, h must be under B, and then follow the way from left to right, C and E accounted for the above two positions, so to the next row to find, find empty, then insert H, the following are the rules

        A     /     \    B      D       /  \    /  C   E  F /    /H   G

There is an I user, and J users are registered in succession, filled in is the ID of C, then

           A        /       \       B       D         /     \   /    C     E  F   /   \   /  H    I G /J

There is a K user, and L users have been registered in succession, filled in is the ID of a, then

          A     /         \    B          D      /     \      /   \ C       E   F   K/   \    /   \   H    I  G   L  /J

There is an M user, n users and O users are registered in, respectively, the 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

There is a P user, Q user, R user, s user and registered in, respectively, is a user, B user, E user, a user ID. The

                  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

Don't talk about why such a strange need is to be achieved.
Simple implementation of this function, can be based on the data structure of the binary tree sequential storage method to solve it.

In this case, we use arrays in PHP to simulate the sequential storage of binary trees, where the key in the array is the equivalent of the storage address, and value is the equivalent of the stored data. Of course, key has a structure similar to the following:

             0           /   \           1   2

That is, the parent node key value is recorded as N, then the left child node key is 2n+1 and the right child node key is 2 (n+1). The following simple implementation of the PHP code (by the home without PHP running environment, not testing the full correctness of the code @#@):

phpClass Tree {Private $data = []; Public Function __construct () {}/** * @param mixed $val * @param int $pid The key value of the parent node in $DATA * @return Int returned The corresponding key value of the insertion node in the $data */Public Function Add ($val, $pid = 0) {//If the empty tree is inserted into the root node if (!count ($this->data)            ) {$this->data[0] = $val;        return 0;            }//Joz node is empty insert left child node if (!isset ($this->data[2* $pid +1]) {$this->data[2* $pid +1] = $val;        return $pid +1;            }//If right child node is empty insert right child node if (!isset ($this->data[2* $pid +2]) {$this->data[2*pid+2] = val;        return 2*pid+2;        }//Gets the key value of the last node in the $data $maxKey = max (Array_keys ($this->data); In the case of a fully binary tree (that is, there are no empty nodes in the $data), the $data is inserted at the last value if (count ($this->data) = = = $maxKey + 1) {$this->data[$            MAXKEY+1] = $val;        return $maxKey + 1; }//Not a full binary tree inserts a value for ($i = 0; $i <= $maxKey at the smallest empty key in $data; + +$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, do you have this demand? Now I also meet this demand, can not achieve ah ... Ask for help, QQ369832727.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.