CSE 326 MultiDimensional Search Trees David Kaplan Dept




































- Slides: 36
CSE 326 Multi-Dimensional Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001
Outline § § Multidimensional search ADT Range Queries k-D Trees Quad Trees Multi-Dimensional Search. Trees CSE 326 Autumn 2001 2
Multi-D Search ADT 5, 2 § Dictionary operations § § § create destroy find insert delete range queries 2, 5 4, 4 4, 2 8, 4 1, 9 3, 6 8, 2 5, 7 9, 1 § Each item has k keys for a k-dimensional search tree § Searches can be performed on one, some, or all of the keys or on ranges of the keys Multi-Dimensional Search. Trees CSE 326 Autumn 2001 3
Applications of Multi-D Search § § § § Astronomy (simulation of galaxies) - 3 dimensions Protein folding in molecular biology - 3 dimensions Lossy data compression - 4 to 64 dimensions Image processing - 2 dimensions Graphics - 2 or 3 dimensions Animation - 3 to 4 dimensions Geographical databases - 2 or 3 dimensions Web searching - 200 or more dimensions Multi-Dimensional Search. Trees CSE 326 Autumn 2001 4
Range Query Range query § search in which exact key may not be entirely specified § primary interface to multi-D data structures Examples: § “All stars in the Milky Way within 1000 light years of the Sun. ” § “All customers named ‘Smith’ who spent from $100 -500 with us last year. ” Multi-Dimensional Search. Trees CSE 326 Autumn 2001 5
Two-Dimensional Range Query Examples Search for items based on … single key range multiple key ranges function of multiple keys Multi-Dimensional Search. Trees CSE 326 Autumn 2001 6
Range Querying in 1 -D x Find everything in the rectangle… Multi-Dimensional Search. Trees CSE 326 Autumn 2001 7
Range Querying in 1 -D with a BST Find everything in the rectangle… x Multi-Dimensional Search. Trees CSE 326 Autumn 2001 8
1 -D Range Querying in 2 -D y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 9 x
2 -D Range Querying in 2 -D y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 10 x
k-D Trees § Split on the next dimension at each succeeding level § If building in batch choose the median along the current dimension at each level § guarantees logarithmic height and balanced tree § common scenario: large, (relatively) static data sets § In general, add as in a BST k-D tree node keys value dimension The dimension that this node splits on left right Multi-Dimensional Search. Trees CSE 326 Autumn 2001 11
Building a 2 -D Tree (1/4) y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 12 x
Building a 2 -D Tree (2/4) y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 13 x
Building a 2 -D Tree (3/4) y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 14 x
Building a 2 -D Tree (4/4) y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 15 x
k-D Tree Data Structure a b d c e f j g i k e h l m g f Multi-Dimensional Search. Trees b h k a j CSE 326 Autumn 2001 d c l i 16 m
2 -D Range Querying in 2 -D Trees y x Search every partition that intersects the rectangle. Multi-Dimensional Search. Trees CSE(including 326 Autumn 2001 17 Check whether each node leaves) falls into the range.
Other Shapes for Range Querying y x Search every partition that intersects the shape (circle). Multi-Dimensional Search. Trees CSE(including 326 Autumn 2001 18 Check whether each node leaves) falls into the shape.
Find in a k-D Tree Finds the node which has the given set of keys in it or returns null if there is no such node Node *& find(const key. Vector & keys, Node *& root) { int dim = root->dimension; if (root == NULL) return root; else if (root->keys == keys) return root; else if (keys[dim] < root->keys[dim]) return find(keys, root->left); else return find(keys, root->right); } Runtime: Multi-Dimensional Search. Trees CSE 326 Autumn 2001 19
Breathalyzer Test: k-D Trees Can Lose Their Balance 1 insert(<5, 0>) insert(<6, 9>) insert(<9, 3>) insert(<6, 5>) insert(<7, 7>) insert(<8, 6>) 5, 0 6, 9 9, 3 Worst-case imbalance? 6, 5 7, 7 1 but not when built in batch! Multi-Dimensional Search. Trees CSE 326 Autumn 2001 8, 6 20
k-D Find Example Actual Find § find(<3, 6>) Insert (first, do a Find) § find(<0, 10>) 5, 2 8, 4 2, 5 1, 9 4, 4 4, 2 Multi-Dimensional Search. Trees CSE 326 Autumn 2001 8, 2 3, 6 5, 7 9, 1 21
Quad Trees § Split on all (two) dimensions at each level § Split key space into equal size partitions (quadrants) § Add a new node by adding to a leaf, and, if the leaf is already occupied, split until only one node per leaf Center quadrants quad tree node 0, 1 1, 1 keys value 0, 0 1, 0 Multi-Dimensional Search. Trees Center: x y Quadrants: 0, 0 1, 0 0, 1 1, 1 CSE 326 Autumn 2001 22
Building a Quad Tree (1/5) y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 23 x
Building a Quad Tree (2/5) y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 24 x
Building a Quad Tree (3/5) y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 25 x
Building a Quad Tree (4/5) y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 26 x
Building a Quad Tree (5/5) y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 27 x
Quad Tree Data Structure a e c b a g d f d g b Multi-Dimensional Search. Trees e CSE 326 Autumn 2001 c 28 f
2 -D Range Querying in Quad Trees y Multi-Dimensional Search. Trees CSE 326 Autumn 2001 29 x
Find in a Quad Tree § Finds the node which has the given pair of keys in it or returns quadrant where the point should be if there is no such node Node *& find(Key x, Key y, Node *& root) { if (root == NULL) return root; // Empty tree if (root->is. Leaf) return root; // Key may not actually be here // Compare vs. center; always make same choice on ties int quad = get. Quadrant(x, y, root); return find(x, y, root->quadrants[quad]); } Multi-Dimensional Search. Trees CSE 326 Autumn 2001 Runtime? 30
Walk this straight line, please … Quad Trees Can Lose Their Balance 1 Worst-case imbalance? • O(log(1/dmin)) a b 1 but Multi-Dimensional Search. Trees CSE 326 Autumn 2001 no batch bail-out – why not? 31
Find Example find(<10, 2>) (i. e. , c) find(<5, 6>) (i. e. , d) a e c b a g d f d g b Multi-Dimensional Search. Trees e CSE 326 Autumn 2001 c 32 f
Insert Example insert(<10, 7>, x) a c b a f x g … … e d g • Find the spot where the node should go. • If the space is unoccupied, insert the node. x • If it is occupied, split until the existing node Multi-Dimensional Search. Trees CSE 326 Autumn 2001 separates from the new one. g 33
Delete Example delete(<10, 2>)(i. e. , c) a c b a e g d f d g • Find and delete the node. b • If its parent has just one child, delete it. • Multi-Dimensional Propagate!Search. Trees CSE 326 Autumn 2001 e c 34 f
Nearest Neighbor Search get. Nearest. Neighbor(<1, 4>) a c b a e g d f g • Find a nearby node (do a find). b c • Do a circular range query. • As you get results, tighten the circle. CSE 326 node Autumn 2001 • Multi-Dimensional Continue Search. Trees until no closer in query. d e f Works on 35 k-D Trees, too!
Quad Trees vs. k-D Trees § § Density balanced trees Number of nodes is O(n) where n is the number of points Height of the tree is O(log n) with batch insertion Supports insert, find, nearest neighbor, range queries Quad Trees § Number of nodes is O(n(1+ log( /n))) where n is the number of points and is the ratio of the width (or height) of the key space and the smallest distance between two points § Height of the tree is O(log n + log ) § Supports insert, delete, find, nearest neighbor, range queries Also: many hybrids and variants Multi-Dimensional Search. Trees CSE 326 Autumn 2001 36