adt Flashcards

1
Q

What methods: Stack Interface

A

1) pop
2) push
3) size
4) isEmpty
5) top

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe push(o)

A

Inserts object o onto top of stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe pop()

A

Removes the top object of stack and returns it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe size()

A

Returns the number of objects in stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe isEmpty()

A

Return a boolean indicating if stack is empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe top()

A

Return the top object of the stack, without removing it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name one limitation of a basic array implementation of a stack. What are some possible solutions?

A

In short:

The linked-list approach has worst-case O(1) guarantees on each operation; the dynamic array has amortized O(1) guarantees.
The locality of the linked list is not as good as the locality of the dynamic array.
The total overhead of the dynamic array is likely to be smaller than the total overhead of the linked list, assuming both store pointers to their elements.
The total overhead of the dynamic array is likely to be greater than that of the linked list if the elements are stored directly.
Neither of these structures is clearly “better” than the other. It really depends on your use case. The best way to figure out which is faster would be to time both and see which performs better.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the orders of the methods in the array based stack?

A

1) pop O(1)
2) push O(1)
3) size O(1)
4) isEmpty O(1)
5) top O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the orders of the methods in linked-based stack

A

1) pop O(1)
2) push O(1)
3) size O(1)
4) isEmpty O(1)
5) top O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the best implementation of a stack?Array or Linked-List? Explain

A

The linked list may be better because it doesn’t have a fixed size. Also, when the array-stack is made extensible it become less fast: pop() and push become O(n) vs. 0(1) in the linked-stack. The array based implementation will also need a block of free contiguous memory, whereas the linked list can be scattered around memory using references.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the Queue methods?

A

) Enqueue

2) Dequeue
3) Front
4) size
5) isEmpty

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the orders of methods in linked-queue

A

All O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a naive array-based queue?

A

Because the dequeue method leaves empty unused spaces behind it, it’s wasteful. The data continually crawls up along array moving to the right. The problem can be fixed by using a circular array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the formula for the size of a queue in a circular array?

A

Size = (N + rear - front) % N

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a deque?

A

<p><p><p>Pronounced 'deck', it is a queue that allows insertion/deletion from either end of the queue.</p></p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the methods of the deque?

A

1) insertFirst(o)
2) insertLast(o)
3) removeFirst()
4) removeLast()
5) size
6) isEmpty
7) front
8) rear

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can a dequeue be implemented?

A

1) Circular Array

2) Doubly Linked List

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a vector?

A

Stacks and Queues are examples of ADTs that support insertion and removal at pre-specified points.
Stacks: insertion / removal at the top
Queues: insertion at the tail / removal at the head

Sequence ADT’s allow for insertion/removal anywhere in the data. A vector is just one type of sequence ADT and one which uses rank to access its elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are the core methods of a vector ADT

A

1) elemAtRank(r)
2) replaceAtRank(r,e)
3) insertAtRank(r,e)
4) removeAtRank(r)
5) isEmpty()
6) size()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the orders of the methods in array based Vector ADT.

A

1) elemAtRank(r) O(1)
2) replaceAtRank(r,e) O(1)
3) insertAtRank(r,e) O(n) * not amortized
4) removeAtRank(r) O(n) * not amortized
5) isEmpty() O(1)
6) size() O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the Order of inserting into an extendable array?

A

Despite having a loop, extendable arrays give amortized O(1) insertion, and not O(n). This is because when you spread the cost of all the operations out you find that it is possible (in theory) to save up operation-tokens such that you can afford a copying of the element into a bigger array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is better? A linked-list, or an array-based implementation of a vector?

A

The orders for a linked appraoch would be:

1) elemAtRank(r) O(n) * bad
2) replaceAtRank(r,e) O(n) * bad
3) insertAtRank(r,e) O(n)
4) removeAtRank(r) O(n)
5) isEmpty() O(1)
6) size() O(1)

For replaceAtRank and insertAtRank the need to shift elements has been replaced with the need to traverse them giving O(n) time. However, the need to traverse for
elemAtRank and replaceAtRank gives additional cost. Thus, the array based approach is superior, especially from an amortized perspective.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are the operations of the list-based map adt

