Week 3 + 4 + 5 Flashcards
Define stack.
Container with insertion and removal based on LIFO (Last-In-First-Out).
Define push, pop, top with stack.
push, inserts onto top of stack
pop, removes off of top of stack
top is last item pushed.
How is stack implemented using array?
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 is stack implemented using linked list?
Top of stack is set to first node. pop is removeFirst. push is addFirst. top points to null for empty stack.
Explain String Builder syntax. Why use string builder?
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.
Define queue.
Container with insertion at back and removal from front. Based on FIFO (First-In-First-Out) data structure.
Define enqueue, dequeue and front for queue.
Enqueue inserts at rear of queue
Dequeue removes the element at the front of the queue.
Front returns the front of the queue.
How is queue implemented using array.
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 is queue implemented using linked list?
Doubly Linked List used. Enqueue, performs addLast, Dequeue performs removeFirst.
Define deque.
A queue where we can insert and delete from both the front and the end.
Define a tree.
A tree consists of nodes, where each node (except the first) has a parent and all nodes have zero or more children.
Define root.
First node which has no parent.
Define internal node.
A node with at least one child.
Define external node.
A node without children.
Define subtree.
Tree within a tree consisting of a node and its descendants.
Define depth of node.
Number of ancestors of p other than p itself.
Define height of tree.
Maximum depth of any node. The height of its root node.
What is the ordering in a tree?
Siblings are arranged left to right, from lowest to greatest.
Define binary tree.
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.
Define height of a node.
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
Define proper or full binary tree.
A binary tree where each node has either zero or two children.
T or F. In a binary tree at level/depth d there is at most 2^d nodes.
True
In a non-empty binary tree what is the minimum and maximum number of nodes n.
h + 1 <= n <= 2^(h + 1) - 1
In a non-empty binary tree what is the minimum and maximum number of external nodes.
1 <= ne <= 2^h