Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

Similar documents
Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

Binary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

protected BinaryNode root; } 02/17/04 Lecture 11 1

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

CS350: Data Structures Tree Traversal

Data Structures in Java

CMSC 341. Binary Search Trees CMSC 341 BST

Priority Queues. 04/10/03 Lecture 22 1

CIS 121. Binary Search Trees

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

Implementations II 1

CMSC 341 Lecture 10 Binary Search Trees

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Implementations II 1

Lecture 23: Binary Search Trees

CmpSci 187: Programming with Data Structures Spring 2015

CS 151. Binary Search Trees. Wednesday, October 10, 12

Generic BST Interface

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

The Binary Search Tree ADT

CS350: Data Structures Binary Search Trees

Binary Search Trees. See Section 11.1 of the text.

CS350: Data Structures AA Trees

COMP : Trees. COMP20012 Trees 219

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

a graph is a data structure made up of nodes in graph theory the links are normally called edges

CS 350 : Data Structures Binary Search Trees

void insert( Type const & ) void push_front( Type const & )

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Examples

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Second Exam SAMPLE Computer Programming 338 Dr. St. John Lehman College City University of New York Tuesday, 5 April 2011

COMS 3137 Class Notes

IMPLEMENTING BINARY TREES

ITI Introduction to Computing II

COMP 250. Lecture 22. binary search trees. Oct. 30, 2017

CE 221 Data Structures and Algorithms

ITI Introduction to Computing II

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

Why Do We Need Trees?

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Principles of Computer Science

CS 310: Tree Rotations and AVL Trees

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

CS302 - Data Structures using C++

Recursion Solution. Counting Things. Searching an Array. Organizing Data. Backtracking. Defining Languages

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

AVL Trees. See Section 19.4of the text, p

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS 206 Introduction to Computer Science II

CS302 - Data Structures using C++

Introduction to Algorithms and Data Structures

TREES Lecture 10 CS2110 Spring2014

Lecture No.07. // print the final avaerage wait time.

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

ECE242 Data Structures and Algorithms Fall 2008

COMP 11 Class 23 Outline

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

CS302 - Data Structures using C++

24-Oct-18. Lecture No.08. Trace of insert. node 17, 9, 14, 5. p->setright( node );

TREES Lecture 12 CS2110 Spring 2018

CSE 214 Computer Science II Heaps and Priority Queues

Sample Questions for Midterm Exam 2

Priority Queues and Huffman Trees

TREES Lecture 12 CS2110 Fall 2016

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Computer Science II Fall 2009

CSE 143 Lecture 11. Decimal Numbers

CSE 373: Data Structures and Algorithms

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Chapter 13 Recursion. Chapter Objectives

ITEC2620 Introduction to Data Structures

CSE 326: Data Structures Binary Search Trees

Chapter 6 Recursion. The Concept of Recursion

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Recursive Algorithms. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

CSI33 Data Structures

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Data Structures And Algorithms

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees. Linda Shapiro Spring 2016

Priority Queues (Heaps)

CS 315 Data Structures mid-term 2

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

Trees, Part 1: Unbalanced Trees

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Transcription:

Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, Recursion public static long fib(int n) if (n <= 1) return n; else return fib(n-1) + fib(n-2); Example 2: Towers of Hanoi 02/11/03 Lecture 10 1

Figure 2.11 Figure 2.11 Recursive calls that rabbit(7) generates 02/11/03 Lecture 10 2

Figure 2.19a and b Figure 2.19a and b a) The initial state; b) move n - 1 disks from A to C 02/11/03 Lecture 10 3

Figure 2.19c and d Figure 2.19c and d c) move one disk from A to B; d) move n - 1 disks from C to B 02/11/03 Lecture 10 4

Sample output Move top disk from pole A to pole B Move top disk from pole A to pole C Move top disk from pole B to pole C Move top disk from pole A to pole B Move top disk from pole C to pole A Move top disk from pole C to pole B Move top disk from pole A to pole B 02/11/03 Lecture 10 5

