Tree traversal preorder, postorder: applies to any kind of tree

Slides:



Advertisements
Similar presentations
§4 Open Addressing 2. Quadratic Probing f ( i ) = i 2 ; /* a quadratic function */ 【 Theorem 】 If quadratic probing is used, and the table size is prime,
Advertisements

CSE 250: Data Structures Week 12 March 31 – April 4, 2008.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
CSE 326: Data Structures: Hash Tables
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
Hashing. Hashing as a Data Structure Performs operations in O(c) –Insert –Delete –Find Is not suitable for –FindMin –FindMax –Sort or output as sorted.
§3 Separate Chaining ---- keep a list of all keys that hash to the same value struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
1.  We’ll discuss the hash table ADT which supports only a subset of the operations allowed by binary search trees.  The implementation of hash tables.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Hashing Dr. Yingwu Zhu.
CS121 Data Structures CS121 © JAS 2004 Tables An abstract table, T, contains table entries that are either empty, or pairs of the form (K, I) where K is.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing as a Dictionary Implementation Chapter 19.
CSE373: Data Structures & Algorithms Lecture 17: Hash Collisions Kevin Quinn Fall 2015.
Hash Tables - Motivation
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
CSE 373 Data Structures and Algorithms Lecture 17: Hashing II.
Chapter 5: Hashing Collision Resolution: Open Addressing Extendible Hashing Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova,
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 Data Structures CSCI 132, Spring 2014 Lecture 34 Analyzing Hash Tables.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
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)
CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Fundamental Structures of Computer Science II
Instructor: Lilian de Greef Quarter: Summer 2017
CE 221 Data Structures and Algorithms
Hashing (part 2) CSE 2011 Winter March 2018.
Hashing.
Hashing Problem: store and retrieving an item using its key (for example, ID number, name) Linked List takes O(N) time Binary Search Tree take O(logN)
Hashing CSE 2011 Winter July 2018.
Hashing - resolving collisions
Hash Tables (Chapter 13) Part 2.
Handling Collisions Open Addressing SNSCT-CSE/16IT201-DS.
Quadratic probing Double hashing Removal and open addressing Chaining
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
Collision Resolution Neil Tang 02/18/2010
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
Searching Tables Table: sequence of (key,information) pairs
CSCE 3110 Data Structures & Algorithm Analysis
Hash Tables and Associative Containers
CSE 326: Data Structures Hashing
Hash Tables Chapter 12.7 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time Nyhoff, ADTs, Data Structures and.
CS202 - Fundamental Structures of Computer Science II
A Hash Table with Chaining
Advanced Implementation of Tables
Pseudorandom number, Universal Hashing, Chaining and Linear-Probing
Collision Resolution Neil Tang 02/21/2008
CSE 326: Data Structures Lecture #12 Hashing II
Ch Hash Tables Array or linked list Binary search trees
Collision Handling Collisions occur when different elements are mapped to the same cell.
Ch. 13 Hash Tables  .
Data Structures and Algorithm Analysis Hashing
DATA STRUCTURES-COLLISION TECHNIQUES
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Collision Resolution: Open Addressing Extendible Hashing
CSE 373: Data Structures and Algorithms
Presentation transcript:

Tree traversal preorder, postorder: applies to any kind of tree inorder: applies only to binary trees void BST::Traverse (BinaryNode *node) { if (node == NULL) // return an appropriate value return; else // PREORDER: compute/use node here Traverse (node->left); // INORDER: compute/use node here Traverse (node->right); // POSTORDER: compute/use node here } Oct 31, 2001 CSE 373, Autumn 2001

Tree traversal, example int NumLeaves (BNode<Comp> *tree) { if (tree == NULL) return 0; else int sum = 0; sum = NumLeaves(tree->left) + NumLeaves(tree->right); if ((tree->left == NULL) && (tree->right == NULL)) sum += 1; } return sum; Oct 31, 2001 CSE 373, Autumn 2001

Tree traversal, example 2 D B F A C E G preorder: ?? inorder: ?? postorder: ?? level order (use a queue): ?? Oct 31, 2001 CSE 373, Autumn 2001

Open Addressing Examine cells in the order h0(x), h1(x), h2(x), … where hi(k)=(hash(k) + f(i)) mod TableSize Linear Probing f(i) = i After searching spot hash(k) in the array, look in hash(k) + 1, hash(k) + 2, etc. example: insert 38, 19, 8, 109, 10 Oct 31, 2001 CSE 373, Autumn 2001

Linear Probing example 8 1 109 2 10 3 4 5 6 7 38 9 19 38 19 8 109 10 primary clustering Oct 31, 2001 CSE 373, Autumn 2001

Quadratic Probing f(i) = i2 After searching spot hash(k), look in the 1st, 4th, 9th, etc. spots after hash(k). Less likely to encounter primary clustering. … n-1 Oct 31, 2001 CSE 373, Autumn 2001

Double Hashing f(i) = i · hash2(x) Items that hash to the same location with hash(x) won’t have the same probe for hash2(x). Oct 31, 2001 CSE 373, Autumn 2001

Analysis of find Defn: The load, , of a hash table is the ratio:  no. of elements  table size Separate chaining unsuccessful:  (avg. length of a list at hash(k)) successful: 1 + (/2) (one node, plus half the avg. length of a list (not including the item)). Oct 31, 2001 CSE 373, Autumn 2001

Open addressing Linear probing unsuccessful:  successful:  (Analysis from Knuth) Oct 31, 2001 CSE 373, Autumn 2001

Random Collision Resolution Model Goal: see how collisions occur under ideal conditions. On an insertion, each probe is independent of previous probes. On a successful find, the exact probe sequence used in insertion is used. unsuccessful: 1/(1 - ) (1- ) fraction of the cells are empty. successful: (ln means loge ) Oct 31, 2001 CSE 373, Autumn 2001

Comparison Double hashing – performance approximated by the random collision resolution model. linear probing random collision  unsucc. successful 0.1 1.1 0.3 1.5 1.4 1.2 0.5 2.5 2.0 0.7 6.1 3.3 2.2 1.7 0.9 50.5 10.0 5.5 2.6 Oct 31, 2001 CSE 373, Autumn 2001

Rehashing Idea: When the table gets too full, create a bigger table and hash all the items from the original table into the new table. When to rehash? half full ( = 0.5) when an insertion fails some other threshold Cost of rehashing Oct 31, 2001 CSE 373, Autumn 2001