Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2

Similar presentations


Presentation on theme: "Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2"— Presentation transcript:

1 Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

2 Midterm Exam 2 When: Wednesday (04/04) 12:10 -1pm Where: In Class
Cpt S 223 Midterm Exam 2 When: Wednesday (04/04) 12:10 -1pm Where: In Class Closed book, Closed notes Comprehensive Material for preparation: Lecture slides primarily covered after Midterm Exam # 1 B-Trees, Hashing and Priority Queues Questions might be indirectly related to previous topics Homework 4 and Programming assignment 3 (Hash Table) Weiss Textbook Washington State University

3 Course Overview B-Trees (Chapter 4.7) Hashing (Chapter 5)
Insertion and Deletion Hashing (Chapter 5) Hash Function Separate Chaining Linear Probing, Quadratic probing, Double Hashing Priority Queues/Heaps (Chapter 6) Binary Heap Insert, deleteMin Leftist Heaps Binomial Queues (Insert, deleteMin, Merge)

4 B-Trees Review (Chapter 4)
A B-tree (also called a B+ Tree) of order M is an M-ary tree with the following properties: Data items are stored at the leaves Non-leaf nodes store up to M-1 keys Key i represents the smallest key in subtree i + 1 Root node is either a leaf or has between 2 and M children Non-leaf nodes have between M / 2 and M children All leaves at same depth and have between L / 2 and L items Requiring nodes to be half full avoids degeneration into binary tree

5 B-Tree B-tree of order 5 (i.e. M = 5)
Node has 2 to 4 keys and 3 to 5 children Leaves have 3 to 5 data elements

6 B-Tree: Insertion Case 1: Insert into a non-full leaf node
E.g., insert 57 into the previous order 5 tree

7 B-Tree: Insertion (cont’d)
Case 2: Insert into a full leaf node, but parent has room Split leaf and promote middle element to parent E.g., insert 55 into previous tree

8 B-Tree: Insertion (cont’d)
Case 3: Insert into full leaf, parent has no room Split parent, promote parent’s middle element to grandparent Continue until non-full parent or split root E.g., insert 40 into previous tree

9 B-Tree: Deletion Case 1: Leaf node containing item not at minimum
E.g., remove 16 from previous tree

10 B-Tree: Deletion (cont’d)
Case 2: Leaf node containing item has minimum elements, neighbor not at minimum Adopt element from neighbor E.g., remove 6 from previous tree 10 8

11 B-Tree: Deletion (cont’d)
Case 3: Leaf node containing item has minimum elements, neighbors have minimum elements Merge with neighbor and intermediate key If parent now below minimum, continue up the tree E.g., remove 99 from previous tree

12 B-Tree: Case 3 Deletion (cont’d)

13 Hashing Review (Chapter 5)

14 h(key) ==> hash table index
Hash Function Mapping from key to array index is called a hash function Typically, many-to-one mapping Different keys map to different indices Distributes keys evenly over table Hash function h(key) = key mod TableSize with integer keys Add up character ASCII values (0-127) to produce integer keys Treat first 3 characters of string as base-27 integer Collision occurs when hash function maps two keys to same array index h(key) ==> hash table index

15 Collision Resolution by Chaining
Hash table T is a vector of lists Only singly-linked lists needed if memory is tight Key k is stored in list at T[h(k)] E.g. TableSize = 10 h(k) = k mod 10 Insert first 10 perfect squares Insertion sequence = 0, 1, 4, 9, 16, 25, 36, 49, 64, 81

16 Collision Resolution by Chaining: Analysis
Load factor  of a hash table T N = number of elements in T M = size of T  = N / M Average length of a chain is  Unsuccessful search O() Successful search O( / 2) Ideally, we want   1 (not a function of N) i.e., TableSize = number of elements you expect to store in the table Keep the TableSize prime to ensure a good distribution

17 Collision Resolution by Open Addressing
Probe sequence Sequence of slots in hash table to search h0(x), h1(x), h2(x), … Needs to visit each slot exactly once Hash function hi(x) = (hash(x) + f(i)) mod TableSize f(0) = 0 ==> first try Three common collision resolution strategies Linear Probing: hi(x) = (hash(x) + f(i)) mod TableSize, f(i)=i Quadratic Probing: hi(x) = (hash(x) + f(i)) mod TableSize; f(i) = i2 Double Hashing: hi(x) = (hash(x) + f(i)) mod TableSize; f(i) = i * hash2(x)

18 Extendible Hashing Example
Extendible hash table Contains N = 12 data elements First D = 2 bits of key used by root node keys 2D entries in directory Each leaf contains up to M = 4 data elements As determined by disk page size Each leaf stores number of common starting bits (dL)

