cse205 review session samuel & jessa. recursion what is recursion? recursion is a tool a...
TRANSCRIPT
CSE205 Review SessionSAMUEL & JESSA
Recursion
WHAT IS RECURSION? Recursion is a tool a programmer can use to
invoke a function call on itself.
Recursive methods contain a base case, and a function call, calling itself.
Common recursion problems involve the Fibonacci sequence, or solving factorials.
Transform iterative methods to become recursive methods.
BASE CASE
BASE CASE
BASE CASE
DON’T FORGET YOUR BASE CASE
*Factorial: Iterative vs Recursion
Recursive method walk through
Given n = 5, when we call this function recursively we will get a value of 5*4*3*2*1*1;
Note that there are 2 1’s. This comes from the base
case
*Merge Sort 3 steps to merge sort
Cut the array in half
Recursively Sort each half
Merge the two halves together
Benefits:Dramatically faster than other sorting methods like insertion sort, quicksort
Quick Sort Quick Sort uses the
divide and conquer strategy.
Step 1: Partition the set/range
Step 2: Sort each following partition
To partition, we can select a pivot point. Here we can select the first element given an array
*Linked Lists A linked list is a data structure which
consists of a group of nodes, when grouped together represent a sequence.
Each node is composed of some form of data, and a reference(link/pointer).
Benefits of Linked Lists:
Linked lists can represent a dynamic set. Linked lists are not static, therefore can be changed in size.
This helps with insertion and deletion within a sequence.
Insertion in Linked Lists
Deletion in Linked List
To remove something from a linked list, we take the previous node, and set its “next”, to the node we are going to deletes “next”.
This removes the link to the unwanted node.
Java handles the rest
*Stacks/Queues
Stacks:
Last in first out (LIFO)
*Push (Adds to top of stack)
*Pop (Removes top of stack)
*Peek (Shows top of stack)
Queues:
First in first out (FIFO)
*Enqueue (Adds to queue)
*Dequeue (Removes from queue)
Binary Search Tree Traversal
Confused on how to traverse a tree through Pre order, in order and post order traversal?
Use this trick: For pre order: Place a dot on the
left of each node in a binary tree For in order: Place a don’t on the
bottom of each node in a binary tree
For post order: Place a don’t on the right of each node in a binary tree
Tree traversal continuedStart from the top of the node, and traverse through the binary tree going counter clockwise.
Whenever you encounter the dot you just placed, write down that node to your list accordingly.
Pre-Order: F, B, A, D, C, E, G, I, H
In-order: A, B, C, D, E, F, G, H, I
Post-order: A, C, E, D, B, H, I, G, F
Heaps
Heaps are a binary tree but with two distinct properties
1. It is nearly complete, with the exception of the lowest level, filling in left from right
2. Heaps fulfill the heap property, each parent node is smaller than both of its child nodes.
Questions?