Introduction to kd-trees

Similar documents
Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Analysis of Algorithms I: Binary Search Trees

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Binary Search Trees 3/20/14

Ordered Lists and Binary Trees

Binary Heaps. CSE 373 Data Structures

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

TREE BASIC TERMINOLOGIES

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Big Data and Scripting. Part 4: Memory Hierarchies

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Algorithms Chapter 12 Binary Search Trees

Converting a Number from Decimal to Binary

6 March Array Implementation of Binary Trees

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Data Structures and Algorithms

Binary Search Trees (BST)

Chapter 14 The Binary Search Tree

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

How To Create A Tree From A Tree In Runtime (For A Tree)

Full and Complete Binary Trees

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Data Structures Fibonacci Heaps, Amortized Analysis

Binary Search Trees CMPSC 122

Symbol Tables. Introduction

From Last Time: Remove (Delete) Operation

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

R-trees. R-Trees: A Dynamic Index Structure For Spatial Searching. R-Tree. Invariants

Data Warehousing und Data Mining

DATA STRUCTURES USING C

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Binary Trees and Huffman Encoding Binary Search Trees

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Binary Heap Algorithms

Analysis of a Search Algorithm

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Heaps & Priority Queues in the C++ STL 2-3 Trees

Lecture Notes on Binary Search Trees

CSE 326: Data Structures B-Trees and B+ Trees

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Data Structure [Question Bank]

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

Data Structures. Level 6 C Module Descriptor

Data Structures and Data Manipulation

DATABASE DESIGN - 1DL400

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Algorithms and Data Structures

Introduction to Data Structures and Algorithms

Persistent Data Structures and Planar Point Location

CS711008Z Algorithm Design and Analysis

Cpt S 223. School of EECS, WSU

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Algorithms and Data Structures

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Algorithms and Data Structures Written Exam Proposed SOLUTION

Data Structures and Algorithms Written Examination

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

In-Memory Database: Query Optimisation. S S Kausik ( ) Aamod Kore ( ) Mehul Goyal ( ) Nisheeth Lahoti ( )

Why Use Binary Trees?

Chapter 13: Query Processing. Basic Steps in Query Processing

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

A Comparison of Dictionary Implementations

The ADT Binary Search Tree

Binary Search Trees. Ric Glassey

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

OPTIMAL BINARY SEARCH TREES

Lecture Notes on Binary Search Trees

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Sample Questions Csci 1112 A. Bellaachia

Analysis of Binary Search algorithm and Selection Sort algorithm

PES Institute of Technology-BSC QUESTION BANK

Introduction to Object-Oriented Programming

Rotation Operation for Binary Search Trees Idea:

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

International Journal of Software and Web Sciences (IJSWS)

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Binary search algorithm

Vector storage and access; algorithms in GIS. This is lecture 6

Introduction to data structures

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Algorithms. Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search line segment intersection kd trees interval search trees rectangle intersection

10CS35: Data Structures Using C

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Persistent Data Structures

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Transcription:

Introduction to kd-trees Page 1 of 100 Dimension of data is k (but common to say k-d tree of dimension 3 instead of 3d-tree). kd-trees are binary trees Designed to handle spatial data in a simple way For n points, O(n) space, O(log n) height (if balanced), supports range and nearest-neighbor queries. Node consists of Two child pointers, Satellite information (such as name). A key: Either a single float representing a coordinate value, or a pair of floats (representing a dimension of a rectangle)

Page 2 of 100 Construct a binary tree Basic Idea Behind kd-trees At each step, choose one of the coordinate as a basis of dividing the rest of the points For example, at the root, choose x as the basis Like binary search trees, all items to the left of root will have the x-coordinate less than that of the root All items to the right of the root will have the x-coordinate greater than (or equal to) that of the root Choose y as the basis for discrimination for the root s children And choose x again for the root s grandchildren Note: Equality (corresponding to right child) is significant

