Dictionaries Collection of items. Each item is a pair. (key, element)

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

Winner trees. Loser Trees.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys. Operations.  get(theKey)  put(theKey, theElement)  remove(theKey) 5/2/20151.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Dictionaries Collection of items. Each item is a pair.  (key, element)  Pairs have different keys.
Binary Search Trees Dictionary Operations:  IsEmpty()  Search(key)  Insert(key, value)  Delete(key)
Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and.
Chapter 61 Chapter 6 Index Structures for Files. Chapter 62 Indexes Indexes are additional auxiliary access structures with typically provide either faster.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys. Operations.  get(theKey)  put(theKey, theElement)  remove(theKey)
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan)
Binary Search Tree 황승원 Fall 2011 CSE, POSTECH 2 2 Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
David Stotts Computer Science Department UNC Chapel Hill.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys. Operations.  find(theKey)  erase(theKey)  insert(theKey, theElement)
Binary Search Trees Dictionary Operations:  get(key)  put(key, value)  remove(key) Additional operations:  ascend()  get(index) (indexed binary search.
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
Binary Search Trees Dictionary Operations:  search(key)  insert(key, value)  delete(key) Additional operations:  ascend()  IndexSearch(index) (indexed.
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)
Binary Search Trees < > =
Unit 9 Multi-Way Trees King Fahd University of Petroleum & Minerals
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Multiway Search Trees Data may not fit into main memory
Binary Search Trees < > =
CSCI 210 Data Structures and Algorithms
Homework will be announced soon Midterm exam date announced
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Multiway search trees and the (2,4)-tree
Best-fit bin packing in O(n log n) time
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Dynamic Dictionaries Primary Operations: Additional operations:
Binary Search Trees Dictionary Operations: Additional operations:
Chapter Trees and B-Trees
Chapter Trees and B-Trees
Binary Search Trees Why this is a useful data structure. Terminology
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:
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.
Interval Heaps Complete binary tree.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
B-Trees (continued) Analysis of worst-case and average number of disk accesses for an insert. Delete and analysis. Structure for B-tree node.
Binary Search Trees < > =
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
A Kind of Binary Tree Usually Stored in an Array
Dictionaries Collection of unordered pairs. Operations. (key, element)
Dictionaries Collection of pairs. Operations. (key, element)
(2,4) Trees (2,4) Trees (2,4) Trees.
Data Structures & Algorithms Hash Tables
Lecture 12 CS203 1.
Dynamic Dictionaries Primary Operations: Additional operations:
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
Binary Trees, Binary Search Trees
Advanced Implementation of Tables
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
B-Trees.
CO4301 – Advanced Games Development Week 12 Using Trees
Binary Trees, Binary Search Trees
Binary Search Trees < > = Dictionaries
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

Dictionaries Collection of items. Each item is a pair. (key, element) Pairs have different keys. The word item is used as a synonym for element to avoid the confusion that would otherwise result by saying that a dictionary is a collection of elements and each element is a pair (key, element). A linear list is an ordered sequence of elements. A dictionary is just a collection of elements/items. C++ STL: Map and Multimap

Application Collection of student records in this class. (key, element) = (student name, linear list of assignment and exam scores) All keys are distinct. Collection of in-use domain names. (godaddy.com, owner information)

Dictionary With Duplicates Keys are not required to be distinct. Word dictionary. Items/pairs are of the form (word, meaning). May have two or more entries for the same word. (bolt, a threaded pin) (bolt, a crash of thunder) (bolt, to shoot forth suddenly) (bolt, a gulp) (bolt, a standard roll of cloth) etc. C++ STL Map and Multimap Can covert a dictionary with duplicates into one with no duplicates by associating with each key a list of elements/values.

Dictionary Operations Static Dictionary. initialize/create get(theKey) (a.k.a. search) CD ROM word dictionary CD ROM geographic database of cities, rivers, roads, auto navigation system, etc. Dynamic Dictionary. put(theKey, theElement) (a.k.a. insert) remove(theKey) (a.k.a. delete)

Hash Table Dictionaries O(1) expected time for get, put, and remove. O(n) worst-case time for get, put, and remove. O(log n) if overflows handled by balanced search trees. Not suitable for nearest match queries. Get element with smallest key >= theKey. Not suitable for range queries. Not suitable for indexed operations. Get element with third smallest key. Remove element with 5th smallest key. Hash table with balanced search trees for overflow lists. Useful to provide better expected performance in applications where you must guarantee log n worst-case performance. Range search.

Bin Packing n items to be packed into bins each item has a size each bin has a capacity of c minimize number of bins Problem first studied in the context of tournament trees. Winner trees used to implement first-fit packing.

Bin Packing Heuristics Best Fit. Items are packed one at a time in given order. To determine the bin for an item, first determine set S of bins into which the item fits. If S is empty, then start a new bin and put item into this new bin. Otherwise, pack into bin of S that has least available capacity.

Best Fit Example n = 4 weights = [4, 7, 3, 6] capacity = 10 Pack red item into first bin.

Best Fit n = 4 weights = [4, 7, 3, 6] capacity = 10 Pack blue item next. Doesn’t fit, so start a new bin.

Best Fit n = 4 weights = [4, 7, 3, 6] capacity = 10

Best Fit n = 4 weights = [4, 7, 3, 6] capacity = 10 Pack yellow item into second bin.

Best Fit n = 4 weights = [4, 7, 3, 6] capacity = 10 Pack green item into first bin.

Best Fit n = 4 weights = [4, 7, 3, 6] capacity = 10 Optimal packing.

Implementation Of Best Fit Use a dynamic dictionary (with duplicates) in which the items are of the form (available capacity, bin index). Pack an item whose requirement is s. Find a bin with smallest available capacity >= s. Reduce available capacity of this bin by s. May be done by removing old pair and inserting new one. If no such bin, start a new bin. Insert a new pair into the dictionary.

Best Fit Example 12 active bins. Pack item whose size is 22. 20 10 6 2 8 15 40 30 25 35 7 18 12 active bins. Pack item whose size is 22.

Complexity Of Best Fit Use a balanced binary search tree (with duplicates) in which the pairs are (available capacity, bin index). O(n) get, put, and remove/put operations, where n is the number of items to be packed. O(n log n). Could also use balanced binary search tree with no duplicates and pairs of the form (available capacity, list of bin indexes)

Indexed Binary Search Tree Each node has an additional field. leftSize = number of nodes in its left subtree

Example Indexed Binary Search Tree 7 20 4 3 10 40 1 1 6 15 30 1 18 25 35 2 8 7 leftSize values are in red

leftSize And Rank Rank of an element is its position in inorder (inorder = ascending key order). [2,6,7,8,10,15,18,20,25,30,35,40] rank(2) = 0 rank(15) = 5 rank(20) = 7 lextSize(x) = rank(x) with respect to elements in subtree rooted at x In inorder we do left subtree, root, right subtree. So, rank = #nodes in left subtree = leftSize.

leftSize And Rank sorted list = [2,6,7,8,10,15,18,20,25,30,35,40] 7 4 1 6 15 30 1 18 25 35 2 8 7 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]

