Week 3 + 4 + 5 Flashcards

1
Q

Define stack.

A

Container with insertion and removal based on LIFO (Last-In-First-Out).

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

Define push, pop, top with stack.

A

push, inserts onto top of stack
pop, removes off of top of stack
top is last item pushed.

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

How is stack implemented using array?

A

Top of stack is number of elements in stack - 1. pop reduces top by 1 push adds element to top + 1 space and increments top. top = -1 for empty stack.

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

How is stack implemented using linked list?

A

Top of stack is set to first node. pop is removeFirst. push is addFirst. top points to null for empty stack.

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

Explain String Builder syntax. Why use string builder?

A

StringBuilder sb = new StringBuilder();
sb.append(“String”);

+ all other normal string functions.

String builder is mutable so a new String is not created after every modification unlike with normal Strings.

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

Define queue.

A

Container with insertion at back and removal from front. Based on FIFO (First-In-First-Out) data structure.

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

Define enqueue, dequeue and front for queue.

A

Enqueue inserts at rear of queue
Dequeue removes the element at the front of the queue.
Front returns the front of the queue.

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

How is queue implemented using array.

A

Front variable keeps track of front of queue, rear location is always empty. Enqueue grows the rear and dequeuing grows the front variable. Elements crawl to right in array as we modify the queue. We use circular arrays so when we reach the end of the array the elements circle back to the start.

Implementing Circular Array:
For enqueue: Insertion position set to (front + size) % CAPACITY
For dequeue: Front set to (front + 1) % CAPACITY.

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

How is queue implemented using linked list?

A

Doubly Linked List used. Enqueue, performs addLast, Dequeue performs removeFirst.

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

Define deque.

A

A queue where we can insert and delete from both the front and the end.

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

Define a tree.

A

A tree consists of nodes, where each node (except the first) has a parent and all nodes have zero or more children.

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

Define root.

A

First node which has no parent.

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

Define internal node.

A

A node with at least one child.

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

Define external node.

A

A node without children.

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

Define subtree.

A

Tree within a tree consisting of a node and its descendants.

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

Define depth of node.

A

Number of ancestors of p other than p itself.

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

Define height of tree.

A

Maximum depth of any node. The height of its root node.

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

What is the ordering in a tree?

A

Siblings are arranged left to right, from lowest to greatest.

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

Define binary tree.

A

A tree consisting of a single node or a tree whose root has an ordered pair of children each of which is a binary tree.

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

Define height of a node.

A

Number of edges on longest path from the node to a leaf which has a height of 0. Defined recursively. H (Node) = 1 + Height (child node). Where H (leaf) = 0

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

Define proper or full binary tree.

A

A binary tree where each node has either zero or two children.

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

T or F. In a binary tree at level/depth d there is at most 2^d nodes.

A

True

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

In a non-empty binary tree what is the minimum and maximum number of nodes n.

A

h + 1 <= n <= 2^(h + 1) - 1

24
Q

In a non-empty binary tree what is the minimum and maximum number of external nodes.

A

1 <= ne <= 2^h

