data structures: an overview of abstract data types

33
Data Structures: An overview of Abstract Data Types P.F.Lammertsma June, 2004

Upload: others

Post on 12-Sep-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

P.F.Lammertsma

June, 2004

Page 2: Data Structures: An overview of Abstract Data Types
Page 3: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

i

Contents

1. Abstract Data Types ....................................................... 1

1.1. Stack ...................................................................................1

1.2. Queue ..................................................................................2

1.3. Vector .................................................................................3

1.4. List ......................................................................................4

1.5. Sequence .............................................................................5

1.6. Tree .....................................................................................6

1.7. Binary Tree .........................................................................7

1.8. Representations of Binary Trees ........................................8

1.9. Priority Queue ....................................................................9

1.10. Heap ..................................................................................10

1.11. Map ...................................................................................11

1.12. Hash Table ........................................................................12

1.13. Dictionary .........................................................................13

1.14. Look-Up Table ..................................................................14

1.15. Skip List.............................................................................15

Page 4: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types ii

2. Algorithms .................................................................... 19

2.1. Bubble Sort .......................................................................19

2.2. Preorder Traversal ............................................................20

2.3. Postorder Traversal ...........................................................21

2.4. Inorder Traversal ..............................................................22

2.5. Euler Tour Traversal ........................................................23

2.6. Last Node in Heap ............................................................24

2.7. Upheap ..............................................................................25

2.8. Downheap ........................................................................26

2.9. Binary Search ...................................................................27

Index ................................................................................. 29

Page 5: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

1

1.1 Stack push(o) Insert object o at the top of the stack. O(1)pop() Remove and return the top object on the stack.1 O(1)size() Return the number of objects in the stack. O(1)isEmpty() Return a Boolean indicating if the stack is empty. O(1)top() Return the top object on the stack, without removing it.1 O(1) 1 An error occurs if the stack is empty.

top

bottom

push pop head

Ø

push pop

Figure 1.1 Schematic depiction of a stack. (a) Array, (b) Linked List

(a) (b)

Page 6: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 2

1.2 Queue enqueue(o) Insert object o at the rear of the queue. O(1)dequeue() Remove and return the front object from the queue.1 O(1)size() Return the number of objects in the queue. O(1)isEmpty() Return a Boolean indicating if the queue is empty. O(1)front() Return the front object of the queue, without removing it.1 O(1) 1 An error occurs if the queue is empty.

Note: Double-ended queues – deques – use different function names than normal queues. To replace a queue with a double-ended queue, replace enqueue with insertLast, dequeue with removeFirst, and front with first. Note: Double-ended queues can also replace stacks. Replace push with insertLast, pop with removeLast, and top with last.

first() Return the first element of the deque, without removing it.1 O(1)

last() Return the last element of the deque, without removing it.1 O(1)

insertFirst(e) Insert a new element e at the beginning of the deque. O(1)insertLast(e) Insert a new element e at the end of the deque. O(1)removeFirst(e) Remove and return the first element of the deque.1 O(1)removeLast(e) Remove and return the last element of the deque.1 O(1) 1 An error occurs if the deque is empty.

front rear

enqueue

dequeue

head Ø

enqueue

dequeue

Figure 1.2 Schematic depiction of a queue. (a) Array, (b) Linked List, (c) Double-Ended Queue

(a) (b)

(c)

head tail

insertLast

removeFirst removeLast

insertFirst

Page 7: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

3

1.3 Vector A vector is also known as an array. elemAtRank(r) Return the element at rank r from the vector. 1 O(1)replaceAtRank(r,e) Replace the element at rank r with element e.1 O(1)

insertAtRank(r,e) Insert a new element e with rank r into the vector.2 O(n)

removeAtRank(r) Remove the element at rank r from the vector.1 O(n)size() Return the number of objects in the queue. O(1)isEmpty() Return a Boolean indicating if the queue is empty. O(1) 1 An error occurs if r < 0 or r > n - 1. 2 An error occurs if r < 0 or r > n.