A

1) size()
2) isEmpty()
3) get(k)
4) put(k,v)
5) remove(k)
6) keys()
7) values()
8) entries()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How can we think of map, in terms of entries?

A

<p><p><p>You can think of a list based map as a list of entries. An entry is simply a single pairing of a key and value (K,V).</p></p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the adapter pattern?

A

In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used from another interface.[1] It is often used to make existing classes work with others without modifying their source code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What are the ways you can implement a map?

A

1) Linked List ( A list of entries)

2) Arrays (Hash mapping)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is a tree?

A

A Tree is a hierarchical ADT where data is related in terms of parent-child relationships.
Each element (node) in the tree has at most 1 parent.
Each element (node) may have 0 or more children.
Each tree will include exactly one element (node), known as the root, which has no parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What are the operations of a tree?

A

root() r-> the Position for the root
parent(p) r-> the Position of p’s parent
children(p) r-> an Iterator of the Positions of p’s children
isInternal(p) r-> does p have children?
isExternal(p) r-> is p a leaf?
isRoot(p) r->is p==root()?
size() r->number of nodes
isEmpty() r->tests whether or not the tree is empty
iterator() r-> an Iterator of every element in the tree
positions() r-> an Iterator of every Position in the tree
replace(p, e) r-> the element at Position p with e

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Describe the steps by which items are removed from a heap.

A
  1. Remove the root node (current min entry)
  2. Replace this node with the last item in the heap, node L
  3. Perform a downheap to re-establish the ordering property.
  4. Update the last reference.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Two ways hash map can be implemented?

A

1) Hash code map: Assign an integer to every key

2) Compression map: Concerts an Int to int in the range (0 - N-1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How do you make a compression map?</p></p></p>

A

A compression map sometimes uses:
1) The division method (%n)
This method requires that n be prime so that their are an even spread of hash integers, and not too many zeros.</p></p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is the MAD method in relation to hash maps?

A

MAD stands for multiply, add, divide. It’s a way of deriving a fairly even spread of integers for a compression map by doing the following:

1) multiplying by some constant
2) shifting the number by some contant
3) modulus the answer by some number.

(ai + b) % N

Constraint: a % N cannot be equal to zero.</p></p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

One application of binary trees is a heap. Heaps make use of two key properties: the order and structural completeness properties. Give definitions for each of these properties.</p></p></p>

A

Order Property: key(parent) ≤ key(child)
Structural “Completeness” Property: all levels are full, except the bottom, which is “left-filled”
</p></p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

How could you use a hash map if your keys were primitive values?</p></p></p>

A

1) Integer Cast: You can translate the bits associated with the primitive into an integer.
2) Component Sum: Break the bits into integer size blocks, cast them to integers, and sum these integers.
3) Polynomial Sum: same as component sum, but multiply each term by a constant polynomial coefficient:
</p></p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Why is polynomial sum better than component sum for deriving integers from primitives?</p></p></p>

A

Naïve solution: Use component sum
h(“dog”) = (int) ‘d’ + (int) ‘o’ + (int) ‘g’ = 100 + 111 + 103 = 314
h(“god”) = (int) ‘g’ + (int) ‘o’ + (int) ‘d’ = 103 + 111 + 100 = 314 !?!?!

Better solution: Use polynomial sum (p=3):
h(“god”) = 103 + 1113 + 1009 = 1,336
h(“dog”) = 100 + 1113 + 1039 = 1,360

For 50,000 English words, a value of p = 33, results in less than 7 collisions!!!</p></p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

In the open addressing strategy of collision-avoidance, what can be done when a collision occurs?

A

1) Linear Probing
2) Double Hashing
3) Re Hashing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What problem might arise with linear probing, which needs to resolved?

A

When items are removed from the hash-map,they will leave behind nulls. These nulls will cause a linear probing algorithm to stop running because it thinks the key being searched for doesn’t exist in the hash map. Otherwise it would have been inserted into the first empty space when probing after a collision. The solution is to enter in AVAILABLE tokens after a remove operation, so that linear probing will continue…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Advantages of the open address collision strategy?

