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

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

二叉查找树范围查询算法

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