Note: Inserting elements at rank r, as shown in figure 1.4, run at Θ(n) at worst case, because when adding an element at rank r, elements from rank r to n-1 (n being the first free element in the vector) must be moved one rank forward to free the element at rank r. Note: Removing an element takes equally long, because the process depicted in figure 1.4 must be reversed. Note: Arrays have a limit, so the vector can at one point be ‘full’. To extend the array, a new array B of capacity 2N must be allocated. All elements from the original array A must be copied to B. Finally, we must set A = B. The array now has double the capacity. This process takes 2n – 1 steps, O(n). (An alternative way of extending the array is ‘incrementally’. However, this method takes O(n2) time.)

Figure 1.3 Schematic depiction of a vector, array-based.

0 N

insertAtRank

removeAtRank

0 N-1 r n-1 n 0 N-1 0 N-1

Figure 1.4 Schematic depiction of insertAtRank – where r is the rank – in an array-based vector.

Page 8: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 4

1.4 List

first() Return the position of the first element of the list.1 O(1)

last() Return the position of the last element of the list.1 O(1)

isFirst(p) Return a Boolean indicating whether the position is the first one in the list. O(1)

isLast(p) Return a Boolean indicating whether the position is the last one in the list. O(1)

before(p) Return the position of the element preceding the one at position p.2 O(1)

after(p) Return the position of the element following the one at position p.3 O(1)

replaceElement(p,e) Return the element at position p, then replace it with element e. O(1)

swapElements(p,q) Swap the elements stored at positions p and q. O(1)

insertFirst(e) Insert a new element e as the first element of the list. O(1)

insertLast(e) Insert a new element e as the last element of the list. O(1)

insertBefore(p,e) Insert a new element e before position p in the list.2 O(1)

insertAfter(p,e) Insert a new element e after position p in the list.3 O(1)

remove(p) Remove element at position p from the list. O(1)size() Return the number of elements in the list. O(1)isEmpty() Return a Boolean indicating if the list is empty. O(1) 1 An error occurs if the list is empty. 2 An error occurs if p is the first position. 3 An error occurs if p is the last position.

Note: Each separate node contains a link to its predecessor, its successor, and its data.

Figure 1.5 Schematic depiction of a list. (a) Linked list, (b) a separate node

trailer header

A B C X (a) (b)

Page 9: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

5

1.5 Sequence A sequence is a combination of a vector and a list, and therefore uses all functions of both abstract data types. However, sequences use two additional functions to link element ranks to node positions. atRank(r) Return the position of the element with rank r. rankOf(p) Return the rank of the element at position p. There are two implementations of sequences: array-based and linked list. array list atRank, rankOf, elemAtRank O(1) O(n)first, last, before, after O(1) O(1)replaceElement, swapElements O(1) O(1)replaceAtRank O(1) O(n)insertAtRank, removeAtRank O(n) O(n)insertFirst, insertLast O(1) O(1)insertAfter, insertBefore O(n) O(1)remove O(n) O(1)size, isEmpty O(1) O(1) 1 An error occurs if the list is empty. 2 An error occurs if p is the first position. 3 An error occurs if p is the last position.

Figure 1.6 Schematic depiction of a sequence. (a) Linked list implementation, (b) Array based

(a)

(b)

trailer header

A B C

p0 p1 p2

r0 r1 r2

positions

elements

array

A B C

p0 p1 p2

r0 r1 r2

positions

elements

Page 10: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 6

1.6 Tree element() Return the object at the current position O(1) root() Return the root of the tree. O(1) parent(v) Return the parent of node v.1 O(1) children(v) Return an iterator of the children of node v.2 O(cv)

isInternal(v) Return a Boolean indicating whether node v is internal. O(1)

isExternal(v) Return a Boolean indicating whether node v is external. O(1)

isRoot(v) Return a Boolean indicating whether node v is the root. O(1)

elements() Return an iterator of objects of all the elements stored at nodes of the tree.2 O(n)

positions() Return an iterator of positions of all the nodes of the tree.2 O(n)

swapElements(v,w) Swap the elements stored at the nodes v and w. O(1)

replaceElement(v,e) Replace the element stored at node v with element e. O(1)

size() Return the number of nodes in the tree. O(1) isEmpty() Return a Boolean indicating if the tree is empty. O(1) cv is the number of nodes that the function applies to (e.g. number of children of v for children(v)). 1 An error occurs if v is the root. 2 The iterator provides access to the nodes in order, if the tree is ordered. If the result

