1B1b Implementing Data Structures Lists, Hash Tables and Trees

Similar documents
Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Programming II (CS300)

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Summer Final Exam Review Session August 5, 2009

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

Binary Trees

Principles of Computer Science

AP Programming - Chapter 20 Lecture page 1 of 17

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Programming II (CS300)

Data Structures Question Bank Multiple Choice

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

ITEC2620 Introduction to Data Structures

Trees. CSE 373 Data Structures

CS301 - Data Structures Glossary By

Programming II (CS300)

CS 307 Final Spring 2009

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

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

INF2220: algorithms and data structures Series 1

BBM 201 Data structures

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

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Stores a collection of elements each with an associated key value

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

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

Binary Trees, Binary Search Trees

Data Structures in Java

Chapter 20: Binary Trees

Name CPTR246 Spring '17 (100 total points) Exam 3

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Collections, Maps and Generics

ITI Introduction to Computing II

ITI Introduction to Computing II

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Lecture 18. Collision Resolution

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved

Standard ADTs. Lecture 19 CS2110 Summer 2009

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

Carlos Delgado Kloos Mª Carmen Fernández Panadero Raquel M. Crespo García Dep. Ingeniería Telemática Univ. Carlos III de Madrid

CSE 230 Intermediate Programming in C and C++ Binary Tree

12 Abstract Data Types

Model Solutions. COMP 103: Test May, 2013

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

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

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

INF2220: algorithms and data structures Series 1

Data Structures and Algorithms

Why Do We Need Trees?

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

TREES Lecture 12 CS2110 Fall 2016

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

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

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Data Structure Advanced

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

SOFTWARE DEVELOPMENT 1. Recursion 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

CMSC132, Practice Questions

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees!

CP2 Revision. theme: dynamic datatypes & data structures

Topic 14. The BinaryTree ADT

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

COMP : Trees. COMP20012 Trees 219

Binary Search Trees Treesort

Elementary Data Structures 2

Points off Total off Net Score. CS 314 Final Exam Spring 2017

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Trees. Truong Tuan Anh CSE-HCMUT

Section 05: Solutions

void insert( Type const & ) void push_front( Type const & )

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

EXAMINATIONS 2016 TRIMESTER 2

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

tree nonlinear Examples

DS ata Structures Aptitude

Visit ::: Original Website For Placement Papers. ::: Data Structure

TREES Lecture 12 CS2110 Spring 2018

Binary Trees. Examples:

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

TREES Lecture 10 CS2110 Spring2014

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

Stacks, Queues & Trees. The Stack Interface. The Stack Interface. Harald Gall, Prof. Dr.

TREES. Trees - Introduction

CS 8391 DATA STRUCTURES

Review. CSE 143 Java. A Magical Strategy. Hash Function Example. Want to implement Sets of objects Want fast contains( ), add( )

CS 3114 Data Structures and Algorithms READ THIS NOW!

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

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Transcription:

1B1b Implementing Data Structures Lists, Hash Tables and Trees Agenda Classes and abstract data types. Containers. Iteration. Lists Hash Tables Trees Note here we only deal with the implementation of data structures. 1b12 and 1b13 cover the detailed properties of data structures. 1 2 Data Abstraction We know a class declaration creates a User Defined Type. We can also describe a class as an implementation of a data abstraction or data type. An Abstract Data Type (ADT) provides a specification of a data type. A Data Abstraction class Pair private int ; private int y ;... public Pair(int a, int b)...... A new data abstraction is created here. Also a new type. 3 4 A Data Abstraction (2)... Pair p = new Pair(1, 3) ; Pair q = new Pair(34, -23) ;... A Pair can now be directly used, rather than having to manage two separate variables. Pair is (a bit) more abstract and hides unwanted detail that would otherwise intrude. Abstract Data Types and Classes A class can be used to provide an implementation that conforms to an ADT specification. Typically ADTs are associated with data structures. Collections or Containers. Collections are objects that act as containers in which other objects (or really object references) are stored. List, Tree, ArrayList,, Graph, Hash Table, etc. 5 6 1

