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

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 

二叉查找树范围查询算法

  • 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)