25
In a non-empty binary tree what is the minimum and maximum number of internal nodes.
h <= ni <= 2^h - 1
26
In a non-empty binary tree what is the minimum and maximum height in terms of the nodes.
log(n + 1) - 1 <= h <= n - 1
27
In a non-empty PROPER binary tree, what is the number of external nodes.
ne = ni + 1
28
Explain inorder traversal.
Left subtree -> Node -> Right subtree
29
Explain preorder traversal.
Node -> Left subtree -> Right subtree. Moves top to bottom.
30
Explain postorder traversal.
Left subtree -> Right subtree -> Node. Moves bottom to top.
31
Explain breadth-first traversal.
Pushes all nodes at a particular depth onto stack and visits them first before moving down to next level. Moves top to bottom.
32
What is the Catalan number?
The number of full binary trees with n + 1 leaves. Cn = 1 / n+1 (2n C n)
33
What is the expected height for a random binary tree of size n = 30.
3.3 log (30) = 11
34
Distinguish between iterable and iterator.
Iterable: Represents a collection that can be iterated over using a for-each loop. When implementing an Iterable, we need to override the iterator() method. Doesn't store the iteration state. Removing elements during the iteration isn't allowed. Iterator: Represents an interface that can be used to iterate over a collection. When implementing an Iterator, we need to override the hasNext() and next() methods. Stores the iteration state Removing elements during the iteration is allowed.
35
Distinguish between the lazy and snapshot implementation of iterator.
Snapshot: Maintains its own private snapshot of element sequence Constructed when iterator object is created Unaffected by subsequent changes to original Lazy: Does not make an upfront copy. Piecewise traversal of original. Affected by subsequent changes to original.
36
What is the definition of the stack ADT?
Stack is a container with insertion and removal based on LIFO (Last-In-First-Out). We can only insert, remove and get at the top of the stack. ADT functions: size() isEmpty() push(e) pop() top()
37
Give two uses of stack.
Web page history in a web browser Undo sequence in a text editor
38
What is the definition of the queue ADT?
Queue is a container with insertion and removal based on FIFO (First-In-First-Out). We can only insert at the rear and get/remove from the front of the queue. ADT functions: size() isEmpty() enqueue(e) dequeue() front()
39
Give two uses of queue.
Waiting lists Access to shared resources
40
What is the definition of the Deque ADT?
A deque is a collection of objects of the same type that is a mixture between a queue and a stack. Supports element insertion and removal at both ends. ADT Functions: size() isEmpty() first() last() addFirst() addLast() removeFirst() removeLast()
41
What is the time complexity of all deque operations with doubly linked list implementation?
All operations O(1) time.
42
What is the time complexity of all queue operations with linked list and array implementation?
Both: All operations O(1) time.
43
What is the time complexity of linked based and array stacks?
Both: All operations O(1) time.
44
What is the worst case running time of the depth function of binary tree?
O(n) is all nodes form a single branch we have a depth of n - 1.
45
What is the definition of the Position ADT?
Position is the abstraction of a node of a tree. An element is stored at each position, and each one satisfies the parent child relationships within a given tree structure. ADT functions getElement() setElement(E e)
46
What is the definition of the Tree ADT? (HINT: 4 accessor, 3 query, 4 general)
A tree consists of nodes, where each node (except the first) has a parent and all nodes have zero or more children. ADT Functions: Accessor: root() parent(p) children(p) numChildren(p) Query: isInternal(p) isExternal(p) isRoot(p) General: size() isEmpty() iterator() positions()
47
Define the binary tree ADT?
A tree consisting of a single node or a tree whose root has an ordered pair of children each of which is a binary tree. So for all nodes z, the left child of z should be less than the right child of z. ADT Functions: left(p) right(p) sibling(p)
48
In a proper binary tree what is the minimum and maximum number of internal nodes and hence, what is the same for external and total.
h <= ni <= 2^h - 1 (same as non-proper) h + 1 <= ne <= 2^h 2h + 1 <= n <= 2^(h + 1) - 1
49
In a proper binary tree, what is the minimum and maximum height? If we have n the number of nodes in the tree how many ni and ne are there?
log(n + 1) - 1 <= h <= (n - 1) / 2 ni = n - 1 / 2 ne = n + 1 / 2
50
Define a binary search tree.
A binary tree where for a given node x, all the values in the left subtree of x is less than x and all the values in the right subtree of x is greater than x.
51
Explain the algorithm for constructing a binary tree from a given inorder and preorder traversal.
Let i be index of inorder[], let j be index of preorder[]. BUILD TREE() Create node with element preorder[j] j++ Set i to element index in inorder. call build tree with elements before i as left subtree of node. call build tree with elements after i as right subtree of node. return node.
52
Explain breadth first traversal algorithm.
Starting at level 0 but levels in array from left to right. Add root to queue Then loop removing first element of queue (adding to result array) and adding all its children at the back starting with the leftmost child. When queue is empty return result array.
52
Explain how a random binary tree is created.
Recursive function that receives random numbers in order. Randomly picks root makes it a node, and makes left subtree with recursive call with all to the left of the index chosen and makes right subtree with recursive call with all to the right of the index chosen.
53
Describe in detail how a queue can be represented using a circular array.
We have two pointers front and rear which point to positions in the array. The front pointer always points to the top of the queue (next element to be removed), the rear always point to a blank space, the next available spot for insertion in the queue. front pointer starts at 0 and is incremented when we remove an item. Rear pointer starts at 0 and is incremented every time we add an item. When either pointer reaches end of array, the pointers can loop around back to the beginning of the array. The array is full when abs(rear - front) = capacity. The rear pointer will be level with the front pointer when the queue is empty.
54
Pseudocode for enqueue and dequeue for queue implemented using a circular array.
enqueue: array[rear] = element rear ++; (mod size) dequeue value = array[front] array[front] = null front ++; (mode size) return value;