19 Extendible Hashing Example (cont’d)
After inserting 100100 Directory split and rewritten Leaves not involved in split now pointed to by two adjacent directory entries. These leaves are not accessed.

20 Priority Queues/Heaps (Chapter 6)

21 Structure Property of Binary Heap
A binary heap is a complete binary tree Each level is completely filled Bottom level may be partially filled from left to right Height of a complete binary tree with N elements is log2 N

22 Binary Heap Example N = 10 Every level (except last) completely filled
Array representation of the binary heap above:

23 Heap-Order Property of Binary Heap
Heap-order property (for a “min binary heap”) For every node X, key(parent(X))  key(X) Except root node, which has no parent Thus, minimum key always at the root Alternatively, for a “max binary heap”, always keep the maximum key at the root insert and deleteMin must always maintain heap-order property

24 Heap Insert: Example 14 vs. 31 Insert 14: 14 vs. 21 14 vs. 13
Heap-order okay; structure okay Percolating up

25 Heap DeleteMin Minimum element is always at the root
A hole is created at the root when minimum element is removed Heap decreases by one in number of elements (or size) Last element X should be moved somewhere in the heap If it can be placed in the hole, then we are done Push smaller of the hole’s children into the hole, thus pushing the hole down one level Repeat this step until X can be placed in the hole Percolate down until the heap-order property is not restored

26 Heap DeleteMin: Example
Remove value at the root Move 31 (last element) to the root Is 31 > min(14, 16)? If yes, swap 31 with min(14, 16) If no, leave 31 in hole

27 Heap DeleteMin: Example (cont’d)
Percolating down… Is 31 > min(19, 21)? If yes, swap 31 with min(19, 21) If no, leave 31 in hole Is 31 > min(65, 26)? If yes, swap 31 with min(65, 26) If no, leave 31 in hole

28 Heap DeleteMin: Example (cont’d)
Percolating down… Heap-order property okay; Structure okay; Done.

29 Building a Heap What if all N elements are all available upfront to build the heap? To build a heap with N elements Default method takes O(N log N) time. New method will take O(N) time – i.e., optimal

30 Building a Heap (cont’d)
Construct a heap from an initial set of N items Solution 1 Perform N successive inserts O(1) constant time on average and O(log N) worst-case O(N) average case, but O(N log2 N) worst-case Solution 2 Randomly populate initial heap with structure property Perform a percolate-down from each internal node (i.e., H[size/2] to H[1]) To take care of heap-order property Do practice the buildheap() example from the lecture notes with linear time O(N)

31 Binomial Heap A forest of heap-ordered binomial trees
Structure property Heap-order property The structural property of each heap-ordered tree in binomial heap is different from binary heap The heap-order property applies to each binomial tree

32 Definition: A “Binomial Tree” Bk
A binomial tree of height k is called Bk It has 2k nodes The number of nodes at depth d is the binomial co-efficient A binomial tree need not to be a binary tree is the form of the coefficients in binomial theorem Depth B3 # of nodes d = 0 d = 1 d = 2 d = 3

33 Binomial Heap with 31 Nodes
We know: Binomial heap should be a forest of binomial trees Each binomial tree has power of 2 elements How many binomial trees do we need? n = 31 = B4 B3 B2 B1 B0

34 Binomial Heap with 31 Nodes (cont’d)
Bk == Bk-1 + Bk-1 B4 B2 B2 B3 B3 A binomial tree Bk consists of a root with children B0, B1, B2 ….Bk-1 How many trees are there in this forest in worst case if the # of nodes are N?

35 merge(H1, H2) Let n1 be the number of nodes in H1
Therefore, the new heap is going to have n1 + n2 nodes Logic: Merge trees of same height, starting from lowest height trees If only one tree of a given height, then just copy that May need carryover (just like adding two binary numbers)

36 merge(H1, H2): Example carryover + B0 B1 B2 13 14 26 16 ? B2

37 insert(x) in Binomial Heap
Goal To insert a new element x into a binomial heap H Observation Element x can be viewed as a single element binomial heap insert(x) == merge(H, {x}) So, if we know how to do merge we will automatically figure out how to implement both insert() and deleteMin().

38 deleteMin() Example Find the binomial tree with smallest root say Bk and original priority queue let be H Remove Bk from the forest of trees in H, forming the new binomial queue H’ Remove the root of Bk creating binomial trees B0, B1, B2 ….Bk-1 which collectively forms H’’ Merge H’ and H’’ B0 B2 B3 B0’ B1’ B2’ For deleteMin(): After delete, how to adjust the heap? New heap: Merge {B0, B2} and {B0’, B1’, B2’}.

39 Cpt S 223 Good Luck ! Washington State University


Download ppt "Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2"

Similar presentations


Ads by Google