CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Advertisements

Solution of Assignment 3 and Midterm CSC2100B. AVL Tree A binary search tree is a binary tree in which every node has larger key than the nodes in its.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CSE 326: Data Structures AVL Trees
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
AVL Trees ITCS6114 Algorithms and Data Structures.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
Instructor: Lilian de Greef Quarter: Summer 2017
Binary Search Trees Implementing Balancing Operations Hash Tables
COMP261 Lecture 23 B Trees.
Part-D1 Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Week 7 - Friday CS221.
Search Trees.
April 19th – Avl Operations
Hashing CSE 2011 Winter July 2018.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
UNIT III TREES.
CSIT 402 Data Structures II
Introduction Applications Balance Factor Rotations Deletion Example
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Cse 373 April 26th – Exam Review.
Trees (Chapter 4) Binary Search Trees - Review Definition
Instructor: Lilian de Greef Quarter: Summer 2017
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Monday, April 16, 2018 Announcements… For Today…
Introduction to Hash Tables
Chapter 6 Transform and Conquer.
Data Structures and Algorithms
Data Structures and Algorithms
Hashing CS2110.
Data Structures and Algorithms
Instructor: Lilian de Greef Quarter: Summer 2017
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
B-Tree Insertions, Intro to Heaps
Data Structures and Algorithms
Lecture 5: Master Theorem, Maps, and Iterators
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Implementing Hash and AVL
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
Sorting and recurrence analysis techniques
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Lecture 9: Self Balancing Trees
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Lecture 14: Midterm Review
ITCS6114 Algorithms and Data Structures
Richard Anderson Spring 2016
Lecture 10: BST and AVL Trees
Lecture 14: Midterm Review
CSE 373 Data Structures Lecture 8
Lecture 9: Intro to Trees
326 Lecture 9 Henry Kautz Winter Quarter 2002
Dictionaries 二○一九年九月二十四日 ADT for Map:
Self-Balancing Search Trees
Presentation transcript:

CSE 373: Data Structures and Algorithms Hash Tables Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...

Today - Wrap up AVL Trees - Problem: Can we make get(k) operation on dictionaries fast: O(1) - Motivation - Hashing - Separate Chaining CSE 373 AU 18 – Shri mare

AVL Trees: Four cases to consider Insert location Case Solution Left subtree of left child of y (A) Left line case Single right rotation Right subtree of left child of y (B) Left kink case Double (left-right) rotation Left subtree of right child of y (C) Right kink case Double (right-left) rotation Right subtree of right child of y (D) Right line case Single left rotation y x z A B C D CSE 373 AU 18 – Shri mare

AVL Trees: Four cases to consider Insert location Case (also called as) Solution Left subtree of left child of y (A) Left line case (case 1) Single right rotation Right subtree of left child of y (B) Left kink case (case 2) Double (left-right) rotation Left subtree of right child of y (C) Right kink case (case 3) Double (right-left) rotation Right subtree of right child of y (D) Right line case (case 4) Single left rotation y x z A B C D CSE 373 AU 18 – Shri mare

AVL Tree: Practice. Insert(6) CSE 373 AU 18 – Shri mare

AVL Tree: Practice Unbalanced CSE 373 AU 18 – Shri mare

AVL Trees: Four cases to consider Insert location Case (also called as) Solution Left subtree of left child of y (A) Left line case (case 1) Single right rotation Right subtree of left child of y (B) Left kink case (case 2) Double (left-right) rotation Left subtree of right child of y (C) Right kink case (case 3) Double (right-left) rotation Right subtree of right child of y (D) Right line case (case 4) Single left rotation y x z A B C D CSE 373 AU 18 – Shri mare

x y z A B C D AVL Tree: Practice Unbalanced CSE 373 AU 18 – Shri mare

x y z A B C D AVL Tree: Practice Unbalanced CSE 373 AU 18 – Shri mare

Worksheet Q1 insert(1) Case: Line or Kink? CSE 373 AU 18 – Shri mare

Worksheet Q1 insert(1) Case: Line or Kink? CSE 373 AU 18 – Shri mare

Worksheet Q1 insert(1) insert(3) Case: Line or Kink? CSE 373 AU 18 – Shri mare

Worksheet Q1 insert(1) insert(3) Case: Line or Kink? CSE 373 AU 18 – Shri mare

AVL Tree insertions 1. Do a BST insert – insert a node as you would in a BST. 2. Check balance condition at each node in the path from the inserted node to the root. 3. If balance condition is not true at a node, identify the case 4. Do the corresponding rotation for the case CSE 373 AU 18 – Shri mare

Worksheet Q2 Draw the AVL tree that results from inserting the keys 1, 3, 7, 5, 6, 9 in that order into an initially empty AVL tree. (Hint: Drawing intermediate trees as you insert each key can help.) CSE 373 AU 18 – Shri mare

How long does AVL insert take? AVL insert time = BST insert time + time it takes to rebalance the tree = O(log n) + time it takes to rebalance the tree How long does rebalancing take? Assume we store in each node the height of its subtree. How long to find an unbalanced node: Just go back up the tree from where we inserted. How many rotations might we have to do? Just a single or double rotation on the lowest unbalanced node. AVL insert time = O(log n)+ O(log n) + O(1) = O(log n)

