CMSC 341 K-D Trees.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
1 Trees 3: The Binary Search Tree Section Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CSED101 INTRODUCTION TO COMPUTING TREE 2 Hwanjo Yu.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
David Stotts Computer Science Department UNC Chapel Hill.
CMSC 341 K-D Trees. 8/3/2007 UMBC CSMC 341 KDTrees 2 K-D Tree Introduction  Multiple dimensional data Range queries in databases of multiple keys: Ex.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Multi-dimensional Search Trees CS302 Data Structures Modified from Dr George Bebis.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Binary Search Trees Chapter 7 Objectives
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Trees Chapter 11 (continued)
CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
Multiway Search Trees Data may not fit into main memory
Trees Chapter 11 (continued)
Binary Search Trees A binary search tree is a binary tree
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
BST Trees
Binary search tree. Removing a node
UNIT III TREES.
Binary Search Tree (BST)
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Trees (Chapter 4) Binary Search Trees - Review Definition
ITEC 2620M Introduction to Data Structures
Tree data structure.
KD Tree A binary search tree where every node is a
Binary Search Trees.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Trees 3: The Binary Search Tree
AVL Trees CENG 213 Data Structures.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Tree data structure.
Search Sorted Array: Binary Search Linked List: Linear Search
CMSC 341 K-D Trees.
CMSC 341 K-D Trees.
CMSC 341 K-D Trees.
Non-Linear Structures
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Chapter 20: Binary Trees.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Mark Redekopp David Kempe
CMSC 341 K-D Trees.
CMSC 341 K-D Trees.
Balanced search trees: 2-3 trees.
Presentation transcript:

CMSC 341 K-D Trees

K-D Tree Introduction Multiple dimensional data Range queries in databases of multiple keys: Ex. find persons with 34  age  49 and $100k  annual income  $150k GIS (geographic information system) Computer graphics Extending BST from one dimensional to k-dimensional It is a binary tree Organized by levels (root is at level 0, its children level 1, etc.) Tree branching at level 0 according to the first key, at level 1 according to the second key, etc. KdNode Each node has a vector of keys, in addition to the two pointers to its left and right subtrees.

K-D Tree A 2-D tree example 53, 14 65, 51 27, 28 70, 3 99, 90 30, 11 31, 85 82, 64 29, 16 40, 26 7, 39 32, 29 38, 25 73, 75 15, 61 A 2-D tree example

K-D Tree Operations Insert A 2-D item (vector of size 2 for the two keys) is inserted New node is inserted as a leaf Different keys are compared at different levels Find/print with an orthogonal (square) range exact match: insert (low[level] = high[level] for all levels) partial match: (query ranges are given to only some of the k keys, other keys can be thought in range  ) low[0] low[1] high[0] high[1]

K-D Tree Insertion template <class Comparable> void KdTree <Comparable>::insert(const vector<Comparable> &x) { insert( x, root, 0); } void KdTree <Comparable>:: insert(const vector<Comparable> &x, KdNode * & t, int level) if (t == NULL) t = new KdNode(x); else if (x[level] < t->data[level]) insert(x, t->left, 1 – level); else insert(x, t->right, 1 – level);

Insert (55, 62) into the following 2-D tree 53, 14 65, 51 27, 28 70, 3 99, 90 30, 11 31, 85 32, 29 82, 64 29, 16 40, 26 7, 39 38, 23 55,62 73, 75 15, 61

K-D Tree: printRange /** * Print items satisfying * low[0] <= x[0] <= high[0] and * low[1] <= x[1] <= high[1] */ template <class Comparable> void KdTree <Comparable>:: printRange(const vector<Comparable> &low, const vector<Comparable> & high) const { printRange(low, high, root, 0); }

K-D Tree: printRange (cont’d) template <class Comparable> void KdTree <Comparable>:: printRange(const vector<Comparable> &low, const vector<Comparable> &high, KdNode * t, int level) { if (t != NULL) if (low[0] <= t->data[0] && high[0] >= t->data[0] && low[1] <= t->data[1] && high[1] >= t->data[1]) cout << “(” << t->data[0] ”,” << t->data[1] << “)” << endl; if (low[level] <= t->data[level]) printRange(low, high, t->left, 1 – level); if (high[level] >= t->data[level]) printRange(low, high, t->right, 1 – level); }

printRange in a 2-D Tree low[0] = 35, high[0] = 40; In range? If so, print cell Low[level]<=data[level]->search t->left High[level] >= data[level]=> search t->right 53, 14 65, 51 70, 3 99, 90 82, 64 73, 75 27, 28 30, 11 31, 85 29, 16 40, 26 7, 39 32, 29 38, 25 15, 61 low[0] = 35, high[0] = 40; This subtree is never searched low[1] = 25, high[1] = 30; Searching is “preorder”. Efficiency is obtained by “pruning” subtrees from the search.

K-D Tree Performance Insert Average and balanced trees: O(lg N) Worst case: O(N) Print/search with a square range query Exact match: same as insert (low[level] = high[level] for all levels) Range query: for M matches Perfectly balanced tree: K-D trees: O(M + kN (1-1/k) ) 2-D trees: O(M + N) Partial match in a random tree: O(M + N) where  = (-3 + 17) / 2

K-D Tree Performance Consider one boundary of the square (say, low[0]) More on range query in a perfectly balanced 2-D tree: Consider one boundary of the square (say, low[0]) Let T(N) be the number of nodes to be looked at with respect to low[0]. For the current node, we may need to look at One of the two children (e.g., node (27, 28), and Two of the four grand children (e.g., nodes (30, 11) and (31, 85). Write T(N) = 2 T(N/4) + c, where N/4 is the size of subtrees 2 levels down (we are dealing with a perfectly balanced tree here), and c = 3. Solving this recurrence equation: T(N) = 2T(N/4) + c = 2(2T(N/16) + c) + c … = c(1 + 2 +  + 2^(log4 N) = 2^(1+ log4 N) – 1 = 2*2^(log4 N) – 1 = 2^ ((log2 N)/2) – 1 = O(N)

K-D Tree Remarks Remove No good remove algorithm beyond lazy deletion (mark the node as removed) Balancing K-D Tree No known strategy to guarantee a balanced 2-D tree Tree rotation does not work here Periodic re-balance Extending 2-D tree algorithms to k-D Cycle through the keys at each level