A

Do not require an auxiliary data structure

Have finite capacity but support rehashing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Describe the Last Occurrence function

A

Boyer-Moore’s algorithm preprocesses the pattern P and the alpha (weird sigma) to build the last-occurrence L mapping sigma to integers where L(c) is defined as;
> the largest index i such that p[i] = c or
> -1 if no such index exists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Pseudo-code Boyer Moore:

A

Algorithm BoyerMooreMatch(T,P,Sigma)
Input: Text T of size n and pattern P of size m
Output: Starting index of a substring of T equal to P or -1 if no such substring exists

L -> lastOccurrenceMap(P,Sigma)
i -> m - 1
j -> m - 1

do
       if  T[i] = P[j] then
          if j = 0 then return 1
          else
             i -> i - 1
             j -> j - 1
       else
         l -> lastOccurrence(L, T[i])
         i -> i + m - min(j, l + 1)
         j -> m - 1

until i < n - 1
return -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Example of Boyer Moore algorithm

A

finish this answer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What is a binary search tree?

re-check answer

A

A binary search tree is a tree that satisfies the following properties:

Each internal node holds a (unique) value.
A total-order relation (~) is defined on those values.
Reflexive: k ~ k
Antisymmetric: if k1 ~ k2 and k2 ~ k1 then k1 = k2
Transitive: if k1 ~ k2 and k2 ~ k3 then k1 ~ k3
All the values (ki) in the left sub-tree of a node with value k satisfy the relation ki ~ k.
All the values (kj) in the right sub-tree of a node with value k satisfy the relation k ~ kj

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

> In the open addressing strategy of collision-avoidance, what can be done when a collision occurs?

A

1) Linear Probing: When a collision occurs you can probe along the map in ones until you find an empty. Later, when you wish to find this entry, you will probe in this linear fashion until you meet either k (found it), null (k doesn’t exit), or N cells have been traversed (k doesn’t exist).
</p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What problem might arise with linear probing, which needs to resolved?

A

When items are removed from the hash-map,they will leave behind nulls. These nulls will cause a linear probing algorithm to stop running because it thinks the key being searched for doesn’t exist in the hash map. Otherwise it would have been inserted into the first empty space when probing after a collision.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What is an AVL Tree? Explain any properties that an AVL tree must satisfy

A

AVL trees are an example of a self-balancing binary search tree.
AVL trees must satisfy the height-balance property:
For every internal node v of T, the heights of the children of v can differ by at most 1. AVL trees have expected performance O(log n).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

<p>Lets say don't want our hash map to require the use of an auxiliary data structure (list) so use the open addressing scheme as our collision strategy. What are our options within this scheme for dealing with collisions?</p>

A

1) Linear Probing
2) Quadratic Probing
3) Double hashing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

Describe with diagrams trinode restructuring.

A

nothing here

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

<p>What is double hashing?</p>

A

<p>Double Hashing is an open addressing solution. If our original compression function (division method, mad method) results in a collision (the key already has a value) then we use a secondary hash function to probe the hash map until we find an empty bucket.

Lets say our original compression function was i = h(k) giving our position in the hash map as a[i]. Following a collision, we now use h'(K) in the following way to probe for an empty bucket:

a[ (i + j*h'(k)) % N]
where j = 0,1,2,3,..... until a bucket is found (j allows probing)

But how do we derive h'(k)?

A common h'(k) is h'(k) = q - (k mod q) where q is a prime number smaller than N and remember, N must also be prime.

</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

<p>What is linear probing?</p>

A

<p>When a collision occurs you can probe along the map in ones until you find an empty. Later, when you wish to find this entry, you will probe in this linear fashion until you meet either k (found it), null (k doesn't exit), or N cells have been traversed (k doesn't exist).</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What is a Queue? List the operations commonly associated with the Queue Abstract Data Type.

A

A queue is a container of objects / values. Insertion and removal based on first-in-first-out (FIFO) principle. Objects can be inserted at any time, but only the object that has been in the queue the longest can be removed.

Items can be enqueued (insertion) or dequeued (removal).The front of the queue is the next item to be dequeued. The rear of the queue is where the last item was enqueued.

Core Operations:

enqueue(v): Inserts value v onto rear of queue

dequeue(): Removes the value at the front of the queue and returns it

front(): Return the value at the front of the queue, without removing it

Support Operations:

size()

isEmpty():

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

<p>What is a better collision strategy: open addressing or collision chaining?</p>

A

<p>Collision chaining is usually competitive or faster than alternative methods depending on the load factor of the bucket array? If memory is in short supply their may an argument for choosing an open address strategy.</p>

52
Q

<p>What are the techniques used to traverse a tree?</p>

A

1) pre-order traversal
2) in-order traversal
3) post-order traversal
4) Euler traversal
5) breadth first traversal

53
Q

<p>What are the two parts of a hash function?</p>

A

<p>The hash code and the compression function? The hash code is the mapping of the key (which not be an int) into an integer. The compression function is what compresses these hash code intgers into a narrower range of integers.</p>

54
Q

How to dequeue in a Queue - Give pseudocode

re-check this answer

A

Algorithm dequeue()

Input: none

Output: the top object

T <- size - 1

return T.element

55
Q

<p>List several common techniques of deriving hash codes from keys..</p>

A

1) Casting to Integer: (for byte, short, int, char)
2) Summing components (for doubles and longs)
3) Polynomial Hash Codes (Dog & God)
4) Cyclic shift hash codes
5) For objects use the memory address

56
Q

Name some compression functions…

A

1) The division method

2) The MAD method

57
Q

So far we have discussed hash codes and compression functions which collectively make up hash functions. However, once a good is hash function is chosen collisions will still occur form time to time. Name some collision handling schemes?

A

Two broad collision handling schemes are

1) Separate chaining (an array of lists which hold nodes k and elements v)
2) Open addressing

58
Q

<p>Describe load factor with reference to hash maps?</p>

A

<p>If a map has to store n entries into N buckets we hope to achieve a number of n/N entries in each bucket. This number is called the loading factor. For a set number of buckets, the speed of operations will slow down with the number of entries. To ensure a fast hash map the loading factor should be kept below some bound. A very low load factor is also wasteful (approaching zero) because it suggests their are too many empty buckets taking up memory. </p>

59
Q

<p>What are some basic facts/rules about a tree?</p>

A

<p>Each element (node) in the tree has at most 1 parent.
Each element (node) may have 0 or more children.
Each tree will include exactly one element (node), known as the root, which has no parent.</p>

60
Q

<p>Can a node in a tree have no parent?</p>

A

<p>Yes, the root will never have a parent</p>

61
Q

<p>What are the methods for a tree structure?</p>

A

<p>Trees make use of the Position ADT and have the following operations:

11 in total:

root() returns the Position for the root
parent(p) returns the Position of p’s parent
children(p) returns an Iterator of the Positions of p’s children
isInternal(p) does p have children?
isExternal(p) is p a leaf?
isRoot(p) is p==root()?
size() number of nodes
isEmpty() tests whether or not the tree is empty
iterator() returns an Iterator of every element in the tree
positions() returns an Iterator of every Position in the tree
replace(p, e) replaces the element at Position p with e</p>

62
Q

<p>What are the properties of a binary tree?</p>

A

<p>A Binary Tree is a tree with the following properties:
Every node has at most 2 children (degree 2)
Each child node is labelled as being either a left child or a right child
A left child precedes a right child in the ordering of children of a node</p>

63
Q

<p>What is a &quot;proper&quot; binary tree?</p>

A

<p>A tree where all nodes have a degree = 0 or degree = 2</p>

64
Q

<p>What are the techniques used to traverse a tree?</p>

A

<p>1) pre-order traversal

2) in-order traversal
3) post-order traversal
4) Euler traversal
5) breadth first traversal</p>

65
Q

<p><p>What is a circular array and how can it be used to implement a queue?</p>
</p>

A

