Dictionaries CS 110: Data Structures and Algorithms First Semester, 2010-2011.

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

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
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());
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.
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:
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
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.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
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.
1 Binary Search Trees (Continued) Study Project 3 Solution Balanced Binary Search Trees Balancing Operations Reading: L&C 11.1 – 11.4.
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.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues 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.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
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.
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 110: Data Structures and Algorithms First Semester,

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)

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)

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; }

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

Dictionary Implementations ► Unordered list ► Ordered table ► Binary search tree

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

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

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

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 ) BinarySearch( S, someKey, 0, size-1 ); array of Entries target key

Binary Search Algorithm lowmidhigh find(22) mid = (low+high)/2

Binary Search Algorithm highlowmid find(22) mid = (low+high)/2

Binary Search Algorithm low midhigh find(22) mid = (low+high)/2

Binary Search Algorithm low=mid=high find(22) mid = (low+high)/2

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 )

Time Complexity O( log n ) O( n ) find() O(n ) Ordered Table O( n )O( 1 )Unsorted List remove()insert()Operation

Binary Search Tree (BST) ► Strategy: store entries as nodes in a tree such that an in-order 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

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 ) )

Insertion in a BST (insert 78)

Insertion in a BST (insert 78)

Insertion in a BST (insert 78)

Insertion in a BST (insert 78)

Insertion in a BST (insert 78)

Insertion in a BST (insert 78)

Insertion in a BST (insert 78)

Insertion in a BST (insert 78)

Insertion in a BST (insert 78)

Removal from a BST (Ex 1)

Removal from a BST (Ex 1) Remove 32

Removal from a BST (Ex 1)

Removal from a BST (Ex 1)

Removal from a BST (Ex 1)

Removal from a BST (Ex 1)

Removal from a BST (Ex 1)

Removal from a BST (Ex 1)

Removal from a BST (Ex 2) Remove 65

Removal from a BST (Ex 2)

Removal from a BST (Ex 2)

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

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

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

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)