Anybody please help use the below table and tree structure and calculate left and right count.

My Table Creation Query:

CREATE TABLE testmlm.binary_tree (
id INT( 11 ) NOT NULL AUTO_INCREMENT ,
name VARCHAR( 150 ) NOT NULL ,
left_id INT( 11 ) NULL ,
right_id INT( 11 ) NULL ,
level INT( 6 ) NULL ,
PRIMARY KEY ( id )
) ENGINE = InnoDB;

      Field     Type
     -------       ------
       id          int(11)
       name        varchar(150)
     left_id    int(11)
     right_id   int(11)
      level     int(6)

After inserting Values:

id name left_id right_id level
-- ---- ------- ---------- -----
1 abc 2 3 1
2 def 4 5 2
3 ghi 6 7 2
4 jkl 8 null 3
5 mno null 9 3
6 pqr null null 3
7 stu 10 null 3
8 vwx null null 4
9 yza null null 4
10 bcd null null 4

           My Tree Structure
          -------------------

                id:1
            /         \
          id:2       id:3
        /     \      /     \
     id:4    id:5  id:6    id:7
     /
  id:8          \          / 
                id:9     id:10

I want to create Customer List, and it will generate via MySQL queries like this for all above 10 Ids

                          For ID 1

       Left Side                                  Right Side
  ID             Name                           ID             Name
  2              def                            3               ghi
  4              jkl                            6               pqr
  5              mno                            7               stu
  8              vwx                           10               bcd
  9              yza


                          For ID 2

       Left Side                                  Right Side
  ID             Name                           ID             Name
  4              jkl                            5               mno
  8              vwx                            9               yza


                          For ID 3

       Left Side                                  Right Side
  ID             Name                           ID             Name
  6              pqr                            7               stu
                                                10              bcd


                          For ID 4

       Left Side                                  Right Side
  ID             Name                           ID             Name
  8              vwx


                          For ID 5

       Left Side                                  Right Side
  ID             Name                           ID             Name
                                                9              yza


                          For ID 6

       Left Side                                  Right Side
  ID             Name                           ID             Name


                          For ID 7

       Left Side                                  Right Side
  ID             Name                           ID             Name
  10             bcd


                          For ID 8

       Left Side                                  Right Side
  ID             Name                           ID             Name


                          For ID 9

       Left Side                                  Right Side
  ID             Name                           ID             Name


                          For ID 10

       Left Side                                  Right Side
  ID             Name                           ID             Name

How its possible! Please help me...

I was looking at a similar database yesterday.

You can read here, and here . Left and right is on part 2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.