Logically join the first and last cells in the array: When front (or rear) reaches the end of the array, perform a
wraparound by setting it to 0

66
Q

<p>What order are the nodes visited in for each of the following:

1) pre-order traversal
2) in-order traversal
3) post-order traversal
4) Euler traversal
5) breadth first traversal</p>

A

<p>1) pre-order traversal
(root, left, right)
(processed as the node is visited)

2) in-order traversal
(Left, Root, Right)
(Ascending)

3) post-order traversal
(Left, Right, Root)
(Node not processed till after its children)

4) Euler traversal

These common traversals can be represented as a single algorithm by assuming that we visit each node three times. An Euler tour is a walk around the binary tree where each edge is treated as a wall, which you cannot cross. In this walk each node will be visited either on the left, or under the below, or on the right. The Euler tour in which we visit nodes on the left produces a preorder traversal. When we visit nodes from the below, we get an inorder traversal. And when we visit nodes on the right, we get a postorder traversal.

5) breadth first traversal</p>

67
Q

<p>What properties must be satisfied for a binary search tree?</p>

A

<p>1) Each internal node holds a (unique) value.

2) A total-order relation (~) is defined on those values.
* Reflexive: k ~ k
* Antisymmetric: if k1 ~ k2 and k2 ~ k1 then k1 = k2
* Transitive: if k1 ~ k2 and k2 ~ k3 then k1 ~ k3
3) All the values (ki) in the left sub-tree of a node with value k satisfy the relation ki ~ k.
4) All the values (kj) in the right sub-tree of a node with value k satisfy the relation k ~ kj.</p>

68
Q

<p>What is a useful feature of a binary search tree?</p>

A

<p>An in-order traversal of a binary search trees visits the values in the order specified by the total-order relation.</p>

69
Q

<p>Binary Search trees support three main operations. What are they?</p>

A

<p>1) insert(e)

2) find(e)
3) remove(e)</p>

70
Q

<p>Can we delete a node v with two children from a binary search tree bst?</p>

A

<p>Yes, but we must do the following:

1) we find the internal node w that follows v in an in-order traversal
2) we then copy w.element() into node v (cause v is going)
3) we then remove node w and its left child z (which must be a leaf)</p>

71
Q

<p>What is the order of operations performed on a binary search tree?</p>

A

<p>In the worst case, when a tree of n nodes has a height of n, the order is O(N). But in a perfectly balanced tree the orderis O(log n)</p>

72
Q

<p>Quick Recap. Turn over.</p>

A

<p>Trees are Hierarchical ADT’s in which data is defined in terms of parent and child relationships between nodes.
A Proper Binary Tree is a Tree in which each node has either 0 (external) or 2 (internal) children.

A Binary Search Tree is a Binary Tree that holds values whose location is determined by a total order relation.

Binary Search Trees are another approach to implementing Maps and Dictionaries that have best case performance O(log n).

Performance depends on the height of the tree, and is best when the tree is balanced.</p>

73
Q

<p>What is special about AVL trees?</p>

A

<p>They are self-balancing. They achieve this by satisfying the height-balance property.</p>

74
Q

<p>What is the height-balance property of an avl tree?</p>

A

<p>For every internal node v of T, the heights of the children of v can differ by at most 1.</p>

75
Q

<p>What is the performance of an avl tree?</p>

A

<p>Because the tree is always balanced, the expected performance of this operation is O(log n)</p>

76
Q

What is a deque? What are its core operations?

A
A deque (pronounced deck) is a container of objects / values. Insertion and removal are based on the first or last-in first or last-out (FLI-FLO) principle. 
Terminology: ltems can be inserted at the front or back of the deque. ltems can be removed at the front or back of the deque: We insert and remove from both the front and the back.

Core Operations:

insertFirst(o): Inserts object o onto front of the deque
insertLast(o): Inserts object o onto the rear of the deque
removeFirst(): ….and returns it
removeLast(): …and returns it

Support Operations:

size():
isEmpty():
front(): Return the front object in the deque
rear(): Return the rear object in the deque

77
Q

<p></p>

A

<p></p>

78
Q