wouldn’t doesn’t any nodes (e.g. the children(v) called on an external node), the iterator is empty.

Figure 1.7 Schematic depiction of a binary tree.

nodes

internal

external

height = 3

depth= 3

Page 11: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

7

1.7 Binary Tree Binary trees are trees where all nodes either have 2, 1 or 0 children. Proper binary trees cannot have a single child, either. In other words, each internal node has exactly two (or one, if it is an improper tree) child(ren). Binary trees have three additional functions. leftChild(v) Return the left child of node v.1 O(1) rightChild(v) Return the right child of node v.1 O(1)

sibling(v) Return the sibling of node v (the other child of its parent).2 O(1)

children(v) Return an iterator of both children of node v.3 O(1) 1 An error occurs if v is an external node. 2 An error occurs if v is the root. 3 The iterator either contains two nodes or is empty. Note: The function children now is O(1), because each node has either zero (one) or two children. Note: Figure 1.7 schematically depicts a binary tree. Properties of binary trees Notation: n number of nodes e number of external nodes i number of internal nodes h height Properties: e = i + 1 n = 2e – 1 h + 1 ≤ e ≤ 2h h ≤ i ≤ 2h – 1 2h + 1 ≤ n ≤ 2h + 1 – 1 log2 (n + 1) – 1 ≤ h ≤ (n – 1) / 2

Figure 1.8 Schematic depiction of a binary tree. (a) Balanced tree, (b) Unbalanced tree

(a)

(b)

height = 2

height = 1

Page 12: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 8

1.8 Representations of Binary Trees Binary trees and linked lists can be combined to create a linked structure, representing a binary tree with linked lists. Each node can then be schematically depicted as shown in figure 1.9a. Alternatively, binary trees can be represented by vector using level numbering. The rules for level numbering are:

- If v is the root node, then p(v) = 1. - If v is the left child of node u, then p(v) = 2p(u). - If v is the right child of node u, then p(v) = 2p(u) + 1.

This representation is schematically depicted in figure 1.9b.

parent

element left right

= =

Figure 1.9 Schematic depiction of a binary tree representations. (a) Vector-based structure, (b) Linked List-based structure

(a)

(b)

1

2 3

4 5 6 7

8 9 12 13

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 13: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

9

1.9 Priority Queue In a priority queue, each element consists of a pair: a key and a value. A priority queue can be sorted or unsorted (upon insertion). An example of a sorted priority queue: {(3,B),(5,A),(9,C)} unsorted sorted

insertItem(k,e) Insert a new element e with key k into the priority queue.1 O(1) O(n)

minElement() Return the element of the priority queue with the smallest key without removing it.1

O(n) O(1)

minKey() Return the smallest key in the priority queue.1 O(n) O(1)

removeMin() Remove and return the element with the smallest key from the priority queue.1 O(n) O(1)

size() Return the number of elements in the priority queue. O(1) O(1)

isEmpty() Return a Boolean indicating whether the priority queue is empty. O(1) O(1)

1 An error occurs if the priority queue is empty. The priority queue uses a comparison method that is used in some of the above listed functions. These functions first compare the keys of element a and b, then (if the keys are equal), the values. isLessThan(a,b) O(1) isLessThanOrEqual(a,b) O(1) isEqualTo(a,b) O(1) isGreaterThan(a,b) O(1) isGreaterThanOrEqualTo(a,b) O(1) isComparable(a,b) O(1) These functions use the following comparison function to determine the results.

compare(a,b) Returns integer i, such that: i < 0 if a < b, i = 0 if a = b, and i > 0 if a > b. O(1)

A priority queue implementation on a sequence can be used for sorting. There are two possibilities:

- Unsorted sequence gives selection-sort and takes O(n2) time, - Sorted sequence gives insertion-sort and takes O(n2) time.

Page 14: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 10

1.10 Heap A heap is a ‘complete’ binary tree where for every node v other than the root, the key stored at v is greater than or equal to the key stored at v’s parent. A binary tree is called ‘complete’ when for each level i the number of nodes at that level is equal to 2i, for 0 ≤ i ≤ h – 1. Furthermore, all internal nodes must be left of all external nodes at level h – 1. Figure 1.10 depicts a complete binary tree.

