PR Quadtree Implementation

Similar documents
PR quadtree. public class prquadtree< T extends Compare2D<? super T> > {

CS 3114 Data Structures and Algorithms DRAFT Minor Project 3: PR Quadtree

Programming II (CS300)

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Generic BST Interface

CS 3114 Data Structures and Algorithms READ THIS NOW!

Organizing Spatial Data

Solution READ THIS NOW! CS 3114 Data Structures and Algorithms

We have the pointers reference the next node in an inorder traversal; called threads

Chapter 20: Binary Trees

Binary Trees: Practice Problems

Data Structures and Algorithms

Trees. (Trees) Data Structures and Programming Spring / 28

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

CS350: Data Structures Tree Traversal

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

INF2220: algorithms and data structures Series 1

Binary Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

IX. Binary Trees (Chapter 10)

Tree Structures. A hierarchical data structure whose point of entry is the root node

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Title Description Participants Textbook

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Unit III - Tree TREES

TREES Lecture 10 CS2110 Spring2014

CSI33 Data Structures

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

TREES. Trees - Introduction

Tree traversals and binary trees

Programming Languages and Techniques (CIS120)

Binary Trees. Examples:

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Spring 2018 Mentoring 8: March 14, Binary Trees

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

CSE 12 Abstract Syntax Trees

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

Binary Search Trees. Chapter 21. Binary Search Trees

Binary Trees, Binary Search Trees

CSI33 Data Structures

Data Structures and Algorithms

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

CS350: Data Structures Binary Search Trees

Heaps. Heaps. A heap is a complete binary tree.

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

CmpSci 187: Programming with Data Structures Spring 2015

Data Structures And Algorithms

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Multi-Way Search Tree

CS210 Project 5 (Kd-Trees) Swami Iyer

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Principles of Computer Science

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search

CS 151. Binary Search Trees. Wednesday, October 10, 12

CIS265/ Trees Red-Black Trees. Some of the following material is from:

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b

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 satisfies the conditions:

Traversing Trees with Iterators

Traversing Trees with Iterators

CS 206 Introduction to Computer Science II

CS200: Balanced Search Trees

CS 350 : Data Structures Binary Search Trees

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes O(n) time in a binary

CS 206 Introduction to Computer Science II

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Building Java Programs

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Algorithms. Deleting from Red-Black Trees B-Trees

CS61B Lecture #21: Tree Searching. Last modified: Wed Oct 12 19:23: CS61B: Lecture #21 1

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Data Structures in Java

Friday Four Square! 4:15PM, Outside Gates

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

8. Binary Search Tree

Section 05: Solutions

ECE 242. Data Structures

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Introduction to Binary Trees

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

CS S-06 Binary Search Trees 1

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Lecture Notes on Tries

Module 8: Range-Searching in Dictionaries for Points

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

Algorithms. AVL Tree

Transcription:

Implementation 1 Of course, the PR quadtree will be implemented as a formal Java generic. However, it may be somewhat less generic than the general BST discussed earlier. During insertion and search, it is necessary to determine whether one point lies NW, NE, SE or SW of another point. Clearly this cannot be accomplished by using the usual Comparable interface design to compare points. Two possible approaches: - have the data type provide accessors for the x- and y-coordinates - have the type provide a comparator that returns NW, NE, SE or SW Either is feasible. It is possible to argue either is better, depending upon the value placed upon various design goals. It is also possible to deal with the issue in other ways. In any case, the PR quadtree implementation will impose fairly strict requirements on any data type that is to be stored in it.

Node Implementation A simple node implementation might look like this: public class prquadnode { public prquadleaf() {... public prquadleaf( T data ) {... 2 public T element; public prquadnode NE, NW, SW, SE; However, this will waste memory equivalent to one data element in each internal node, and equivalent to four pointers in each leaf node. This suggests using a hierarchy of node types, with an abstract base type. But, this raises some thorny implementation issues, since a child of an internal node could be either another internal node or a leaf node.

Improving Storage Efficiency 3 Using a single node type wastes space in every node. The unused members can be eliminated by defining a hierarchy of nodes: prquadnode abstract prquadleaf concrete data elem no ptrs prquadinternal concrete no data elem ptrs prquadleaf { prquadleaf(t elem); ; T element; prquadinternal { prquadinternal(); ; prquadnode NE, NE, SE, SW; The definitions of the relevant classes are straightforward. But an internal node may point to either internal or leaf nodes, and there is no overlap in the public interfaces of the two derived types

Getting It to Work 4 The basic problem is quite simple: given a base-type pointer how can we tell whether its target is a leaf node or an internal node? One answer is that we may use the getclass method to determine the type of the target at runtime: // see if we're at a leaf node if ( sroot.getclass().getname().equals("prquadtree$prquadleaf") ) {... Although this is somewhat clumsy, it does allow the use of a node hierarchy to reduce the space cost of the tree. Note syntax for name of inner class

Implementation Here's a possible PR quadtree interface: 5 public class prquadtree< T extends 2DComparable<? super T> > { private class prquadnode {... private class prquadleaf extends prquadnode {... private class prquadinternal extends prquadnode {... private prquadnode root; private int xmin, xmax, ymin, ymax; public prquadtree(int xmin, int xmax, int ymin, int ymax) {... public boolean insert(t elem) {... private prquadnode inserthelper(prquadnode sroot, T elem, double xlo, double xhi, double ylo, double yhi) {... public boolean remove(t elem) {... public T find(t elem) {... public void clear();...

Implementation Some comments: 6 - the tree must be created to organize data elements that lie within a particular, bounded region in order for the partitioning logic - the question of how to manage different types for internal and leaf nodes raises some fascinating design and coding issues - the question of how to manage comparisons of the user data objects raises some fascinating design and coding issues - how to display the tree also raises some fascinating issues

Displaying the 7 The display here (aside from the edges) was produced by using a modification of the inorder traversal for binary trees. In order to make the structure clear: - internal nodes are represented by - empty children are represented by There are alternative ways to do this D C I H A B E F G

Example Code 8 public void printtreehelper(prquadnode sroot, String Padding) { // Check for empty leaf if ( sroot == null ) { System.out.println(Padding + "\n"); return; // Check for and process SW and SE subtrees if ( sroot.getclass().getname().equals("prquadtree$prquadinternal") ) { prquadinternal p = (prquadinternal) sroot; printtreehelper(p.sw, Padding + " "); printtreehelper(p.se, Padding + " "); // Display indentation padding for current node System.out.println(Padding); // Determine if at leaf or internal and display accordingly if ( sroot.getclass().getname().equals("prquadtree$prquadleaf") ) { prquadleaf p = (prquadleaf) sroot; System.out.println( Padding + p.element ); else System.out.println( Padding + "\n" );...

Example Code 9... // Check for and process NE and NW subtrees if ( sroot.getclass().getname().equals("prquadtree$prquadinternal") ) { prquadinternal p = (prquadinternal) sroot; printtreehelper(p.ne, Padding + " "); printtreehelper(p.nw, Padding + " ");