SolveTowers Solution public static void solvetowers(int count, char source, char destination, char spare) if (count == 1) System.out.println("Move top disk from pole " + source + " to pole " + destination); else solvetowers(count-1, source, spare, destination); // X solvetowers(1, source, destination, spare); // Y solvetowers(count-1, spare, destination, source); // Z // end if // end solvetowers 02/11/03 Lecture 10 6

Figure 2.20 Figure 2.20 The order of recursive calls that results from solvetowers(3, A, B, C) 02/11/03 Lecture 10 7

Figure 2.21a Figure 2.21a Box trace of solvetowers(3, A, B, C ) 02/11/03 Lecture 10 8

Figure 2.21b Figure 2.21b Box trace of solvetowers(3, A, B, C ) 02/11/03 Lecture 10 9

Figure 2.21c Figure 2.21c Box trace of solvetowers(3, A, B, C ) 02/11/03 Lecture 10 10

Figure 2.21d Figure 2.21d Box trace of solvetowers(3, A, B, C ) 02/11/03 Lecture 10 11

Figure 2.21e Figure 2.21e Box trace of solvetowers(3, A, B, C ) 02/11/03 Lecture 10 12

Binary Trees class BinaryNode public BinaryNode( ) this( null, null, null ); public BinaryNode( Object theelement,binarynode lt,binarynode rt); public static int size( BinaryNode t ); // size of subtree rooted at t public static int height( BinaryNode t ); public void printpreorder( ); public void printpostorder( ); public void printinorder( ); public BinaryNode duplicate( ); // make a duplicate tree and return root public Object getelement( ); public BinaryNode getleft( ); public BinaryNode getright( ); public void setelement( Object x ); public void setleft( BinaryNode t ); public void setright( BinaryNode t ); private Object element; private BinaryNode left; private BinaryNode right; 02/11/03 Lecture 10 13

Binary Trees public class BinaryTree public BinaryTree( ); public BinaryTree( Object rootitem ); public void printpreorder( ); public void printinorder( ); public void printpostorder( ); public void makeempty( ); public boolean isempty( ); /** Forms a new tree from rootitem, t1 and t2. t1 not equal to t2. */ public void merge( Object rootitem, BinaryTree t1, BinaryTree t2 ); public int size( ); public int height( ); public BinaryNode getroot( ); private BinaryNode root; 02/11/03 Lecture 10 14

Binary Trees public class BinaryTree static public void main( String [ ] args ) BinaryTree t1 = new BinaryTree( "1" ); BinaryTree t3 = new BinaryTree( "3" ); BinaryTree t5 = new BinaryTree( "5" ); BinaryTree t7 = new BinaryTree( "7" ); BinaryTree t2 = new BinaryTree( ); BinaryTree t4 = new BinaryTree( ); BinaryTree t6 = new BinaryTree( ); t2.merge( "2", t1, t3 ); t6.merge( "6", t5, t7 ); t4.merge( "4", t2, t6 ); System.out.println( "t4 should be perfect 1-7; t2 empty" ); System.out.println( "----------------" ); System.out.println( "t4" ); t4.printinorder( ); System.out.println( "----------------" ); System.out.println( "t2" ); t2.printinorder( ); System.out.println( "----------------" ); System.out.println( "t4 size: " + t4.size( ) ); System.out.println( "t4 height: " + t4.height( ) ); 02/11/03 Lecture 10 15

Binary Trees public void printpreorder( ) System.out.println( element ); // Node if( left!= null ) left.printpreorder( ); // Left if( right!= null ) right.printpreorder( ); // Right public void printpostorder( ) if( left!= null ) left.printpostorder( ); // Left if( right!= null ) right.printpostorder( ); // Right System.out.println( element ); // Node public void printinorder( ) if( left!= null ) left.printinorder( ); // Left System.out.println( element ); // Node if( right!= null ) right.printinorder( ); // Right 02/11/03 Lecture 10 16