Abstraction Layers Application Package Base language User Package WordProcessor Components ADTs Java Implementing a container Obviously use a class Need a data structure to store contained object references: one or more instance variables (private of course). Need algorithms to implement access operations as methods. 7 8 Implementation properties Need to consider: Memory use. Speed of operation. Typically trading off one property against another. Need to select implementations that match the needs of your program. Iterators Every collection has to provide a mechanism for dealing with each element in the collection in turn. Such an mechanism is called an iterator. Algorithms such as linear searching, comparison, function application depend on use of iterators. 9 10 ArrayList Using an Iterator ArrayList a = new ArrayList () ;... for (Iterator( i = a.iterator() ; i.hasnet() ; ) dosomething((string)i.net()) ) ; No longer need an integer counter, instead an Iterator object is used to access each ArrayList element. Questions? 11 12 2

Linked Lists A linked list is implemented as a chain of linked elements (objects). 1 head 7 11 Linked Lists Each element or node consists of a stored value and a reference to the net element. A reference is maintained to the head of the list. An individual element is located by following the chain from the head. 13 14 Sequence Elements in a list (or vector, or array) are stored in sequence. Accessing elements often relies on the sequence. A list is a sequence container. List Implementation List Element class ListElement Object datum ; // Note use of Object ListElement netelement ;... datum netelement 15 16 Complete ListElement public class ListElement private ListElement net; private Object value; public ListElement(Object value, ListElement net) this.value = value; this.net = net; public Object getvalue() return value; public ListElement getnet() return net; Using ListElements Lists can be constructed directly using ListElements. Useful for creating chains of objects. Need to implement methods to access the chain (add, remove, search, etc.). 17 18 3

19 A List class class List private static class ListElement... private ListElement head = null ; Note: The element class is nested inside the List public List()... class and made private. public Object gethead()... public List gettail()... public boolean isempty()... public Iterator iterator()... public void inserthead(object obj) )... // etc. 20 List Class v. List Elements The List class provides the public interface to lists. A List object can only be used by calling the public methods declared by the List class. List elements are private and cannot be used eternally. Implementation details are hidden (encapsulated). Eample algorithm Insertion 1 p head 4 newelement 7 c 11 Linked List Properties Inserting/removing at beginning or end is fast. Insertion/removal in middle can be fast once the location is found. But there is the potential cost of linear access O(n). Good for situations when elements are repeatedly inserted and deleted. 21 22 Double-Link List Questions? Links in both directions. Head and tail references. Some algorithms easier to implement but etra storage cost for each element. head tail 23 24 4

Trees Trees (2) branching factor Trees are another variation of data structures based on linked elements. They use a hierarchical organisation of elements rather than straight chains. Level 0 Level 1 root children parent Level 2 node 25 26 height = depth = 3 leaf node Trees (3) Crucial properties of Trees: Links only go down from parent to child. Each node has one and only one parent (ecept root which has no parent). There are no links up the data structure; no child to parent links. There are no sibling links; no links between nodes at the same level. Trees (4) Trees are immensely useful for sorting: insertion automatically sorts! and searching: sorted structure minimises the number of comparisons. 27 28 Binary Trees Binary Trees (2) The simplest kind of tree. root 9 3 14 37 54 68 This is a complete binary tree. Each node has a maimum of 2 child nodes. Nodes are ordered so that left child nodes have a value less than parent, right child nodes greater than or equal to. // A binary tree node class Node public Node(Object o, Node l, Node r) value = o ; left = l ; right = r ; Object value ; Node left ; Node right ; 29 30 5

Binary Trees (3) public class Tree private class Node private Node root = null ; public Tree()... public void insert(object obj)... public void delete(object obj)... public boolean includes(object obj)... Binary Tree Iteration Four ways of iterating through a tree: In-order. Pre-order. Post-order. order. Level-order. // Iterator(s) public Iterator iterator()... // But which order?... 31 32 Binary Tree Iteration (2) Pre-order, post-order order and in-order are related since they just rearrange order of iteration. Depth-first searches. Level-order is different. Breadth-first search. Binary Tree Iteration (3) root 9 3 14 37 54 In-order: 3, 9, 14, 37, 54, 68 Pre-order: 37, 9, 3, 14, 68, 54 Post-order: 3, 14, 9, 54, 68, 37 Level-order: 37, 9, 68, 3, 14, 54 68 This is a complete binary tree. 33 34 Binary Tree Iteration (4) In-order iteration: void inorder () if (left!= null) left.inorder(); System.out.println(value); if (right!= null) right.inorder(); Binary Tree Iteration (5) Pre-Order Iteration: void preorder () System.out.println(value); if (left!= null) left.preorder(); if (right!= null) right.preorder(); 35 36 6

