🖥️
Sunil Notebook
Interview Preparation
  • 📒Notebook
    • What is this about ?
  • System Design
    • 💡Key Concepts
      • 🌐Scalability
      • 🌐Latency Vs Throughput
      • 🌐Databases
      • 🌐CAP Theorem
      • 🌐ACID Transactions
      • 🌐Rate limiting
      • 🌐API Design
      • 🌐Strong Vs eventual consistency
      • 🌐Distributed tracing
      • 🌐Synchronous Vs asynchronous Communication
      • 🌐Batch Processing Vs Stream Processing
      • 🌐Fault Tolerance
    • 💎Building Blocks
      • 🔹Message
      • 🔹Cache
      • 🔹Load Balancer Vs API Gateway
    • 🖥️Introduction to system design
    • ⏱️Step By Step Guide
    • ♨️Emerging Technologies in System Design
    • ☑️System design component checklist
      • 🔷Azure
      • 🔶AWS
      • ♦️Google Cloud
    • 🧊LinkedIn feed Design
    • 🏏Scalable Emoji Broadcasting System - Hotstar
    • 💲UPI Payment System Design
    • 📈Stock Broker System Design - Groww
    • 🧑‍🤝‍🧑Designing Instagram's Collaborative Content Creation - Close Friends Only
    • 🌳Vending Machines - Over the air Systems
    • Reference Links
  • DSA
    • Topics
      • Introduction
      • Algorithm analysis
        • Asymptotic Notation
        • Memory
      • Sorting
        • Selection Sort
        • Insertion Sort
        • Merge Sort
        • Quick Sort
        • Quick'3 Sort
        • Shell Sort
        • Shuffle sort
        • Heap Sort
        • Arrays.sort()
        • Key Points
        • Problems
          • Reorder Log files
      • Stacks and Queues
        • Stack Implementations
        • Queue Implementations
        • Priority Queues
        • Problems
          • Dijkstra's two-stack algorithm
      • Binary Search Tree
        • Left Leaning Red Black Tree
          • Java Implementations
        • 2-3 Tree
          • Search Operation - 2-3 Tree
          • Insert Operation - 2-3 Tree
        • Geometric Applications of BST
      • B-Tree
      • Graphs
        • Undirected Graphs
        • Directed Graphs
        • Topological Sort
      • Union Find
        • Dynamic Connectivity
        • Quick Find - Eager Approach
        • Quick Find - Lazy Approach
        • Defects
        • Weighted Quick Union
        • Quick Union + path comparison
        • Amortized Analysis
      • Convex Hull
      • Binary Heaps and Priority Queue
      • Hash Table vs Binary Search Trees
  • Concurrency and Multithreading
    • Introduction
    • Visibility Problem
    • Interview Questions
    • References
      • System design
  • Design Patterns
    • ℹ️Introduction
    • 💠Classification of patterns
    • 1️⃣Structural Design Patterns
      • Adapter Design Pattern
      • Bridge Design Pattern
      • Composite Design Pattern
      • Decorator Design Pattern
      • Facade Design Pattern
      • Flyweight Design Pattern
      • Private Class Data Design Pattern
      • Proxy Design Pattern
    • 2️⃣Behavioral Design Patterns
      • Chain Of Responsibility
      • Command Design Pattern
      • Interpreter Design Pattern
      • Iterator Design Pattern
      • Mediator Design Pattern
      • Memento Design Pattern
      • Null Object Design Pattern
      • Observer Design Pattern
      • State Design Pattern
      • Strategy Design Pattern
      • Template Design Pattern
    • 3️⃣Creational Design Patterns
      • Abstract Factory Design Pattern
      • Builder Design Pattern
      • Factory Method Design Pattern
      • Object Pool Design Pattern
      • Prototype Design Pattern
      • Singleton Design Pattern
    • Java Pass by Value or Pass by Reference
  • Designing Data-Intensive Applications - O'Reilly
    • Read Me
    • 1️⃣Reliable, Scalable, and Maintainable Applications
      • Reliability
      • Scalability
      • Maintainability
      • References
    • 2️⃣Data Models and Query Languages
      • Read me
      • References
    • Miscellaneous
  • Preparation Manual
    • Disclaimer
    • What is it all about?
    • About a bunch of links
    • Before you start preparing
    • Algorithms and Coding
    • Concurrency and Multithreading
    • Programming Language and Fundementals
    • Best Practices and Experience
  • Web Applications
    • Typescript Guidelines
  • Research Papers
    • Research Papers
      • Real-Time Data Infrastructure at Uber
      • Scaling Memcache at Facebook
  • Interview Questions
    • Important links for preparation
    • Google Interview Questions
      • L4
        • Phone Interview Questions
      • L3
        • Interview Questions
      • Phone Screen Questions
  • Miscellaneous
    • 90 Days Preparation Schedule
    • My Preparation for Tech Giants
    • Top Product Based Companies
  • Links
    • Github
    • LinkedIn