Define a vector.

A

A Vector supports insertion, removal and accessing of objects based on rank.

Terminology:Rank: The rank of an object e is an integer value that specifies the number of objects that come before e in vector.

79
Q

What are the core operations of a Vector?

A

Core Operations:

elemAtRank(r): an error condition occurs if the vector is empty or r < 0 or r > size()

replaceAtRank(r, e): error condition occurs if the vector is empty or r < 0 or r > size()

insertAtRank(r, e): an error condition occurs if r < 0 or r > size()

removeAtRank(r): an error condition occurs if S is empty or r < 0 or r > size()

Support Operations:

isEmpty()
size()

80
Q

What is the height of an avl tree storing n keys?

A

O(log n)

81
Q

Prove that the height of an avl tree storing n keys is O(log n)

A

Okay, before we begin lets state two truths:

1) A tree of size 1, has a height of 1 (n=1)
2) A tree of size two has a height of 2 (n=2)

What about a tree with more than 2 keys? This will be:
n(h) = 1 + n(h-1) +n(h-2) (Formula 1)

Because the height of the left sub-tree and right sub-tree will always differ by one in a balanced tree with the minimum amount of nodes contained it.

Obviously n(h-1) will be higher than n(h2):
     n(h-1) > n(h-2)

Now, if we substitute n(h-1) with n(h-2) in formula (1) we get
something interesting:

n(h) > 2 x n(h-2) formula(2)

It follow from this,that:

n(h) > 4 x n(h-4)…
n(h) > 8 x n(h-6)…

We can summarize this behaviour:

n(h) = 2**i(n)(h-2(i)) formula(3)

If we had a value for n(h-2(i)) we could sub it into formula(2) and we know that n(2) =2 and n(1) = 1.

So, n(h-2i) = n(2), thus (h-2i) = 2
This also means, h/2 -1 = i

Now plug this into formula(2)

     n(h) > 2**(h/2)-1 .  n (h – 2 . ( (h/2) – 1 ) )
 n(h) > 2**(h/2)-1 .  n (h – 2 (h/2) + 2 )
 n(h) > 2**(h/2)-1 .  n(2) 	           where n(2) = 2
 n(h) > 2**(h/2)-1 .  2**1
 n(h) > 2**(h/2)

Taking logarithms:
log n(h) > log ( 2 (h/2) )
log n(h) > h/2
h < 2. log n(h)

Thus the height of an AVL tree is O(log n)!

82
Q

What is splaying?

A

Given an internal node x of a binary search tree T, we splay x by moving x to the root of T through a sequence of restructurings.

83
Q

What are the types of restructuring available in a splay tree?

A

1) Zig
2) Zig-Zig
3) Zig-Zag

84
Q

When do we use a zig?

A

When x, the value being inserted, has no grandparent.

85
Q

When do we use a zig-zig?

A

When x and y are both right children, or they are both left children.

86
Q

How good is performance of splaying?

A

Using amortized analysis we can see that splaying is O(log n) BUT its hard to prove.

87
Q

What node gets splayed when using the find operation?

A

Either the found node, or the parent of ending external node.

88
Q

What node gets splayed when using the insert operation?

A

Splay the new node containing the inserted entry

89
Q

What node gets splayed when using the remove operation?

A

First splay the tree around the node to be removed. Then remove it, and make the largest left child the new root.

90
Q

What are the key operations of a priority queue?

A

1) Insert(k,e) Insert a new element e with key k into P
2) min() Return (but don’t remove) an element of P with highest priority; error occurs if P empty.
3) remove() Remove from P and return an element with the highest priority; an error occurs if P is empty

Support Operations:

4) isEmpty() Returns true if the priority queue is empty, or false otherwise
5) size() Returns the number of elements in the priority queue

91
Q

<p>What are Sequence ADTs?</p>

A

<p>ADTs that support insertion and removal at various points</p>

92
Q

<p>What is the List ADT?</p>

A

<p>List ADT is a sequence data structure that abstracts the notion of a &ldquo;place&rdquo; in a list.</p>

93
Q

What is a priority queue?

