CMSC 341 K-D Trees.

Slides:



Advertisements
Similar presentations
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Advertisements

Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
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.
Data Structures Using C++1 Chapter 11 Binary Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
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,
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
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.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
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.
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.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Trees - Part II © Dave Bockus. 2 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k.
Binary Search Trees (BST)
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
CMSC 341 Binary Search Trees. 8/3/2007 UMBC CMSC 341 BST 2 Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the values.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Search Trees Chapter 7 Objectives
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
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
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
BST Trees
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
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.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Tree data structure.
Search Sorted Array: Binary Search Linked List: Linear Search
CSE 373: Data Structures and Algorithms
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 &
CMSC 341 K-D Trees.
Basic Data Structures - Trees
Binary Search Trees.
AVL Tree Chapter 6 (cont’).
CMSC 341 K-D Trees.
Tree.
Search Sorted Array: Binary Search Linked List: Linear Search
CMSC 341 Binary Search Trees 8/3/2007 CMSC 341 BST.
CMSC 341 K-D Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

CMSC 341 K-D Trees

K-D Tree Introduction KdNode 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 pointers to its subtrees. 8/3/2007 UMBC CSMC 341 KDTrees

K-D Tree A 2-D tree example 8/3/2007 UMBC CSMC 341 KDTrees

2-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 (rectangular) 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  ) high[1] key[1] low[1] key[0] low[0] high[0] 8/3/2007 UMBC CSMC 341 KDTrees

2-D Tree Insertion public void insert(Vector <T> x) { root = insert( x, root, 0); } // this code is specific for 2-D trees private KdNode<T> insert(Vector <T> x, KdNode<T> t, int level) if (t == null) t = new KdNode(x); int compareResult = x.get(level).compareTo(t.data.get(level)); if (compareResult < 0) t.left = insert(x, t.left, 1 - level); else if( compareResult > 0) t.right = insert(x, t.right, 1 - level); else ; // do nothing if equal return t; 8/3/2007 UMBC CSMC 341 KDTrees

Insert (55, 62) into the following 2-D tree 55 > 53, move right 53, 14 62 > 51, move right 65, 51 27, 28 70, 3 99, 90 30, 11 31, 85 55 < 99, move left 82, 64 29, 16 40, 26 7, 39 32, 29 38, 23 55,62 73, 75 15, 61 62 < 64, move left Null pointer, attach 8/3/2007 UMBC CSMC 341 KDTrees

2-D Tree: printRange /** * Print items satisfying * lowRange.get(0) <= x.get(0) <= highRange.get(0) * and * lowRange.get(1) <= x.get(1) <= highRange.get(1) */ public void printRange(Vector <T> lowRange, Vector <T>highRange) { printRange(lowRange, highRange, root, 0); } 8/3/2007 UMBC CSMC 341 KDTrees

2-D Tree: printRange (cont.) private void printRange(Vector <T> low,Vector <T> high, KdNode<T> t, int level) { if (t != null) if ((low.get(0).compareTo(t.data.get(0)) <= 0 && t.data.get(0).compareTo(high.get(0)) <=0) &&(low.get(1).compareTo(t.data.get(1)) <= 0 && t.data.get(1).compareTo(high.get(1)) <= 0)) System.out.println("(" + t.data.get(0) + "," + t.data.get(1) + ")"); if (low.get(level).compareTo(t.data.get(level)) <= 0) printRange(low, high, t.left, 1 - level); if (high.get(level).compareTo(t.data.get(level)) >= 0) printRange(low, high, t.right, 1 - level); } 8/3/2007 UMBC CSMC 341 KDTrees

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 7, 39 15, 61 40, 26 32, 29 29, 16 38, 23 low[0] = 35, high[0] = 40; This sub-tree is never searched. low[1] = 23, high[1] = 30; Searching is “preorder”. Efficiency is obtained by “pruning” subtrees from the search. 8/3/2007 UMBC CSMC 341 KDTrees

3-D Tree example 20,12,30 X < 20 X > 20 15,18,27 40,12,39 Y < 18 Y > 18 Y < 12 Y > 12 17,16,22 19,19,37 22,10,33 25,24,10 Z < 22 Z < 33 Z > 33 16,15,20 24,9,30 50,11,40 X < 16 X > 16 A B C D 12,14,20 18,16,18 What property (or properties) do the nodes in the subtrees labeled A, B, C, and D have? 8/3/2007 UMBC CSMC 341 KDTrees

K-D Operations Modify the 2-D insert code so that it works for K-D trees. Modify the 2-D printRange code so that it works for K-D trees. 8/3/2007 UMBC CSMC 341 KDTrees

K-D Tree Performance Insert Print/search with a square range query 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 8/3/2007 UMBC CSMC 341 KDTrees

K-D Tree Performance 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*2^ ((log2 N)/2) – 1 = O(N) 8/3/2007 UMBC CSMC 341 KDTrees

K-D Tree Remarks Remove Balancing K-D Tree 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 Periodic re-balance Extending 2-D tree algorithms to k-D Cycle through the keys at each level 8/3/2007 UMBC CSMC 341 KDTrees