CS Problem Solving and Object Oriented Programming Spring 2019

Slides:



Advertisements
Similar presentations
Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Lecture 16: Tree Traversal.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
CS 206 Introduction to Computer Science II 12 / 10 / 2008 Instructor: Michael Eckmann.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
F453 Computing Searches. Binary Trees Not this kind of tree!
Lecture 17 Non-Linear data structures Richard Gesick.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Emma Price 1.  To be able to:  Explain what a binary tree is.  To traverse a binary tree using the three different methods. 2.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
CompSci 102 Discrete Math for Computer Science April 17, 2012 Prof. Rodger.
CSED101 INTRODUCTION TO COMPUTING TREE 2 Hwanjo Yu.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Computer Science 101 A Survey of Computer Science QuickSort.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself. 
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Chapter 19: Recursion.
Planning & System installation
Chapter 15 Recursion.
Planning & System installation
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Recursive Objects (Part 4)
Chapter 15 Recursion.
Planning & System installation
Binary Search Tree (BST)
Trees 8/7/2018 2:27 PM Binary Trees © 2010 Goodrich, Tamassia Trees.
Divide-and-Conquer The most-well known algorithm design strategy:
Programming in Java: lecture 10
Algorithms and Data Structures Recursion and Binary Tree
Section 8.1 Trees.
Data Structures & Algorithm Design
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
QuickSort QuickSort is often called Partition Sort.
Introduction to Computer Science - Alice
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4: Divide and Conquer
Data Structures & Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
Section 9.3 by Andrew Watkins
Design and Analysis of Algorithms
Announcements Last … First … … quiz section … office hour
Binary Search Trees Chapter 7 Objectives
Algorithm Efficiency and Sorting
Chapter 19: Recursion.
Trees.
Chapter 20: Binary Trees.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Non-Linear data structures
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

CS 18000 Problem Solving and Object Oriented Programming Spring 2019 Section LE2 Week 14: Lecture 27, April 22. 2019 Slides updated: 9:21am, April 22, 2019 Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA https://www.cs.purdue.edu/homes/apm/courses/CS180_Java/CS180Spring2019/

©Aditya Mathur. CS 180. Fall 2019.Week 15 This week Recursion Some mathematical recursive functions Tower of Hanoi Binary trees Quicksort 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Recursion Using a function or a method to evaluate itself! Some problems have a easier to find recursive than an iterative solution. In some cases a recursive solution may turn out to be more expensive than an iterative solution. Thus, use recursion when the problem demands it! 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Sample recursive functions Fibonnacci(n)= 1; if n=0 or 1; =Fibonnaci(n-1)+Fibonnaci(n-2); if n>1; Fibonnaci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34…. fact(n)= 1; if n=0 =n*fact(n-1) if n>0 GCD(x,y)= x; if x=y =GCD(x-y,y); x>y =GCD(x,y-x); x<y 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Tower of Hanoi A C B N discs in tower A Smaller disc must always be above a larger disc; arranged in order Move all discs from A to C via B. Only one disc can be moved at a time and must be placed on a tower and not anywhere else. 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Tower of Hanoi: Solution 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Live demo 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Binary tree T3 T1 T2 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Binary tree: Elements Root Nodes Left link Right link Leaf nodes 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Binary tree: with data Data: 12, 8, -2, 11, 17, 99, 3 12 8 17 11 99 -2 3 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Binary tree: traversal Inorder: Left, Root, Right 12 8 -2 11 17 99 3 Preorder: Root, Left, Right Postorder: Left, Right, Root Level order: Level by level 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Quiz: 04/22/2019 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

Quicksort: The algorithm Problem: Sort an array: data=[d0 d1 d2 d3 dN ] Quicksort(data, 0, N) Select an element in the array; this is element is known as the pivot. Partition the array so that the left partition (Pleft) has elements less than pivot and the right partition (Pright) has elements equal to or greater than the pivot. Apply Quicksort( ) to Pleft and Pright

©Aditya Mathur. CS 180. Fall 2019.Week 15 Quicksort: Illustration of partitioning j=0 Pivot i=-1 i=-1; j=0 Compare 15 with the pivot (8) [15 3 19 2 8] 15>8; array not changed J=1; i=-1; compare 3 with 8. [15 3 19 2 8] 3<8; bring 3 to position 0 J=2; i=0; compare 19 with 8 [3 15 19 2 8] 19>8; no change J=3; i=0; compare 2 with 8 [3 15 19 2 8] 2<8; move 2 to i+1 J=4; i=1; [3 15 19 2 8] Exchange pivot with 19 J=4; i=2; [3 2 19 15 8] Exchange pivot with 19 J=4; i=2; [3 2 8 15 19] 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15

©Aditya Mathur. CS 180. Fall 2019.Week 15 Quicksort: Apply quicksort on two subarrays [3 2 8 15 19] [3 2 ] Quicksort [15 19] Quicksort 04/22/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 15