final csc

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Collections and Basic Data Structures

- Know the different data structures we went over and how they work (their operators) --> ArrayList --> LinkedList --> Stack --> (Priority) Queue --> Map --> Set - Understand what makes them unique and the considerations for choosing each --> which are fast for insertion? (LinkedList) --> which maintain a FIFO processing order? --> which put elements in order? (Tree)

Recursion

- Know what recursion is - Understand the methodology and thinking behind creating a recursive algorithm - Evaluate basic recursive code - Know considerations for when to use recursion and when to use another approach (iterative) - Backtracking

Backtracking

- Problem solving technique that builds up partial solutions that get increasingly closer to the goal - if partial solutions cant be completed, its abandoned and program returns to examining the other candidates - all paths towards solutions are explored and if one path is a dead end, u backtrack and try another choice - problems requiring multiple paths like crossword puzzles, mazes, or solutions to other systems

Sorting and Searching

- Sorting algorithms --> Selection Sort --> Insertion Sort --> Bubble Sort (SC) --> Merge Sort --> Quicksort - Searching Algorithms --> Linear Search --> Binary Search - Be able to trace the behavior of these algorithms and understand how they work - Understand considerations when choosing each --> basic time complexity --> is additional temporary storage required --> best case scenarios

Set options

- TreeSet if you want to visit the sets elements in sorted order or to control it - otherwise, HashSet, which is more efficient if the hash function is well chosen

Set interface

- UNORDERED collection of UNIQUE elements ( no duplicates ) - arranges its elements so that finding, adding, and removing elements is more efficient --> HashSet --> TreeSet

Generics

- What a Generic class usage looks like or what ArrayList<String> means may be asked

Trees

- What is a tree? - The distinctions for binary trees and binary search trees - The operations of a binary search tree --> Insertion --> Deletion --> Traversal - pre, post, in order --> Search - depth vs breadth

Queue (FIFO)

- add items to one end (tail) and remove them from the other end (head) - like a queue of people, FIRST IN, FIRST OUT

Sets in hash table

- all have implemented hashCode and equals method - set elements are grouped into smaller collections of elements that share the same characteristics - ex: books are sorted by color

O(n^2)

- bubble, insertion, selection sort - Worst case: Quick sort

Consider a standard doubly linked list where each node has a data portion and two node references, next and previous. How many references need to be updated when we remove a node from the beginning of a list with multiple nodes? Consider the first reference and neighboring node(s). a) 1 b) 2 c) 3 d) 4

2

What would the content of the below array be after 1 full pass of an ascending Bubble Sort? 51, 54, 66, 81, 42, 16, 79

51, 54, 66, 42, 16, 79, 81

Bubble Sort O(n^2)

A pair-wise comparison of adjacent elements. You swap elements until the array is sorted - Impractical - can become really slow when the array size is large - short circuit

Which of the following statements about trees is not correct? a) The height of a tree is based on the longest path from the root to a leaf. b) Many tree properties are computed with recursive methods. c) A simple implementation of a Tree class uses an instance variable for the root node and a nested Node class, which stores the node's data and its children. d) A subtree rooted at node n is the tree formed by taking n as the root node and including all of its ancestors.

A subtree rooted at node n is the tree formed by taking n as the root node and including all of its ancestors

When a recursive method is called correctly, and it does not perform recursion, what must be true? a) Both a terminating condition and a recursive case condition were true. b) A terminating condition was true. c) All recursive case conditions were true. d) One recursive case condition was true.

A terminating condition was true

ArrayList and LinkedList

ArrayList is better at random access and has a fixed size, while LinkedLists are easier to add, remove, and uses more memory, cant access stuff well

Depth First Search

- Iterative traversal that stops when a node has been found - It visits all descendants before visiting siblings - uses a stack to track nodes that it still needs to visit

Doubly Linked lists

- use this for some applications because it is advantageous to move through a list, backwards and forwards - with the linked-list, we can only traverse the list forwards

Binary Search trees

- All nodes in binary search tree fulfill --> descendants to the left have smaller data values than node data value --> descendants to the right are larger. an use BST for this

O(n)

- Best case: Insertion and Bubble sort SC - Average: Linear search and linear search SC

Best-case complexity

- Bubble sort will still run at O(n^2) at best case - Bubble sort SC will run at O(n) at best case - Insertion sort will also run at O(n) in best case

Insertion

- If you encounter a non-null reference, look at its data value - If the data value of that node is larger than the value you want to insert, continue the process with the left child - If the node data value is smaller than the one you want to insert, continue the process with the right child - If the node data value is the same as what you want to insert, you are done. a set does not store duplicate values - When you encounter a null node reference, replace it with the new node

Balanced trees

- In balanced binary trees, each subtree has approx. same number of nodes

Postfix expressions are evaluated from left to right

- 138*+ = 1+3*8 - 85/ = 8/5 - 545*+5/ = (5+4*5)/5 - 45+72-* = (4+5)*(7-2) - 42+351-*+ = (4+2)+(3*(5-1))

Know considerations for when to use recursion and when to use another approach (iterative)

- In most cases (not fib), iterative and recursive approaches have comparable efficiency - recursive runs much slower than iterative (iterative isPalindrome performs slightly better than recursive solution) - recursive is easier to understand and implement correctly