Page 3 of 100 Example: Construct kd-tree Given Points Coordinates of points are (35, 90), (70, 80), (10, 75) (80, 40), (50, 90), (70, 30), (90, 60), (50, 25), (25, 10), (20, 50), and (60, 10) Points may be given one a time, or all at once. Data best visualized as shown below

Example: kdtree Insertion Page 4 of 100

Page 5 of 100 Building: Dynamic Insertion KDNode i n s e r t ( point p, KDNode t, int cd ) { i f ( t == n u l l ) t = new KDNode ( p ) ; // s e t s up node. data. x and node. data. y else i f ( p == t. data )... // d u p l i c a t e else i f ( p. cd < t. data. cd ) t. l e f t = i n s e r t ( p, t. l e f t, cd +1); else t. r i g h t = i n s e r t ( p, t. r i g h t, cd +1); return t ; } Initial call: root = insert (p, root, 0); Each node is associated with a rectangular region Tree is balanced if points are given in random order Or if all points are given in advance

Building: The Static Case Page 6 of 100 Assume points are sorted on both x and y in a composite array S S[x] corresponds to a list of points sorted by x. KDNode buildtree ( SortedArray S, int cd ) { i f ( S. empty ( ) ) return n u l l else i f S. s i n g l e t o n ( ) return new KDNode(S [ x ] [ 0 ], cd ) ; else { m = median ( S, cd ) // median ( c u t t i n g dimension ) l e f t = l e f t P o i n t s (S, cd ) ; r i g h t = S l e f t ; t = new KDNode(m) ; t. l e f t = buildtree ( l e f t, cd +1); t. r i g h t = buildtree ( r i g h t, cd +1); return t } } T (n) = kn + 2T (n/2), so the algorithm takes O(n log n) time.

Remove Requires Finding Minimum Page 7 of 100 Given a node, and a cutting dimension, find the node with minimum value (with respect to that cutting dimension) Point findmin ( KDNode t, int whichaxis, int cd ) { i f ( t == n u l l ) return n u l l ; else i f ( whichaxis == cd ) i f ( t. l e f t == n u l l ) return t. data ; } else return findmin ( t. l e f t, whichaxis, cd+1) else return minimum( t. data, findmin ( t. l e f t, whichaxis, cd +1), findmin ( t. r i g h t, whichaxis, cd +1), i ) ; If tree is balanced, findmin (root) takes no more than O( n) time in the worst case.

Example: findmin(root, x, y) Page 8 of 100

Basic Idea Behind Removing Page 9 of 100 Want to remove point p = (a, b) First find node t which has this point Node t discriminates on x (say) If t is a leaf node, replace it by null Otherwise, find a replacement node r with coordinates (c, d) Replace the data at t by (c, d). The kd-tree structure must not be violated Recursively remove point p = (c, d) Finding the replacement If t has a right child, use the inorder successor Otherwise minimum value of the left child is appropriately used

Remove Example: Delete Point At Root Page 10 of 100

Remove Example: Delete Point Page 11 of 100

Remove Example: Delete Point Page 12 of 100

Page 13 of 100 Remove Takes O(log n) Time KDNode remove ( KDNode t, Point p, int cd ) { i f ( t == n u l l ) return n u l l ; else i f (p. cd < t. data ) t. l e f t = remove ( t. l e f t, p, cd +1); else i f (p. cd > t. data ) t. r i g h t = remove ( t. r i g h t, p, cd +1); else { i f ( t. r i g h t == n u l l && t. l e f t == n u l l ) return n u l l ; i f ( t. r i g h t! = n u l l ) t. data = findmin ( t. r i g h t, cd, cd +1); else { t. data = findmin ( t. l e f t, cd, cd +1); t. l e f t = n u l l ; } t. r i g h t = remove ( t. r i g h t, t. data, cd +1); return t ; }} We expect to delete nodes at leaf level. If tree is balanced, we expect remove() to take O(log n) time

Remove Example: Delete Point At Root Page 14 of 100

Remove: Solution Page 15 of 100

Page 16 of 100