Multi-dimensional Search Trees

COMP9315 21T1 ♢ Nd Search Trees ♢ [0/16]
❖ Multi-dimensional Tree Indexes

Over the last 20 years, from a range of problem areas

Consider three popular schemes:   kd-trees, Quad-trees, R-trees.

Example data for multi-d trees is based on the following relation:

create table Rel (
    X char(1) check (X between 'a' and 'z'),
    Y integer check (Y between 0 and 9)
);

COMP9315 21T1 ♢ Nd Search Trees ♢ [1/16]
❖ Multi-dimensional Tree Indexes (cont)

Example tuples:

R('a',1)  R('a',5)  R('b',2)  R('d',1)
R('d',2)  R('d',4)  R('d',8)  R('g',3)
R('j',7)  R('m',1)  R('r',5)  R('z',9)

The tuple-space for the above tuples:

[Diagram:Pics/select/2d-space.png]

COMP9315 21T1 ♢ Nd Search Trees ♢ [2/16]
❖ kd-Trees

kd-trees are multi-way search trees where

[Diagram:Pics/select/kd-tree.png]

COMP9315 21T1 ♢ Nd Search Trees ♢ [3/16]
❖ kd-Trees (cont)

How this tree partitions the tuple space:

[Diagram:Pics/select/kd-tree-space.png]

COMP9315 21T1 ♢ Nd Search Trees ♢ [4/16]
❖ Searching in kd-Trees

// Started by Search(Q, R, 0, kdTreeRoot)
Search(Query Q, Relation R, Level L, Node N)
{
   if (isDataPage(N)) {
      Buf = getPage(fileOf(R),idOf(N))
      check Buf for matching tuples
   } else {
      a = attrLev[L]
      if (!hasValue(Q,a))
         nextNodes = all children of N
      else {
         val = getAttr(Q,a)
         nextNodes = find(N,Q,a,val)
      }
      for each C in nextNodes
         Search(Q, R, L+1, C)
}  }

COMP9315 21T1 ♢ Nd Search Trees ♢ [5/16]
❖ Quad Trees

Quad trees use regular, disjoint partitioning of tuple space.

Example:

[Diagram:Pics/select/quad-tree-space.png]

COMP9315 21T1 ♢ Nd Search Trees ♢ [6/16]
❖ Quad Trees (cont)

Basis for the partitioning:

Note: effective for d≤5, ok for 6≤d≤10, ineffective for d>10
COMP9315 21T1 ♢ Nd Search Trees ♢ [7/16]
❖ Quad Trees (cont)

The previous partitioning gives this tree structure, e.g.

[Diagram:Pics/select/quad-tree1.png]

In this and following examples, we give coords of top-left,bottom-right of a region

COMP9315 21T1 ♢ Nd Search Trees ♢ [8/16]
❖ Searching in Quad-trees

Space query example:

[Diagram:Pics/select/quad-query.png]

Need to traverse: red(NW), green(NW,NE,SW,SE), blue(NE,SE).

COMP9315 21T1 ♢ Nd Search Trees ♢ [9/16]
❖ Searching in Quad-trees (cont)


Method for searching in Quad-tree:


Note that query region may be a single point.
COMP9315 21T1 ♢ Nd Search Trees ♢ [10/16]
❖ R-Trees

R-trees use a flexible, overlapping partitioning of tuple space.

Overlap and partial cover means: Aim: height-balanced, partly-full index pages   (cf. B-tree)
COMP9315 21T1 ♢ Nd Search Trees ♢ [11/16]
❖ R-Trees (cont)

[Diagram:Pics/select/r-tree.png]

COMP9315 21T1 ♢ Nd Search Trees ♢ [12/16]
❖ Insertion into R-tree

Insertion of an object R occurs as follows:

Note that R may be a point or a polygon.
COMP9315 21T1 ♢ Nd Search Trees ♢ [13/16]
❖ Query with R-trees

Designed to handle space queries and "where-am-I" queries.

"Where-am-I" query: find all regions containing a given point P:

Space (region) queries are handled in a similar way
COMP9315 21T1 ♢ Nd Search Trees ♢ [14/16]
❖ Costs of Search in Multi-d Trees

Cost depends on type of query and tree structure

Best case: pmr query where all attributes have known values

Typical case: some attributes are unknown or defined by range
COMP9315 21T1 ♢ Nd Search Trees ♢ [15/16]
❖ Multi-d Trees in PostgreSQL

Up to version 8.2, PostgreSQL had R-tree implementation

Superseded by GiST = Generalized Search Trees

GiST indexes parameterise: data type, searching, splitting

GiST trees have the following structural constraints: Details: Chapter 64 in PG Docs  or  src/backend/access/gist
COMP9315 21T1 ♢ Nd Search Trees ♢ [16/16]


Produced: 18 Mar 2021