A

A priority queue is an abstract data type for storing a collection of prioritized elements.
Like a queue but, removal is based on a priority

94
Q

What do priority queues hold?

A

Priority Queues hold entries (key + value) – the key is the priority

95
Q

What element can be removed from a priority queue?

A

Only the element with the highest priority can be removed

96
Q

<p>What is a position?</p>

A

<p>A position is a place in the list that a piece of data is stored.</p>

97
Q

What are the key operations of a priority queue?

A

1) Insert(k,e) Insert a new element e with key k into P
2) min() Return (but don’t remove) an element of P with highest priority; error occurs if P empty.
3) remove() Remove from P and return an element with the highest priority; an error occurs if P is empty

98
Q

What are the core operations of a List?

A

Core Operations:

replace(p,e): r-> the element formerly at p.
insertFirst(e): r-> the position of e.
insertLast(e): r-> the position of e.
insertBefore(p,e) r-> the position of e.
insertAfter(p,e): r-> the position of e.
remove(p): Remove from S the element at position p.

Support Operations:

isEmpty()
size()

99
Q

How are priority queues implemented?

A

List-based Strategies

1) unsorted
2) sorted

Both strategies give O(n) for some operations:

sorted -> insert()
unsorted -> min() and removeMin()

However, sorted is better because it only results in one O(n) operation vs. two with unsorted.

100
Q

How can a list be implemented using links?

A

Link-based Implementation:

Nodes are positions
Nodes maintain ordering information

Link to the next and previous objects in the List.

Auxiliary references maintained; front and rear nodes.

Keep track of the current size of the list

101
Q

Show how doubly-linked lists can represent a List ADT</p>

A

Approach: Use a doubly linked list

A Node is basically a position

Finding adjacent nodes is very fast O(1) and simple to do

Key positions can be easily identified (front, rear)

Need to keep track of the size (size)

102
Q

What is a heap?

A

A heap is a binary tree that stores keys (key-value pairs) at its internal nodes and that satisfies two additional properties:

1) Order Property: key(parent) <= key(child)
2) Structural “Completeness” Property: all levels are full, except the bottom, which is “left-filled”

103
Q

What is the height of a heap?

A

h = [log(N+1)] where N = total entries and [] means absolute value

104
Q

How can we implement a heap?

A

It can be implemented sing a vector. Reading from left to right on the heap you can simply add the values to the vector from left to right, also.

105
Q

Describe the remove operation for a link-based List ADT.

A

Basic Idea: Removes the element at position p from the List
Process (Informal):
Modify the links to remove the node from the list.
Reduce the size
Return the value stored in the node

Potential Use Cases:

  1. Removal of the element at the front position
  2. Removal of the element at the rear position
  3. Removal of the last element
  4. Removal of element at other positions
106
Q

What is the pseudocode for the remove operation of the List?

A

Finish Later

107
Q

In a heap, how do you find the left and right child of node j?

A

Left child = 2*j +1

Right child = 2*j +2

108
Q

How do you find the parent of node j in a heap?

A

(j-1)/2

109
Q

How do we insert in a heap?

A

We insert in three stages.

1) Insert the new entry at the last position in the heap
2) Unheap to restore the order-property.
3) Identfy the new last position in the heap.This means finding the next free space via an in-order traversal. If no available space is found the bottom level must be full, so the new last position will be the leftmost child on the next level.

110
Q

How do you know when to stop up-heaping in a heap?

A

Upheap terminates when new value is greater than the value of its parent, or the top of the heap is reached.

111
Q

How do we delete from a heap (min-heap)

A

If we are deleting the last value in the heap, it’s easy: we just remove the value.

1) If we are removing any other value, we swap that value with the position of the root so that it too can now be deleted easily.
2) However, we must preserve the order-property by down-heaping. This involves checking whether L is less that its children. If it is not, then L is swapped with the smaller of the two children, and downheap is performed again on L in its new position.
3) Update the “last” reference. Need to do an inverse pre-order traversal (I.e. goto the node that is before the last node in a pre-order traversal). If there is no previous node, then find the last node on the next level up.