We can implement a heap on a priority queue, thus each node is a pair. For example: In figure 1.10, we could use: {(4,Sue),(5,Pat),(6,Mark),(15,Jeff),(9,Anna),(7,Dan)} Note: A heap storing n keys always has height O(log n), because: - 2i nodes per level i where 0 ≤ i ≤ h – 1 (and at least 1 node at level h), - n ≥ 20 + 21 + 22 + … + 2h-1 + 1 - n ≥ 2h - h ≤ log2 n Note: An insert will always add a new node to the right of the last node. If this is not possible (i.e. level i contains 2i nodes), then the node is added on the very left of level i+1. The heap needs to its heap-order property restored: this is called the upheap algorithm. Note: A removemin will always remove the root node, because this is per definition the minimum. Once again, we need to restore the heap-order property: we now use the downheap algorithm. Because insertions take O(log2 n), sorting a heap-based priority queue would take O(n log2 n); considerably faster than a sequence-based priority queue, taking O(n2) time.

Figure 1.10 Schematic depiction of a complete, proper binary tree. The keys are displayed next to each internal node, while external nodes are empty.

4

5

15 9 7

6

last node

Page 15: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

11

1.11 Map Similar to the priority queue, items in a map are distinct. Distinct items are a pair: a key and a value. Maps, however, do not allow multiple entries with the same key: in this case the value of the entry with that key will be replaced. Maps are efficient for adding data, but inefficient for searching and removing.

get(k) Return the associated value of the entry with key k from the map.1 O(n)

put(k,v) Insert entry (k, v) into the map.2 O(1) remove(k) Remove the entry with key k from the map.1 O(n) keys() Return an iterator of the keys in the map. O(n) values() Return an iterator of the values in the map. O(n) size() Return the number of items in the map. O(1)

isEmpty() Return a Boolean indicating whether the map is empty. O(1)

1 Return NULL if the entry with key k doesn’t exist. 2 Return the old value of the entry with key k, if this entry exists, otherwise return NULL.

Page 16: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 12

1.12 Hash Table When implementing a map with a hash table, the goal is to store item (k, v) at index h(k), where h(k) is a hash function that maps keys of a given type to integers in a fixed interval [0, N – 1]. A common hash function is h(x) = x mod N for integer keys. Because we are using a map, we can’t have multiple entries with the same key. Therefore, collision detection is required. There are several methods:

- Linear probing If the item at h(k) is free, place at h(k), otherwise at h(k+1), h(k+2), ….

- Double hashing

Place the item at index i so that: i = (h(k) + j(q – (k mod q))) mod N With j = 0, 1, 2, 3, …, and q = prime number < N.

- Quadratic probing

If the item at h(k) is free, place at h(k), otherwise at h(k+12), h(k+22), …. In the worst case, hashing can make searches, insertions and removals in a hash table take O(n), when all inserted keys collide. However, with a wide enough table, the expected running time can be much faster.

Page 17: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

13

1.13 Dictionary Dictionaries are similar to maps, except that multiple entries per key are allowed.

elements() Return an iterator of the elements in the dictionary. O(n)

keys() Return an iterator of the keys in the dictionary. O(n)

findElement(k) Return the element of an item with key k.1 O(n) findAllElements(k) Return an iterator of all elements with keys k. O(n)

insertItem(k,e) Insert an item with element e and key k into the dictionary. O(1)

removeElement(k) Remove an item with key k from the table, and return its element.1 O(n)

removeAllElements(k) Remove all items with keys k and return an iterator of their elements. O(n)

size() Return the number of items in the dictionary. O(1)

isEmpty() Return a Boolean indicating whether the dictionary is empty. O(1)

1 Return NO_SUCH_KEY if the item with key k doesn’t exist. Note: Another data type, log files, are unordered dictionaries without any form of hashing (i.e. an unordered sequence implementation of a dictionary).

Page 18: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 14

1.14 Look-Up Table Ordered vector implementations of dictionaries, also known as look-up tables or search tables, have the advantage of being able to use the binary search algorithm, thus cutting the time required for a findElement down to O(log2 n), and the time required for a findAllElements down to O(log2 n + s), where s is the size of the iterator. Aside from those that dictionaries use, look-up tables have a few additional functions.

