SlideShare a Scribd company logo
1 of 9
Download to read offline
Range Queries                               17-1



    Range Queries on Binary Search Trees
Algorithm Ê Ò ÉÙ ÖÝË ÑÔÐ (Ì , ½ , ¾ ):
 1: If Ì is empty, return
 2: if key(Ì .root) < ½ then
 3:    RangeQuerySimple(Ì .right, ½ , ¾ )
 4: else if key(Ì .root) > ¾ then
 5:    RangeQuerySimple(Ì .left, ½ , ¾ )
 6: else
 7:    RangeQuerySimple(Ì .left, ½ , ¾ )
 8:    report Ì .root
 9:    RangeQuerySimple(Ì .right, ½ , ¾ )
Range Queries                                                     17-2



     Range Queries on Binary Search Trees


                                                   35


                                        18                            45


                    10                            25             42


       7                           14        22             31


 4          8                 12        16             30


                9




 ¯   query range         ¾℄
Range Queries                                                          17-3



                Coloring for Range Queries


Algorithm Ê      Ò ÉÙ ÖÝ ÓÐÓÙÖ(         Ì   , ½ , ¾ , splitValue = undef):

 1:   if Ì is non-empty then Ì .root is gray
 2:      if key(Ì .root) < ½ then left child is white
 3:         RangeQueryColour(Ì .right, ½ , ¾ , splitValue)
 4:      else if key(Ì .root > ¾ then right child is white
 5:         RangeQueryColour(Ì .left, ½ , ¾ , splitValue)
 6:      else if splitValue       undef then root is splitting point
 7:         splitValue = key(Ì .root)
 8:         RangeQueryColour(Ì .left, ½ , ¾ , splitValue)
 9:         report Ì .root
10:         RangeQueryColour(Ì .right, ½ , ¾ , splitValue)
11:      else below the splitting point
12:         if key(Ì .root) < splitValue then right child is black
13:            RangeQueryColour(Ì .left, ½ , ¾ , splitValue)
14:            report Ì .root
15:            report all nodes in Ì .right
16:         else left child is black
17:            report all nodes in Ì .left
18:            report Ì .root
19:            RangeQueryColour(Ì .right, ½ , ¾ , splitValue)
Range Queries                                                       17-4



                        Quad-tree example

           *
                *               *
                    *


                *                       *
                            *




                *                               *


                                                            *




                                    *


       *                *                               *       *


           *                                *       *


                        *               *
Range Queries                                                                         17-5



                               kd-tree example
 140                                *
 130   *
 120        *
 110                 *
 100                                          *


 80                            *




 40                                                *
 30                                                                *


 10                                                                              *


       10   20       30       40    50        70   80             110           140




                                              Ü     ¼
                          Ý        ½¾¼                        Ý        ¿¼
             Ü        ¿¼                 Ü   ½¼        Ü    ½½¼         Ü   ¼
       Ý    ½½¼            *            *     *         *    *          *   *


       *         *
Range Queries                                                                 17-6



                     kd-tree range queries

                                    Ü   ¼

                     Ý       ½¾¼                  Ý   ¿¼

            Ü       ¿¼         Ü   ½¼   Ü   ½½¼               Ü       ¼

       Ý   ½½¼           *     *    *   *    *        Ý       ¼           *


       *        *                                 Ü       ¼       *


                                                 *        *

 ¯   query range         ¼ ¼℄ ¢ ¾¼ ¼℄
Range Queries                                                     17-7



                       Building kd-trees
Algorithm Ë ÑÔÐ Ù Ð                ÌÖ ( points È , int level ):
 1: if È     ½     then
 2:    Build one-node tree and return
 3: else
 4:    if level is even then
 5:        Let ÜÑ = median Ü-coordinate among È
 6:        set È         points in È with Ü-coord. ÜÑ
 7:        set È Ö       points in È with Ü-coord. ÜÑ
 8:    else
 9:        [...] // Similarly with Ý -coordinate
10:    ̽ = SimpleBuildkdTree(È , level      ·½  )
11:    ̾ = SimpleBuildkdTree(È , level
                                        Ö
                                             ·½  )
12:    return tree with subtrees ̽ and ̾
Range Queries                                               17-8



        Building kd-trees with pre-sorting
Algorithm ÈÖ ×ÓÖØ Ù Ð          ÌÖ ( points È ):
 1: Let ÈÜ be a copy of È , sort by Ü-coordinate
 2: Let ÈÝ be a copy of È , sort by Ý -coordinate
 3: buildKdTree(ÈÜ , ÈÝ , 0)


Algorithm Ù Ð Ã ÌÖ ( points ÈÜ , points ÈÝ , level ):
 1: // ÈÜ and ÈÝ are same points, sorted by Ü and Ý respectively
 2: if ÈÜ       ½   then
 3:    Build one-node tree and return
 4: else
 5:    if level is even then
 6:        Let ÜÑ = median Ü-coordinate among È
 7:        set ÈÜ        points in ÈÜ with Ü-coord. ÜÑ
                   Ö
 8:        set ÈÜ        points in ÈÜ with Ü-coord. ÜÑ
                   Ö
 9:        set ÈÝ        points in ÈÝ with Ü-coord. ÜÑ
10:        set ÈÝ        points in ÈÝ with Ü-coord. ÜÑ
11:        // All sub-lists maintain their relative order.
12:    else
13:        [...] // Similarly with Ý -coordinate
14:                                 ·½
       ̽ = buildKdTree(ÈÜ , ÈÝ , level        )
15:
                                Ö
                                    ·½
                                     Ö
       ̾ = buildKdTree(ÈÜ , ÈÝ , level        )
16:    return tree with subtrees ̽ and ̾
Range Queries                                                                       17-9



                              Range trees

BST by Ü-coordinate:
                                     (60,70)


           (20,50)                                          (80,60)


 (10,80)                  (40,10)                 (70,90)             (90,30)


                (30,40)             (50,20)




                                              BST of subtree by Ý -coordinate:


                                                            (30,40)


                                               (40,10)                  (20,50)


                                                    (50,20)                     (10,80)

More Related Content

More from Institute of Computing Technology, Chinese Academy of Sciences (9)

C make tutorial
C make tutorialC make tutorial
C make tutorial
 
Distributedsystems 100912185813-phpapp01
Distributedsystems 100912185813-phpapp01Distributedsystems 100912185813-phpapp01
Distributedsystems 100912185813-phpapp01
 
Gfs andmapreduce
Gfs andmapreduceGfs andmapreduce
Gfs andmapreduce
 
leveldb Log format
leveldb Log formatleveldb Log format
leveldb Log format
 
随即存储引擎Bitcask
随即存储引擎Bitcask随即存储引擎Bitcask
随即存储引擎Bitcask
 
Skip list
Skip listSkip list
Skip list
 
Five minuterule
Five minuteruleFive minuterule
Five minuterule
 
Lsm
LsmLsm
Lsm
 
Hfile格式详细介绍
Hfile格式详细介绍Hfile格式详细介绍
Hfile格式详细介绍
 

Recently uploaded

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Recently uploaded (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

二叉查找树范围查询算法

  • 1. Range Queries 17-1 Range Queries on Binary Search Trees Algorithm Ê Ò ÉÙ ÖÝË ÑÔÐ (Ì , ½ , ¾ ): 1: If Ì is empty, return 2: if key(Ì .root) < ½ then 3: RangeQuerySimple(Ì .right, ½ , ¾ ) 4: else if key(Ì .root) > ¾ then 5: RangeQuerySimple(Ì .left, ½ , ¾ ) 6: else 7: RangeQuerySimple(Ì .left, ½ , ¾ ) 8: report Ì .root 9: RangeQuerySimple(Ì .right, ½ , ¾ )
  • 2. Range Queries 17-2 Range Queries on Binary Search Trees 35 18 45 10 25 42 7 14 22 31 4 8 12 16 30 9 ¯ query range ¾℄
  • 3. Range Queries 17-3 Coloring for Range Queries Algorithm Ê Ò ÉÙ ÖÝ ÓÐÓÙÖ( Ì , ½ , ¾ , splitValue = undef): 1: if Ì is non-empty then Ì .root is gray 2: if key(Ì .root) < ½ then left child is white 3: RangeQueryColour(Ì .right, ½ , ¾ , splitValue) 4: else if key(Ì .root > ¾ then right child is white 5: RangeQueryColour(Ì .left, ½ , ¾ , splitValue) 6: else if splitValue undef then root is splitting point 7: splitValue = key(Ì .root) 8: RangeQueryColour(Ì .left, ½ , ¾ , splitValue) 9: report Ì .root 10: RangeQueryColour(Ì .right, ½ , ¾ , splitValue) 11: else below the splitting point 12: if key(Ì .root) < splitValue then right child is black 13: RangeQueryColour(Ì .left, ½ , ¾ , splitValue) 14: report Ì .root 15: report all nodes in Ì .right 16: else left child is black 17: report all nodes in Ì .left 18: report Ì .root 19: RangeQueryColour(Ì .right, ½ , ¾ , splitValue)
  • 4. Range Queries 17-4 Quad-tree example * * * * * * * * * * * * * * * * * * * *
  • 5. Range Queries 17-5 kd-tree example 140 * 130 * 120 * 110 * 100 * 80 * 40 * 30 * 10 * 10 20 30 40 50 70 80 110 140 Ü ¼ Ý ½¾¼ Ý ¿¼ Ü ¿¼ Ü ½¼ Ü ½½¼ Ü ¼ Ý ½½¼ * * * * * * * * *
  • 6. Range Queries 17-6 kd-tree range queries Ü ¼ Ý ½¾¼ Ý ¿¼ Ü ¿¼ Ü ½¼ Ü ½½¼ Ü ¼ Ý ½½¼ * * * * * Ý ¼ * * * Ü ¼ * * * ¯ query range ¼ ¼℄ ¢ ¾¼ ¼℄
  • 7. Range Queries 17-7 Building kd-trees Algorithm Ë ÑÔÐ Ù Ð ÌÖ ( points È , int level ): 1: if È ½ then 2: Build one-node tree and return 3: else 4: if level is even then 5: Let ÜÑ = median Ü-coordinate among È 6: set È points in È with Ü-coord. ÜÑ 7: set È Ö points in È with Ü-coord. ÜÑ 8: else 9: [...] // Similarly with Ý -coordinate 10: ̽ = SimpleBuildkdTree(È , level ·½ ) 11: ̾ = SimpleBuildkdTree(È , level Ö ·½ ) 12: return tree with subtrees ̽ and ̾
  • 8. Range Queries 17-8 Building kd-trees with pre-sorting Algorithm ÈÖ ×ÓÖØ Ù Ð ÌÖ ( points È ): 1: Let ÈÜ be a copy of È , sort by Ü-coordinate 2: Let ÈÝ be a copy of È , sort by Ý -coordinate 3: buildKdTree(ÈÜ , ÈÝ , 0) Algorithm Ù Ð Ã ÌÖ ( points ÈÜ , points ÈÝ , level ): 1: // ÈÜ and ÈÝ are same points, sorted by Ü and Ý respectively 2: if ÈÜ ½ then 3: Build one-node tree and return 4: else 5: if level is even then 6: Let ÜÑ = median Ü-coordinate among È 7: set ÈÜ points in ÈÜ with Ü-coord. ÜÑ Ö 8: set ÈÜ points in ÈÜ with Ü-coord. ÜÑ Ö 9: set ÈÝ points in ÈÝ with Ü-coord. ÜÑ 10: set ÈÝ points in ÈÝ with Ü-coord. ÜÑ 11: // All sub-lists maintain their relative order. 12: else 13: [...] // Similarly with Ý -coordinate 14: ·½ ̽ = buildKdTree(ÈÜ , ÈÝ , level ) 15: Ö ·½ Ö Ì¾ = buildKdTree(ÈÜ , ÈÝ , level ) 16: return tree with subtrees ̽ and ̾
  • 9. Range Queries 17-9 Range trees BST by Ü-coordinate: (60,70) (20,50) (80,60) (10,80) (40,10) (70,90) (90,30) (30,40) (50,20) BST of subtree by Ý -coordinate: (30,40) (40,10) (20,50) (50,20) (10,80)