112
Q

When does a downheap terminate?

A

Downheap terminates when the key is greater than the keys of both its children, or the bottom of the heap is reached.

113
Q

What is heap-sort?

A

Heap sort is where you implement a priority queue ADT using a heap. If it is a min-heap then you add your keys to the heap so that the children are always larger. Remember, that items can only be added the last position of the heap (the leftmost child of the bottom level). However after this key has been added you can recursively swap the key with it’s parents if the parent is bigger. This preserves the order-property of the heap.

When removing from the min-heap, the root will be the smallest key, so we swap the root with the last position (the only removable position).

Now we have removed/returned our first smallest number (sorting) but the min-heap will need to be down-heaped (recursive swapping) again to preserve the order-property.

https://www.youtube.com/watch?v=Uxo7UiMco58

114
Q

Name some ways to implement a priority queue and compare their performance

A

List-based
1) Sorted 0(n) for insert
2) Unsorted o(n) for min() and removeMin()
Heap based
3) O(log n) for inset() and removeMin()
o(1) for everything else.

heap = CLEAR WINNER!

115
Q

How does heap sort compare with other sorting methods?

A

Heap sort is O(log n)
selection sort O(n2)
insertion sort O(n
2)

heapsort = CLEAR WINNER!

116
Q

Say we have an algorithm: 11n + 5. We can guess that it is O(n), but how can we show this?

A

Given functions f(n) and g(n), we say that
f(n) is O(g(n)) if and only if
there are positive constants c and n0 such that

	f(n) ≤ c * g(n) for all n ≥ n0         formula (1)

If we make g(n) in the above formula equal to n, g(n) = n,
and this satisfies formula (1) then we can prove that
11n+5 is O(n)

Make c equal to 12 for simplicity:

11n + 5 ≤ 12n
5 ≤ 12n - 11n
5 ≤ n

Therefore, f(n) ≤ 12 * g(n) for all n ≥ 5

117
Q

What do we consider a good approximation, in terms of O(n)

A

We consider an approximation to be good if it is similar to the actual function, but does not include lower order terms.

118
Q

What is the order of:

1) 11n + 5
2) 8n2log n + 5n2 + n

A

Simple Rule: Drop lower order terms and constant factors:
11n + 5 is O(n)
8n2log n + 5n2 + n is O(n2log n)

119
Q

What is the performance hierarchy,in terms of n?

A

log n &laquo_space;n &laquo_space;n2 &laquo_space;n3 &laquo_space;2n

120
Q

What is a list data type?

A

A list is an ADT in it’s own right, but it’s also part of what are collectively called sequence ADT’s. Another sequence ADT is the vector. However, while vectors use the idea of rank, we define the List ADT to be a sequence data structure that abstracts the notion of a “place” in a list.

121
Q

What is position, in terms of the list sequence ADT?

A

A position is a place in the list that holds a value.

It is an auxiliary ADT for ADTS in which the values are stored at positions. It’s only method is element().

122
Q

How would insert() or remove() be implemented with regard to ADT’s that use position? like lists…

A

Insertion is carried out relative to a position or a known fixed point. For example, insert bob “after” jane or “before” sandra.

123
Q

What are the operations we get with a list ADT?

A

replace(p,e): r-> replaced element
insertFirst(e): r-> position of e
insertLast(e): r -> position of e
insertBefore(p,e): r -> position of e
insertAfter(p,e): . r -> position of e
remove(p):

isEmpty()
size()

first()
last()
next()
prev()

124
Q

What ways can a list be implemented?

A

1) Array list

2) Linked List (Double)

125
Q

What is the best implementation of a list?

A

Linked List is better (Double)

“Linked lists have several advantages over arrays. Elements can be inserted into linked lists indefinitely, while an array will eventually either fill up or need to be resized, an expensive operation that may not even be possible if memory is fragmented. Similarly, an array from which many elements are removed may become wastefully empty or need to be made smaller.”

126
Q

How does an array based list work?

A

The array stores a positions and each position stores its associated index. If insertion/removal occurs then the position must change it’s stored index