closestKeyBefore(k) Return the key of the item with the largest key less than or equal to k.1 O(log2 n)

closestElemBefore(k) Return the element of the item with the largest key less than or equal to k.1 O(log2 n)

closestKeyAfter(k) Return the key of the item with the smallest key greater than or equal to k.1 O(log2 n)

closestElemAfter(k) Return the element of the item with the smallest key greater than or equal to k.1 O(log2 n)

1 Return NO_SUCH_KEY if no item satisfies the query.

Figure 1.11 Schematic depiction of a look-up table. The boxes below the dictionary keys depict the element entry.

0 N-1

4 6 9 12 15 16 18 28 34 Dictionary

Vector

Page 19: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

15

1.15 Skip List A skip lists for a set S of distinct (key, element) items is a series of lists S0, S1, …, Sh, such that:

- Each list Sh contains the special keys –∞ and +∞. - Each list Si is a subsequence of the previous one, i.e.,

S0 ⊇ S1 ⊇ … ⊇ Sh - List S0 contains all of the keys, and thus also all keys in Si, in ascending

order. - List Sh contains only the two special keys.

Insertions in a skip list use a randomized algorithm. An example is shown in figure 1.12.

- Repeatedly toss a coin until we get tails; denote the number of time heads came up as i.

- If i ≥ h, then we need to add the following new list(s) to the skip list: Sh+1, …, Si+1, each containing only the two special keys.

- We mark the largest items who’s key is less than x as p0, p1, …, pi for each list S0, S1, …, Si.

- For each list S0 to Si, we insert item (x, o) after the respective position p.

Insert key 16, with i = 1

–∞ +∞

+∞

S1

S0

–∞ +∞ S2 p2

p1

p0 12 16

16

–∞

S1

S0 p0

p1 –∞ +∞

–∞ +∞ 12

Insert key 12, with i = 0

–∞ +∞ S0

Empty skip list

Figure 1.12 Schematic depiction of insertions in a skip list. Remember that i is determined randomly.

Page 20: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 16

Searches in a skip list require stepping over, similar to insertions. An example is shown in figure 1.13.

1 We start at the first position of the top list Sh, searching for key x. 2 At the current position p, we compare its x with the key y to the right of p.

- If x = y, then return the element of y. - If x > y, then move the position one to the right and repeat step 2. - If x < y, then drop down to the next list and repeat step 2. However, if we drop down off the last list (i.e. drop down off of list S0),

return NULL, because x is not in the skip list.

16

21

Search for key 21

–∞ +∞

+∞

S1

S0

–∞ +∞ S2

16 –∞

Figure 1.13 Schematic depiction of a search in a skip list. The dashed arrows indicate that the right position is visited, but the associated element is too great (i.e. don’t step to the right).

Page 21: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

17

Removals from a skip list require position flags and stepping over, similar to insertions. An example is shown in figure 1.14.

- Search the skip list for x, and flag the positions with p0, p1, …, pj, where j is the index of the highest list where x was found.

- Remove the positions S0, S1, …, Sj from the lists S0, S1, …, Sj. - Remove Sj+1 to Sh, if that particular list only contains the two special keys

(i.e. only leave one list with the two special keys).

Note: The nature of a node in a skip list requires that it links to all its neighbors, both horizontally and vertically. Therefore, a quad-node is required, depicted in figure 1.15.

Figure 1.14 Schematic depiction a removal in a skip list. List S2 can be removed as well, because list S1 already contains only the two special keys.

16

Remove key 16

–∞ +∞

+∞

S1

S0

–∞ +∞ S2

p1

p0 12 16 –∞

x

Figure 1.15 Schematic depiction a quad-node.

Page 22: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 18

Page 23: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

19

2.1 Bubble-Sort The bubble-sort algorithm is used to sort an unordered sequence in O(n2) time, because in the worst case, the algorithm must do n passes on n items in the sequence. Figure 2.1 visualizes the workings of the algorithm.

5 7 2 6 9 3

5 2 6 7 3 9

2 5 6 3 7 9

2 5 3 6 7 9

2 3 5 6 7 9