Powered by GitBook
On this page
  • 1-D Range Search
  • BST Implementation
  • Line Segment Intersection
  • Sweep-line algorithm
  • 1D Interval Search Trees
  • Algorithm
  • Java Implementation
  • Interval Search Tree Analysis
  • Orthogonal rectangle intersection
  • Algorithm
  • Summary

Was this helpful?

Edit on GitHub
  1. DSA
  2. Topics
  3. Binary Search Tree

Geometric Applications of BST

PreviousInsert Operation - 2-3 TreeNextB-Tree

Last updated 3 years ago

Was this helpful?

1-D Range Search

This is the extension for symbol table.

  • Insert key-value pair.

  • Search for key k.

  • Delete key k.

  • Range search: Find all keys between k1 & k2.

  • Range count: Number of keys between k1 & k2.

Implementation

  • Un Ordered List: Fast Insertion and slow search

  • Ordered List: Slow insert, binary search for k1 and k2 to do range search.

BST Implementation

Example1:

Java code to find the number of keys between low and high

// Running time is proportional to log(n)
public int size(key low, key high) {
    if(contains(high)){
        return rank(high) - rank(low) + 1;
    } else {
        return rank(high) - rank(low);
    }
}

Example 2:

Line Segment Intersection

Given N horizontal and vertical line segments, find all intersections.

Sweep-line algorithm

Sweep vertical line between left to right and record your observations

Algorithm:

  • x-coordinates define events.

  • h-segment (left endpoint): insert y-coordinate into BST.

  • h-segment (left endpoint): insert y-coordinate into BST.

  • v-segment: Range search for interval of y-end points.

Explanation:

  • Put x-coordinate in PQ (or Sort ) . -------------> N * log(N)

  • Insert y-coordinate into the BST . -------------> N * log(N)

  • Remove y-coordinate from the BST -------------> N * log(N)

  • Range Searches in BST -------------> N * log(N) + R

1D Interval Search Trees

Data structure to hold the set of overlapping intervals.

Create BST, where each node stores an interval (lo, hi).

  • Use left endpoint as BST key.

  • Store max endpoint in subtree rooted at node.

Algorithm

  • If interval in node intersects query interval, return it.

  • Else if left subtree is null, go right.

  • Else if max endpoint in left subtree is less than lo, go right.

  • Else go left.

Java Implementation

..............

 TreeNode x = root;
 
 while(x!=null){
     if(x.interval.insersects(lo,hi)){
         return x.interval;
     }else if(x.left == null) {
         x = x.right;
     }else if(x.left.max < lo){
         x = x.right;
     }else {
         x = x.left;
     }
 }

return null;

..............

Interval Search Tree Analysis

Implementation: Use a red-black BST ( easy to maintain auxiliary information using log N extra work per op ) to guarantee performance.

Orthogonal rectangle intersection

Goal: Find all intersections among a set of N orthogonal rectangles.

Here we use the same principle sweep line algorithm. Sweep one vertical line from left to right. when you encounter left end point insert y interval. and repeat the interval search until you get the right end point.

Algorithm

  • x-coordinates of left and right endpoints define events.

  • Maintain set of rectangles that intersect the sweep line in an interval search tree (using y-intervals of rectangle).

  • Left endpoint: Interval search for y-interval of rectangle; insert y-interval.

  • Right endpoint: Remove y-interval.

Analysis

  • Put x-coordinate in PQ (or Sort ) . -------------> N * log(N)

  • Insert y-coordinate into the BST . -------------> N * log(N)

  • Reomve y-coordinate from the BST -------------> N * log(N)

  • Interval Search for y Coordinates -------------> N * log(N) + R * log(N)

Summary

order of growth of running time for N intervals
Orthogonal Rectangle Intersection