Binary Tree Iteration (6) Post-Order Iteration: void postorder () if (left!= null) left.postorder(); if (right!= null) right.postorder(); System.out.println(value); Binary Tree Iteration (7) Level-order iteration. Need a queue of nodes: void levelorder() create empty queue add root node to queue while (queue is not empty) Node n = get and remove node at front of queue print n.value add n.left to end of queue add n.right to end of queue 37 38 Searching binary tree Use node value to determine whether to go left or right. boolean search(int n) if (value == n) return true; if ((value < n) && (left!= null)) return left.search(n); if ((value >= n) && (right!= null)) return right.search(n); return false; Questions? 39 40 Map In mathematics a map (aka( function) relates members of one set to members of another set: m : X Y Arrays Arrays (and ArrayLists) ) are implementations of maps: array : int Y For eample: char array[20] ; array[3] = c ; array[5] = w ; 41 42 7

Keys and values For eample: m : X Y Key Value Key type String. Value type PhoneNumber. Mapping from names to phone numbers. Hash Table An ADT that implements a map from any class type to any class type. For eample: map : String Colour Colour c = (Colour)a.get( green ) ; Needs a data structure to store mapping. Want O(1) access. 43 44 An idea? Could set up a mapping from the key to an int value, and then use the int as an array inde. G : X int H : int Y Hash Function Use a hash function to map the search key into an integer that can be used as an inde into the array: int hash(x key) ; m = H. G 45 46 Hash Function (2) The hash function must: return an integer within the array bounds of the storing array, map keys consistently and evenly to the integers; and, be quick to calculate. Hash Function eample Consider the case where keys are strings. Need a mapping from the string to an integer array inde. If we use characters as the key then: int key = (key[0] + 3*key[1]) % tablesize is a possible hash function. 47 48 8

Hash Function (3) Hashing is so important that in Java every object has a hash code to enable easy storage in hash tables. See the method hashcode implemented by all objects. (Inherited from Object.) Hash Function (4) Given that there are more keys that array entries, there will be multiple hits. The hash function will return the same integer for a number of keys. Need a mechanism for handling this. 49 50 Chained Hashing The hash table is an array of linked nodes (like linked lists). The first stage of search is an hash lookup. The second stage of search is a linear search along the linked chain of nodes. The chains allow for overflow when hash values collide. Chained Hashing (2) array key value key value key value key key value value key value chains 51 52 Chain Node Class Hash Table class private static class Node public Node net ; public Object key ; public Object val ; etc. Like a LinkedList node, but with an etra field. Rest of class is a simplified list class. class HashTable private Node[ ] table = new Node[tableSize] ] ;... 53 54 9

Chained Hashing (3) Values are inserted by: Hashing key and performing array inde. Creating new node. Inserting new node at head of chain. Look-up: Hash key and perform array inde. Linear search of chain to find node with matching key. Return value from node. Allows duplicate key/values pairs to eist. Open Hashing (1) Have seen linked lists used as the overflow technique in an hash table. There is one other major technique for handling hash collisions: open hashing. 55 56 Open Hashing (2) The array holds the data itself not chains of nodes holding the data. If the slot determined by the hash function is full, linearly search down the array for the net empty slot. Open Hashing (3) 57 58 Open Hashing (4) X Y Open Hashing (5) Can do this linearly, e.g. step by 1 if there is a clash. Can also do this quadratically,, or even eponentially. But number of elements that can be stored is limited by array size. 59 60 10

Eample code See the 1b1b web page for eample code for a Linked List and a Chained Hash Table. Make sure you study this code and understand how it works. Questions? 61 62 Further Information Check the 1b1b web page for eample code. Read Part 2 of the tet book. Net year you need to know and understand the content of part 2 in detail. There won t be lectures on the material. Summary Looked at the key data structures: List Tree Hash Table (Map) All rely on object references (pointers). Have different performance properties. 63 64 11