1st

2nd

3rd

4th

(a)

(b)

Figure 2.1 Schematic depiction of a bubble-sort on an unordered sequence. (a) Original sequence, (b) Sorting, taking four passes, each taking O(n) time.

Page 24: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 20

2.2 Preorder Traversal The preorder traversal algorithm visits each node of a tree, by first visiting the parent, then each of its children. See figure 2.2.

Figure 2.2 Schematic depiction of a preorder traversal on a binary tree.

1

2

3 4 6

5

Page 25: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

21

2.3 Postorder Traversal The postorder traversal algorithm visits each node of a tree, by visiting each child node before visiting the parent node itself. See figure 2.3.

Figure 2.3 Schematic depiction of a postorder traversal on a binary tree.

6

3

1 2 4

5

Page 26: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 22

2.4 Inorder Traversal The inorder traversal algorithm visits each node of a tree, by first the left child, then the parent node, then the right child. See figure 2.4.

Figure 2.3 Schematic depiction of a inorder traversal on a binary tree.

4

2

1 3 6

5

Page 27: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

23

2.5 Euler Tour Traversal The Euler tour traversal algorithm is a combination of the previously described traversals, making it now possible to ‘walk’ around the tree. This is very convenient for printing an expression from a tree. See figure 2.5.

Furthermore, three functions must be defined: visitLeft, visitBelow, visitRight and visitElement.

visitLeft(v,r) Function determining the result to append to result r before visiting the left child.

visitBelow(v,r) Function determining the result to append to result r after visiting the left child and before visiting the right child.

visitRight(v,r) Function determining the result to append to result r before visiting the right child.

visitExternal(v,r) Function determining the result to append to result r when visiting an external node v.

An example: visitLeft(v,r) appends “(“ to r. visitBelow(v,r) and visitExternal(v,r) appends the element of node v to r. visitRight(v,r) appends “)” to r. The result of the Euler tour shown in figure 2.5, using the example functions would return: r = (((A × B) + (C – D)) × (E – (F ÷ G)))

Figure 2.5 Schematic depiction of a Euler tour traversal on a binary tree.

17

1,15,25

2,8,14

3,5,7 9,11,13 19,21,23

16,18,24

4 6 10 12 20 22

×

+ –

× – ÷

A B C D F G

E

Page 28: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 24

2.6 Last Node in Heap When adding a new node, the heap must determine the new position for the last node. This can be done by following these steps:

- Go up until a left child or the root is reached. - If a left child is reached, go to its sibling. - Go down left until a leaf is reached.

Note: This only works if you start from the previous last node. Note: If the previous last node is a left child, go directly to its sibling.

Figure 2.6 Schematic depiction of locating the last node in a heap.

4

5

15 9 8

6

new nodelast node

Page 29: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

25

2.7 Upheap Upon inserting the node k, keep swapping it with its parent until k reaches the root or k reaches a node that is smaller or equal to k. This algorithm runs in O(log2 n) time, because the maximum height of the heap is log2 n. In figure 2.7 we show the insertion of a value 1 into an existing heap, and the upheap algorithm that follows.

Figure 2.7 Schematic depiction of an insertion on a heap. Note that the external nodes aren’t drawn.

4

5

15 9 1

6

new node

1

5

15 9 6

4

4

5

15 9 6

1

Page 30: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 26

2.8 Downheap Upon removing the minimum node, which is per definition the root of the heap, replace the root with key k, the last node. Keep swapping k with its smallest child, as long as the child is less than or equal to k. This algorithm runs in O(log2 n) time, because the maximum height of the heap is log2 n. In figure 2.8 we show the the downheap algorithm that follows from a removeMin.

Figure 2.8 Schematic depiction of a removeMin on a heap. Note that the external nodes aren’t drawn.

5

9

15

6

9

5

15

6

4

5

15 9

6

root

last node

Page 31: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

27

2.9 Binary Search The advantage of using a array-based vector on an ordered dictionary with n items, is the possibility of using a binary search. The binary search algorithm cuts the findElement and findAllElements times down to O(log2 n) and O(log2 n + s), respectively, where s is the size of the iterator returned by findAllElements. The algorithm works as follows:

1 We declare three variables: low, mid and high. Initially, we set these values: low = 0 high = n – 1

2 Calculate mid:

mid = ⎥⎦⎥

⎢⎣⎢ +

2highlow

3 Evaluate the our search key k with key(mid): - k = key(mid)

We found the item we were looking for, so return elem(mid). - k < key(mid)

The key we’re looking for is further left. high = mid – 1 Continue from step 2.

- k > key(mid) The key we’re looking for is further right. low = mid + 1 Continue from step 2.

When returning to step 2, however, return NULL instead when low = mid = high, because this means the key we’re looking for isn’t in the dictionary.

Figure 2.9 Schematic depiction of binary search on a array-based vector. In a dictionary implementation, each key would link to an element (see figure 1.11).

0 N-1

low high mid

2 4 5 7 8 9 12 14 17 19

2 4 5 7 8 9 12 14 17 19

low high mid

2 4 5 7 8 9 12 14 17 19

low high

mid

Page 32: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types 28

Page 33: Data Structures: An overview of Abstract Data Types

Data Structures: An overview of Abstract Data Types

29

Index after ........................................................4, 5 atRank .........................................................5 array, ...........................................................1 extension ..............................................3 balanced tree ...............................................7 before ......................................................4, 5 bubble-sort ................................................19 binary search..............................................27 binary tree, ..............................................6, 7 properties of .........................................7 representations of .................................8 children ...................................................6, 7 closestKeyAfter, closestKeyBefore ...........14 closestElemAfter, closestElemBefore .......14 compare ......................................................9 complete tree ............................................10 deque ..........................................................2 dequeue........................................................2 depth ...........................................................6 distinct item ..............................................11 double hashing ..........................................12 double-ended queue ....................................2 dictionary ..................................................13 enqueue .......................................................2 elemAtRank .............................................3, 5 element .......................................................2 element ........................................................6 elements ................................................6, 13 Euler tour traversal ...................................23 findAllElements, findElement ...................13 first ......................................................2, 4, 5 front ............................................................2 get .............................................................11 hash table ..................................................12 hash function ............................................12 heap, .........................................................10 last node in .........................................24 downheap ............................................26 upheap.................................................25 height ......................................................6, 7 index .........................................................12 inorder traversal ........................................22 insertAtRank ...........................................3, 5 insertBefore, insertAfter .........................4, 5 insertFirst, insertLast .........................2, 4, 5 insertItem ..............................................9, 13 isFirst, isLast ..............................................4 isInternal, isExternal, isRoot ......................6 iterator ....................................................6, 7 key ..............................................................9 keys .....................................................11, 13

last ...................................................... 2, 4, 5 leftChild ...................................................... 7 level numbering .......................................... 8 linear probing ........................................... 12 linked list .................................................... 1 list ............................................................... 4 log files ..................................................... 13 look-up table ............................................. 14 map ........................................................... 11 minElement, minKey .................................. 9 node(s), ................................................... 4, 6 number of ............................................. 7 object .......................................................... 1 parent .......................................................... 6 position ....................................................... 4 positions ..................................................... 6 priority queue ............................................. 9 pop .............................................................. 1 postorder traversal .................................... 21 preorder traversal ..................................... 20 push ............................................................ 1 put ............................................................. 11 quad-node ................................................. 15 quadratic probing ..................................... 12 queue .......................................................... 2 random ...................................................... 15 rankOf ........................................................ 5 remove .............................................. 4, 5, 11 removeAllElements ................................... 13 removeAtRank ........................................ 3, 5 removeFirst, removeLast ........................ 2, 4 removeElement ......................................... 13 removeMin .................................................. 9 replaceAtRank ........................................ 3, 5 replaceElement ................................... 4, 5, 6 rightChild ................................................... 7 root ............................................................. 6 sequence ..................................................... 5 sibling ......................................................... 7 skip list ..................................................... 15 special keys –∞ and +∞ ............................ 15 stack ........................................................... 1 step over, step down ................................. 15 swapElements ..................................... 4, 5, 6 top ............................................................... 1 tree, ............................................................. 6 walking / printing ............................... 23 unbalanced tree ........................................... 7 value ........................................................... 9 values ........................................................ 11 vector .......................................................... 3