CS 310 RU

Ace your homework & exams now with Quizwiz!

If we add 'a', 'b', 'c', 'd' into a queue and dequeue(), what will be returned?

'a'

If we add 'a', 'b', 'c', 'd' onto a stack and pop(), what will be returned?

'd'

Explain the difference between a dynamic array list and a linked list.

A dynamic array list is simply a collection of values with their own index. A linked list is a collection of nodes pointing to one another. -insertion/removal is easier on a linked list (just adding a node) while a dynamic array list may have to be resized -linked list has to store more memory as it has the values and the addresses of the next node -linked lists cannot have algorithms used on it as indexes cannot be called

Compare and contrast singly and doubly linked lists, including the pros and cons of using them.

A singly linked list means each node points to a single node ahead of it. A doubly linked list has each node pointing to the node in front and behind it. -Singly linked list is much easier to remove a node as you can readjust a single pointer and the Java Sweeper will eliminate the unreachable data -Doubly linked lists are easily traversal as you can go forwards and backwards in the list while Singly linked you can only move forwards -Double linked lists have to store more memory as each node has two addresses it is pointing to

Explain the two ways to store a graph we covered in class. When would you want to use which?

Adjacency list and matrix

Explain how to store a stack in an array with the optimal big-O.

Pop each part of the stack into the array starting from the end. The big-O for this would be O(N) as it is dependent on how many objects are in the stack.

Describe how you make a class "iterable" in Java. What interfaces do you need? What methods are required by those interfaces? Are there any additional optional methods?

Simply need a means to go through them one by one. For loops are an easy means of doing so, but things like iterators can be implemented to achieve the same task.

What's the difference between "best case", "worst case", and "average case" Big-O?

The "best case" would be the case where the time and space complexities are at their most minimal during runtime. The "worse case" would be the situation where the time and space complexities are at their maximum during runtime. The "average case" is methods in between these two. Example: Having to find an object in an array. Best - Calling the index of that object. Average - Looping through to find that object. Worst- Using a complex algorithm to find that object.

Explain the formal definition of Big-O: T(n) is O(F(n)) if there are positive constants c and n0 such that, when n >= n0, T(n) <= c F(n).

The Big-O definition states that there is a point N0 such that for all values of N that are past this point, T(N) is bounded by some multiple F(N). Thus, if the running time T(N) of an algorithm is O(N^2), then, ignoring constants, we are guaranteeing that at some point we can bound the running time by a quadratic function.

If I stated that the big-O of a method was O(1) and that it also contained a loop, describe that loop.

The loop simply does not change with varied input. It will remain the same no matter what. An example of this would be predefined starting and endpoints that have no connection with input.

Explain time/space complexity using a real life example (like the time complexity examples we did in class).

Time example: Having multiple people complete a task rather than a single person. Space example: Having to build something and only grabbing the necessary tools rather than the entire toolbox so you have enough space at your workplace.

What are the two types of complexity we discussed in class for assessing algorithms? Give an example of trading off one for the other using a data structure discussed in CS310.

Time-complexity and Space-complexity. A simple tradeoff is making an algorithm rather than having a simple loop to find a given object. While more space is taken up to create this algorithm, the time it takes to complete this task is significantly shortened.

Explain how a circular queue works for storing a queue in an array.

[null, null, 3, 4] Given that the array has no more space at the end to add more values, values are added to the beginning, thus the queue goes in somewhat of a circle to the beginning of the array

Convert the following functions to Big-O: a) 10n + O(lg(n)) b) 0.25n! + 2n c) 1+2+3 d) 5 log(n) e) log(n^2) + 100

a)O(nlog(n)) b)O(n!) c)O(1) d)O(log(n)) e)O(log(n^2))

Explain the difference between a "node" class and a "linked list" class. Include in your description the terms: node, next, value, head, and tail.

linked list - is a collection of nodes which point to other nodes node - is an object which stores a value, an address to another node in front (singly) and back (doubly) next - the node that the current node points to value - the value stored within the next node head - a pointer which p

Write code to implement a basic iterator for an [array | linked list] and show how the iterator would be used.

next (to move to next object) and hasNext (to check if next object exists) method

Explain the difference between a queue and a stack.

queue- first thing in is first thing out stack - last thing in is first thing out

Explain how to implement a [queue | stack] with a [singly | doubly] linked list with the optimal BigO? Which end(s) should you insert/remove from for the optimal Big-O?

queue/singly -> add to tail node, remove from head node queue/doubly -> add to tail node, remove from head node stack/singly ->add/remove from tail node stack/doubly ->add/remove from tail node

Explain how to store a binary tree using an [array | linked structure]. For an array, explain how to find the child/parent of a given node based only on the index of that element. For a linked structure, write the code for a node. Can you generalize the idea to k-ary trees?

to store it simply add it one by one from the root on down in a level-order traversal. Use the equation (2i+1 / 2i +2) to find the children of a binary search tree. NEEDS MOR SHIT

Explain the following terms: trees, root, descendants, ancestors, leaves, siblings, parent, child, depth, node height, tree height, full tree, (nearly) complete tree, perfect tree, balanced tree.

trees -> a collection of nodes pointing to other nodes root -> the starting node found at the top of the tree descendants ->a node with parents (basically all but the root) ancestors -> a node with children ( basically all but the leaves) leaves -> nodes without children found at the bottom of the tree siblings -> nodes with the same parents parent -> the node pointing to the node you refer to child ->the nodes to which your node is pointing to depth-> the number of edges from node to root node height -> the number of edges from node to deepest leaf tree height ->max depth of any node in the tree full tree-> all but leaves have the max number of children nearly complete tree-> the last level not completely filled perfect tree-> all nodes have max children/ leaves have the same depth balanced tree-> height of left and right subtrees differ by less than 1


Related study sets

UNIT 2 How did Hitler change Germany from a Democracy to a Dictatorship?

View Set

Lecture 14: Polynomial Functions of Higher Degree & Long Division of Polynomials

View Set

Chapter 13 Abnormal Uterine Bleeding (DMS120)

View Set