Binary Trees public void merge( Object rootitem, BinaryTree t1, BinaryTree t2 ) if( t1.root == t2.root && t1.root!= null ) System.err.println( "lefttree==righttree; merge aborted" ); return; root = new BinaryNode( rootitem, t1.root, t2.root ); if( this!= t1 ) t1.root = null; if( this!= t2 ) t2.root = null; public BinaryNode duplicate( ) BinaryNode root = new BinaryNode( element, null, null ); if( left!= null ) root.left = left.duplicate( ); if( right!= null ) root.right = right.duplicate( ); return root; // Return resulting tree 02/11/03 Lecture 10 17

Binary Search Trees // BinarySearchTree class // // void insert( x ) --> Insert x // void remove( x ) --> Remove x // void removemin( ) --> Remove minimum item // Comparable find( x ) --> Return item that matches x // Comparable findmin( ) --> Return smallest item // Comparable findmax( ) --> Return largest item // boolean isempty( ) --> Return true if empty; else false // void makeempty( ) --> Remove all items public class BinarySearchTree private Comparable elementat( BinaryNode t ) return t == null? null : t.element; protected BinaryNode insert( Comparable x, BinaryNode t ) protected BinaryNode remove( Comparable x, BinaryNode t ) protected BinaryNode removemin( BinaryNode t ) protected BinaryNode findmin( BinaryNode t ) private BinaryNode findmax( BinaryNode t ) private BinaryNode find( Comparable x, BinaryNode t ) protected BinaryNode root; 02/11/03 Lecture 10 18

Binary Search Trees public static void main( String [ ] args ) BinarySearchTree t = new BinarySearchTree( ); final int NUMS = 4000; final int GAP = 37; System.out.println( "Checking... (no more output means success)" ); for( int i = GAP; i!= 0; i = ( i + GAP ) % NUMS ) t.insert( new Integer( i ) ); for( int i = 1; i < NUMS; i+= 2 ) t.remove( new Integer( i ) ); if( ((Integer)(t.findMin( ))).intvalue( )!= 2 ((Integer)(t.findMax( ))).intvalue( )!= NUMS - 2 ) System.out.println( "FindMin or FindMax error!" ); for( int i = 2; i < NUMS; i+=2 ) if( ((Integer)(t.find( new Integer( i ) ))).intvalue( )!= i ) System.out.println( "Find error1!" ); for( int i = 1; i < NUMS; i+=2 ) if( t.find( new Integer( i ) )!= null ) System.out.println( "Find error2!" ); 02/11/03 Lecture 10 19

Binary Search Trees protected BinaryNode insert( Comparable x, BinaryNode t ) if( t == null ) t = new BinaryNode( x ); else if( x.compareto( t.element ) < 0 ) t.left = insert( x, t.left ); else if( x.compareto( t.element ) > 0 ) t.right = insert( x, t.right ); else throw new DuplicateItemException( x.tostring( ) ); // Duplicate return t; protected BinaryNode remove( Comparable x, BinaryNode t ) if( t == null ) throw new ItemNotFoundException( x.tostring( ) ); if( x.compareto( t.element ) < 0 ) t.left = remove( x, t.left ); else if( x.compareto( t.element ) > 0 ) t.right = remove( x, t.right ); else if( t.left!= null && t.right!= null ) t.element = findmin( t.right ).element; t.right = removemin( t.right ); else t = ( t.left!= null )? t.left : t.right; return t; 02/11/03 Lecture 10 20

Binary Search Trees protected BinaryNode removemin( BinaryNode t ) if( t == null ) throw new ItemNotFoundException( ); else if( t.left!= null ) t.left = removemin( t.left ); return t; else return t.right; protected BinaryNode findmin( BinaryNode t ) if( t!= null ) while( t.left!= null ) t = t.left; return t; private BinaryNode find( Comparable x, BinaryNode t ) while( t!= null ) if( x.compareto( t.element ) < 0 ) t = t.left; else if( x.compareto( t.element ) > 0 ) t = t.right; else return t; // Match return null; // Not found 02/11/03 Lecture 10 21