How long does AVL insert take? AVL insert time = BST insert time + time it takes to rebalance the tree = O(log n) + time it takes to rebalance the tree How long does rebalancing take? Assume we store in each node the height of its subtree. How long to find an unbalanced node: Just go back up the tree from where we inserted.  O(log n) How many rotations might we have to do? Just a single or double rotation on the lowest unbalanced node.  O(1) AVL insert time = O(log n)+ O(log n) + O(1) = O(log n)

AVL wrap up Pros: - O(log n) worst case for find, insert, and delete operations. - Reliable running times than regular BSTs (because trees are balanced) Cons: - Difficult to program & debug [but done once in a library!] - (Slightly) more space than BSTs to store node heights. CSE 373 AU 18 – Shri mare

Lots of cool Self-Balancing BSTs out there! Popular self-balancing BSTs include: AVL tree Splay tree 2-3 tree AA tree Red-black tree Scapegoat tree Treap (Not covered in this class, but several are in the textbook and all of them are online!) (From https://en.wikipedia.org/wiki/Self-balancing_binary_search_tree#Implementations) CSE 373 SU 17 – lilian de Greef

Announcements - HW3 out. Due this Friday (10/26) at Noon (not at the usual time 11:59pm) - HW4 out later today (or latest by tomorrow morning). Due next Tuesday (10/30) - Midterm coming up – Nov 2, 2:30-3:20pm, here in the class - If you can’t take the midterm on Nov 2, let me know ASAP. - Midterm practice material will be posted on the website tomorrow - Midterm review next Wednesday CSE 373 AU 18 – Shri mare

Hash tables CSE 373 AU 18 – Shri mare

Revisiting Dictionaries - data = (key, value) - operations: put(key, value); get(key); remove(key) - O(n) with Arrays and Linked List - O(log n) with BST and AVL trees. - Can we do better? Can we do this in O(1) ? CSE 373 AU 18 – Shri mare

Motivation Why we are so obsessed with making dictionaries fast? Dictionaries are extremely most common data structures. - Databases - Network router tables - Compilers and Interpreters - Faster than O(log n) search in certain cases - Data type in most high level programming languages CSE 373 AU 18 – Shri mare

Question How would you implement at dictionary such that dictionary operations are O(1)? (Assume all keys are non-zero integers) CSE 373 AU 18 – Shri mare

Question How would you implement at dictionary such that dictionary operations are O(1)? (Assume all keys are non-zero integers) Idea: Create a giant array and use keys as indices CSE 373 AU 18 – Shri mare

Question How would you implement at dictionary such that dictionary operations are O(1)? (Assume all keys are non-zero integers) Idea: Create a giant array and use keys as indices Problems? 1. ? 2. ? CSE 373 AU 18 – Shri mare

Question How would you implement at dictionary such that dictionary operations are O(1)? (Assume all keys are non-zero integers) Idea: Create a giant array and use keys as indices Problems? 1. Can only work with integer keys? 2. Too much wasted space Idea 2: Can we convert the key space into a smaller set that would take much less memory CSE 373 AU 18 – Shri mare

Solve problem: Too much wasted space CSE 373 AU 18 – Shri mare

Review: Integer remainder with % The % operator computes the remainder from integer division. 14 % 4 is 2 3 43 4 ) 14 5 ) 218 12 20 2 18 15 3 Applications of % operator: Obtain last digit of a number: 230857 % 10 is 7 See whether a number is odd: 7 % 2 is 1, 42 % 2 is 0 Limit integers to specific range: 8 % 12 is 8, 18 % 12 is 6 218 % 5 is 3 CSE 142 SP 18 – Brett Wortzman

Implement Direct Access Map public V get(int key) { // input validation return this.array[key].value; } public void put(int key, V value) { this.array[key] = value; public void remove(int key) { this.array[key] = null; CSE 373 WI 18 – Michael Lee

Implement First Hash Function public V get(int key) { // input validation int newKey = key % this.array.length; return this.array[newkey].value; } public void put(int key, V value) { this.array[key % this.array.length] = value; public void remove(int key) { this.array[newKey] = null; CSE 373 SP 18 - Kasey Champion

First Hash Function: % table size indices 1 2 3 4 5 6 7 8 9 elements “foo” “biz” “bar” “bop” “poo” put(0, “foo”); put(5, “bar”); put(11, “biz”) put(18, “bop”); put(20, “poo”); 0 % 10 = 0 5 % 10 = 5 11 % 10 = 1 18 % 10 = 8 20 % 10 = 0 Collision! CSE 373 SP 18 - Kasey Champion

Today √ Wrap up AVL Trees √ Problem: Can we make get(k) operation on dictionaries fast: O(1) √ Motivation √ Hashing - Separate Chaining CSE 373 AU 18 – Shri mare

Hashing: Separate Chaining CSE 373 AU 18 – Shri mare