cse205 review session samuel & jessa. recursion what is recursion? recursion is a tool a...

17
CSE205 Review Session SAMUEL & JESSA

Upload: beverly-phelps

Post on 18-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

CSE205 Review SessionSAMUEL & JESSA

Page 2: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

Recursion

Page 3: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

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

Page 4: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

*Factorial: Iterative vs Recursion

Page 5: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

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

Page 6: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

*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

Page 7: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

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

Page 8: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

*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.

Page 9: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

Insertion in Linked Lists

Page 10: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

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

Page 11: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

*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)

Page 12: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

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

Page 13: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

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

Page 14: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

In-order: A, B, C, D, E, F, G, H, I

Page 15: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

Post-order: A, C, E, D, B, H, I, G, F

Page 16: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

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.

Page 17: CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself

Questions?