Breadth First Search

- It first visits all nodes on the same level before the children - uses a queue

Priority queue

- does not maintain FIFO - retrieved according to priority - ex: airport line

Binary trees

- each node has a left/right child node reference, max of 2 child nodes - decision tree, if yes we do something, if no do another thing - tree nodes have references to a right or left child, either can be null (leaf)

Sets in a tree set

- elements are sorted in order and in nodes, arranged in a tree shape, not linear sequence - ex: books are kept in order, sorted by height

Collections

- groups together elements and allows them to be retrieved later - a hierarchy of interface types and classes for collecting objects - starts from the collection interface at the root

Maps

- keeps associates between key and value objects or things were trying to retrieve. - every key in the map has an associated value - ex: barcodes are the keys related to the values or the books they associate with

Stack (LIFO)

- remembers the order of elements - only add and remove at the top - LAST IN, FIRST OUT (LIFO) - push: add and element to the top - pop: remove the top element - peek: examine the top element

What is Recursion?

- the same computation occurring repeatedly - implemented by having a method call itself - solves a problem by using the solution of the same problem with simpler values

BST removal

1. The node has no children = set the link in the parent to null 2. The node has 1 child = modify the parent link to the node to point to the child node 3. The node has 2 children = replace it with the smallest node of the right subtree - O(logn) and worst case is O(n)

O(1)

Best case: Binary and Linear search

Merge Sort O(nlogn)

Cuts the array in half, sorts, and then merges it together - much faster than bubble, selection, insertion - Divide and Conquer --> divide problem into smaller parts --> combine the solutions to get overall solution

Quick Sort O(nlogn)

Sorts by partitioning elements into 2 groups, holding smaller and larger elements; then sort each group - also Divide and Conquer, but no temporary arrays needed

We need to traverse a binary search tree using preorder traversal. Arrange the following actions in the correct order to accomplish this. I. Print the left subtree recursively II. Print the root III. Print the right subtree recursively

II, I, III

What is a tree?

In Java, a tree is a data structure that represents a hierarchical organization of elements. It is a widely used abstract data type and can be implemented in various ways. In general, a tree consists of nodes that are connected by edges.

Binary Search O(logn) worst case/average O(1) best case

Locates a value in a sorted array - Determined whether the value occurs in the first or second half, repeats the search in one of the halves

O(nlogn)

Merge sort and quick sort

Linear Search O(n) worst case/average O(1) best case

Searches for a specific value in an array, iterate through each element of the array and keep track of when the value is encountered - start at beginning, go until found.

We might use an ArrayList over a simple LinkedList when we expect that we will be doing many: Sorting operations Inserting operations Searching operations Deletion operations

Searching operations

Insertion Sort O(n^2)

Sorts an array by repeatedly finding the right position to place the value in sorted region. - start at second element, compare each item w item to the left, if smaller, move to its correct location - much faster in certain cases

Selection Sort O(n^2)

Sorts an array by repeatedly finding the smallest element of the unsorted tail region and moving it to the front - bad choice for larger arrays - can not be short circuit

PriorityQueue Tasks = new PriorityQueue<>(); Tasks.add(new Tasks("Eat", 7)); Tasks.add(new Task("Study", 1)); Tasks.add(new Task("Assignments", 2)); System.out.print(Tasks.peek().toString()); Tasks.add(new Task("Sleep", 9000)); } What is the priority queue of tasks after the above code executes?

Study, Assignments, Eat, Sleep

Assume we are using quicksort to sort an array in ascending order. What can we conclude about the elements to the left of the currently placed pivot element? a) They are all greater than or equal to the pivot element. b) They are all less than or equal to the pivot element. c) They are all sorted. d) None can equal the pivot element.

They are all greater than or equal to the pivot element

O(logn)

Worst case: Binary Search

postorder traversal

left subtree, right subtree, root - use to remove all directories from a directory tree

inorder traversal

left subtree, root, right subtree - only binary

Consider the getArea method from the textbook shown below. public int getArea(){ if (width <= 0) { return 0; } // line #1 else if (width == 1) { return 1; } // line #2 else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } } Where is/are the recursive call(s)?

line #4

Complete the following code snippet, which is a recursive method that will find the sum of all elements in an array from the beginning of the array to the index: // return the sum of all elements in arr[] public static double findSum(double arr[], int index){ if (index == 0){ _____________________ } else{ return (arr[index] + findSum(arr, index - 1)); } } Assume that this method would be called using an existing array named myArray as follows: findSum(myArray, myArray.length - 1); a) return arr[index + 1]; b) return arr[1]; c) return arr[index - 1]; d) return arr[index];

return arr[index];

Given the LinkedListStack class implementation partially shown below, select the statement(s) to complete the peek method. public class LinkedListStack{ private Node first; public LinkedListStack() { first = null; } public Object peek(){ if (empty()){ throw new NoSuchElementException(); } _____________________________ } ... }

return first.data;

preorder traversal

root, left subtree, right subtree - use to copy a directory tree


Ensembles d'études connexes

Intro to Physical Anthropology Final Study Set

View Set

Mastering Biology: The Chemical Basis of Life

View Set

NCLEX RN Examination Maternity: Antepartum

View Set