# ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.

## Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures."— Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures Other languages: C# Using an IDE: Eclipse This material will not be on the final.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 2 Recursion Recursion is a process that proceeds by breaking a problem down into one or more smaller problems with the same structure. Examples –Any problem that involves doing something with each element of a list can be thought of as processing the first element and then processing the rest of the list –Many mathematical functions are defined recursively –Data structures can be recursive Directory structures are recursive –The best sorting algorithms are recursive An recursive method is a method that contains a statement (or statements) that makes a call to itself. Basic solution structure –If problem is the base case (the simplest version of the problem), solve it –If not the base case, recur on a smaller version of the problem.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 3 Recursion The factorial of N is the product of the first N positive integers: N * (N – 1) * (N – 2 ) *... * 2 * 1 The factorial of N can be defined recursively as 1 if N = 1 factorial( N ) = N * factorial( N-1 ) N>1

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 4 Recursive Method Implementing the factorial of N recursively will result in the following method. public int factorial( int N ) { if ( N == 1 ) { return 1; } else { return N * factorial( N-1 ); } } Test to stop or continue. Recursive case: recursion continues. End case: recursion stops.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 5 Anagram List all anagrams of a given word. Word C A T C T A A T C A C T T C A T A C Anagrams

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 6 Anagram Solution The basic idea is to make recursive calls on a sub-word after every rotation. Here’s how: C C A A T T Recursion A A T T C C T T C C A A Rotate Left C A T C T A A T C A C T T C A T A C

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 7 Towers of Hanoi The goal of the Towers of Hanoi puzzle is to move N disks from peg 1 to peg 3: –You must move one disk at a time. –You must never place a larger disk on top of a smaller disk.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 8 Towers of Hanoi Solution

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 9 Recursive Sorting Algorithms There are two commonly used sorting methods that use recursion –Merge sort: sort each half of the array and merge the sorted halves together –Quicksort: use a pivot value to separate the array into two parts Both these methods are more efficient that the sorts we’ve seen before –N log N instead of N 2

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 10 Quicksort To sort an array from index low to high, we first select a pivot element p. –Move all elements less than the pivot to the first half of an array and all elements larger than the pivot to the second half. Put the pivot in the middle. –Recursively apply quicksort on the two halves.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 11 When Not to Use Recursion When recursive algorithms are designed carelessly, it can lead to very inefficient and unacceptable solutions. For example, consider the following: public int fibonacci( int N ) { if (N == 0 || N == 1) { return 1; } else { return fibonacci(N-1) + fibonacci(N-2); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 12 Excessive Repetition Recursive Fibonacci ends up repeating the same computation numerous times.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 13 Static vs. Dynamic Structures A static data structure has a fixed size Arrays are static; once you define the number of elements it can hold, the number doesn’t change –An ArrayList can change its size but it really just hides the work involved in changing the size of a static structure A dynamic data structure grows and shrinks at execution time as required by its contents A dynamic data structure is implemented using links

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 14 References as Links Recall that an object reference is a variable that stores the address of an object Object references can be used to create links between objects Suppose a Student object contains a reference to another Student object John Smith 40725 3.57 Jane Jones 58821 3.72

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 15 References as Links References can be used to create a variety of linked structures, such as a linked list: This is called a LinkedList (or stack) –LinkedList class has same methods as ArrayList studentList

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 16 Other Dynamic List Representations doubly linked list, with next and previous references A queue adds items to the rear of the list and removes them from the front list enqueue dequeue

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 17 Trees and Binary Trees A tree is a non-linear data structure that consists of a root node and potentially many levels of additional nodes that form a hierarchy –Each node can be linked to at least two other nodes A binary tree is defined recursively. Either it is empty (the base case) or it consists of a root and two subtrees, each of which is a binary tree Directories form a tree-like structure

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 18 Related Languages C is a procedural language –Not object-oriented –Efficient –Provides low-level access to hardware C++ is an object-oriented language which is backwards compatible with C

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Other Languages: C# Like Java, C# is –A modern, general-purpose object-oriented language –Based on C and C++ C# is part of the.NET family of languages supported by MicroSoft –Multiple languages which can interoperate –Languages compile to a common intermediate language –Common Language Runtime runs programs from all the.NET languages

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C# Program Structure A program consists of one or more files A file can contain one or more classes and/or namespaces –name of file is not tied to name of class At least one class must contain Main –There are several allowed signatures return type is either int or void either no parameter or String array

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. First Program namespace FirstProgram { class First { static void Main() { System.Console.WriteLine( "Welcome to C#!"); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Console I/O System.Console.WriteLine( arg) –argument can be any type –for objects, ToString is invoked System.Console.ReadLine() –returns a String System is a namespace –using System; allows you to omit the namespace when calling the method

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C# Types Value types –simple types: primitive types from Java plus unsigned types and decimal Reference types - objects

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Operators Has same operators as Java with similar precedence and associativity == compares values for strings and simple types, addresses for all other objects

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C# on onyx mono is an open-source project that provides facilities for running C# programs under Linux http://www.mono-project.com Compile a program by typing mcs First.cs Run a program by typing mono First.exe

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 26 Using an IDE IDE = Integrated Development Environment –Do all your editing, compiling and running from inside the IDE program –Debugger allows you to execute your program in steps and look at the variables to see what is happening. Eclipse is an IDE used for Java –available on onyx

Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures."

Similar presentations