get(index) And remove(index) 7 20 10 6 2 8 15 40 30 25 35 18 1 4 3 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]

get(index) And remove(index) if index = x.leftSize desired element is x.element if index < x.leftSize desired element is index’th element in left subtree of x if index > x.leftSize desired element is (index – x.leftSize – 1)’th element in right subtree of x

Linear List As Indexed Binary Tree h e b a d f l j i k c g 1 4 7 3 list = [a,b,c,d,e,f,g,h,i,j,k,l] List elements are placed in a binary tree so that an inorder traversal of the binary tree visits the elements in list order from left to right. get() and remove() work as they do in an indexed binary search tree.

Indexed AVL Tree (IAVL) Performance Linear List. get(index) put(index, element) remove(index) Array. O(1), O(n), O(n). Chain. O(n), O(n), O(n). Indexed AVL Tree (IAVL) O(log n), O(log n), O(log n).

40,000 of each operation. Java code on a 350MHz PC Experimental Results 40,000 of each operation. Java code on a 350MHz PC Start with an empty list and either do 40,000 best-case inserts or 40,000 inserts at random positions (average-case measurements). For remove, either do 40,000 worst-case removes starting from a list with 40,000 elements or do remove from randomly generated positions. For search, get each element once. Reported time is total time for 40,000 ops.

Performance Time for 40,000 operations Indexed AVL Tree (IAVL) Operation Array Chain IAVL get 5.6ms 157sec 63ms average puts 5.8sec 115sec 392ms worst-case puts 11.8sec 157sec 544ms average removes 5.8sec 149sec 1.5sec worst-case removes 11.7sec 157sec 1.6sec Average and worst-case adds/removes are much faster when an IAVL tree, a linked data structure is used. A sequence of 40K inserts, 40K puts, 40K removes (all average case) takes 11.6 sec using arrays, 421 sec using chains and 1.9 sec using IAVL. True worst-case times for a chain could be somewhat higher as in the tests, nodes were not randomized and so 2 adjacent nodes may lie in the same cache line. For IAVL. Two adjacent nodes on a root to leaf path are unlikely to lie in the same cache line. Average-case inserts randomize memory used along the chain while worst-case inserts result in adjacent nodes being adjacent in memory. Hence, average inserts time was not half worst-case time. Worst-case removes experiment started with worst-case inserts chain. So, this test had a better (memory adjacent) chain to work with than the average removes test that started with the average inserts chain. Time for 40,000 operations

Focus Tree structures for static and dynamic dictionaries.