Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.

Slides:



Advertisements
Similar presentations
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
BST Data Structure A BST node contains: A BST contains
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
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 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
Starting at Binary Trees
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Symbol Tables and Search Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Searching the dictionary ADT binary search binary search trees.
Searching and Binary Search Trees CSCI 3333 Data Structures.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Dictionaries CS /02/05 L7: Dictionaries Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
David Stotts Computer Science Department UNC Chapel Hill.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Binary Search Trees (BST)
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Dictionaries CS 110: Data Structures and Algorithms First Semester,
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)
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
AA Trees.
Week 7 - Friday CS221.
Search Trees.
BST Trees
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
COMP 103 Binary Search Trees.
CS200: Algorithms Analysis
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Dictionaries < > = /17/2019 4:20 PM Dictionaries
CSC 143 Binary Search Trees.
Presentation transcript:

Dictionaries CS 105

L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search keys; insertion of an object must include a key searching requires a key and returns the key-object pair removal also requires a key Need an Entry interface/class Entry encapsulates the key-object pair (just like with priority queues)

L11: Dictionaries Slide 3 Sample Applications An actual dictionary key: word object: word record (definition, pronunciation, etc.) Record keeping applications Bank account records (key: account number, object: holder and bank account info) Student records (key: id number, object: student info)

L11: Dictionaries Slide 4 Dictionary Interface public interface Dictionary { public int size(); public boolean isEmpty(); public Entry insert( int key, Object value ) throws DuplicateKeyException; public Entry find( int key ); // return null if not found public Entry remove( int key ) // return null if not found; }

L11: Dictionaries Slide 5 Dictionary details/variations Key types For simplicity, we assume that the keys are ints But the keys can be any kind of object as long as they can be ordered (e.g., string and alphabetical ordering) Duplicate entries (entries with the same key) may be allowed Our textbook calls the data structure that does not allows duplicates a Map, while a Dictionary allows duplicates For purposes of this discussion, we assume that dictionaries do not allow duplicates

L11: Dictionaries Slide 6 Dictionary Implementations Unordered list (section 8.3.1) Ordered table (section 8.3.3) Binary search tree (section 9.1)

L11: Dictionaries Slide 7 Unordered list Strategy: store the entries in the order that they arrive O( 1 ) insert operation Can use an array, ArrayList, or linked list Find operation requires scanning the list until a matching key value is found Scanning implies an O( n ) operation Remove operation similar to find operation Entries need to be adjusted if using array/ArrayList O( n ) operation

L11: Dictionaries Slide 8 Ordered table Idea: if the list was ordered by key, searching is simpler/easier Just like for priority queues, insertion is slightly more complex Need to search for proper position of element -> O( n ) Find: don’t do a linear scan; instead, do a binary search Note: use array/ArrayList; not a linked list

L11: Dictionaries Slide 9 Binary search Take advantage of the fact that the elements are ordered Compare the target key with middle element to reduce the search space in half Repeat the process until the element is found or search space reduces to 1 Arithmetic on array indexes facilitate easy computation of middle position Middle of S[low] and S[high] is S[(low+high)/2] Not possible with linked lists

L11: Dictionaries Slide 10 Binary Search Algorithm Algorithm BinarySearch( S, k, low, high ) if low > high then return null; // not found else mid  (low+high)/2 e  S[mid]; if k = e.getKey() then return e; else if k < e.getKey() then return BinarySearch( S, k, low, mid-1 ) else return BinarySearch( S, k, mid+1, high ) array of Entriestarget key BinarySearch( S, someKey, 0, size-1 );

L11: Dictionaries Slide 11 Binary Search Algorithm lowmidhigh find(22) mid = (low+high)/2

L11: Dictionaries Slide 12 Binary Search Algorithm highlowmid find(22) mid = (low+high)/2

L11: Dictionaries Slide 13 low Binary Search Algorithm midhigh find(22) mid = (low+high)/2

L11: Dictionaries Slide 14 low=mid=high Binary Search Algorithm find(22) mid = (low+high)/2

L11: Dictionaries Slide 15 Time complexity of binary search Search space reduces by half until it becomes 1 n -> n/2 -> n/4 -> … -> 1 log n steps Find operation using binary search is O( log n )

L11: Dictionaries Slide 16 Time complexity O( log n ) O( n ) find() O(n ) Ordered Table O( n )O( 1 )Unsorted List remove()insert()Operation

L11: Dictionaries Slide 17 Binary Search Tree (BST) Strategy: store entries as nodes in a tree such that an inorder traversal of the entries would list them in increasing order Search, remove, and insert are all O( log n ) operations All operations require a search that mimics binary search: go to left or right subtree depending on target key value

L11: Dictionaries Slide 18 Traversing a BST Insert, remove, and find operations all require a key First step involves checking for a matching key in the tree Start with the root, go to left or right child depending on key value Repeat the process until key is found or a null child is encountered (not found) For insert operation, duplicate key error occurs if key already exists Operation is proportional to height of tree ( usually O(log n ) )

L11: Dictionaries Slide 19 Insertion in BST (insert 78)

L11: Dictionaries Slide Insertion in BST

L11: Dictionaries Slide Removal from BST (Ex. 1) w z Remove 32

L11: Dictionaries Slide Removal from BST (Ex. 1) w z

L11: Dictionaries Slide Removal from BST (Ex. 1)

L11: Dictionaries Slide Removal from BST (Ex. 2) w Remove 65

L11: Dictionaries Slide Removal from BST (Ex. 2) w y x 54

L11: Dictionaries Slide 26 Removal from BST (Ex. 2) w

L11: Dictionaries Slide 27 Time complexity for BSTs O( log n ) operations not guaranteed since resulting tree is not necessarily “balanced” If tree is excessively skewed, operations would be O( n ) since the structure degenerates to a list Tree could be periodically reordered to prevent skewedness

L11: Dictionaries Slide 28 Time complexity (average case) O(n )O( log n )O( n )Ordered Table O( log n ) O( n ) find() O( log n ) BST O( n )O( 1 )Unsorted List remove()insert()Operation

L11: Dictionaries Slide 29 Time complexity (worst case) O(n )O( log n )O( n )Ordered Table O( n ) find() O( n ) BST O( n )O( 1 )Unsorted List remove()insert()Operation

L11: Dictionaries Slide 30 About BSTs AVL tree: BST that “self-balances” Ensures that after every operation, the difference between the left subtree height and the right subtree height is at most 1 O( log n ) operation is guaranteed Many efficient searching methods are variants of binary search trees Database indexes are B-trees (number of children > 2, but the same principles apply)