Data Structure - Recursion and tower of hanoi - Notes, Study notes of Data Structures and Algorithms

In this document topics covered which are Data and File Structures, Array, 1D Array Representation In Java, C, and C , 2D Arrays, Rows Of A 2D Array, Columns Of A 2D Array.

Typology: Study notes

2010/2011

Uploaded on 09/01/2011

visir66
visir66 🇮🇳

4.4

(74)

97 documents

1 / 46

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data and File Structures
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

Download Data Structure - Recursion and tower of hanoi - Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data and File Structures

Array

  • (^) Homogeneous, indexed collection of values
  • (^) Access to individual elements through subscript
  • (^) PL choices
    • (^) Subscript syntax
    • (^) Subscript type, element type
    • (^) When to set bounds, compile time or run time?
    • (^) How to initialize?
    • (^) What built-in operations are allowed?

2D Arrays

The elements of a 2-dimensional array a declared as: int [][]a = new int[3][4]; may be shown as a table a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

Rows Of A 2D Array

a[0][0] a[0][1] a[0][2] a[0][3] row 0 a[1][0] a[1][1] a[1][2] a[1][3] row 1 a[2][0] a[2][1] a[2][2] a[2][3] row 2

Pointers and Arrays

Arrays vs. Pointers

Pointer Arithmetic

  • (^) To conduct arithmetical operations on pointers is a little different than to conduct them on regular integer data types.
  • (^) Only addition and subtraction operations are allowed to be conducted with them, the others make no sense in the world of pointers.
  • (^) Both addition and subtraction have a different behavior with pointers according to the size of the data type to which they point.
  • (^) row-major access formula using pointer arithmetic x[i][j] == *(x + i * n + j), where n is the row size of x

Simple recursion

So a recursive function

  • (^) calls itself
  • (^) calls to lower values
  • (^) must terminate in definite time.

Recursive execution trace

Types of recursion

1. Linear Recursion Recursion where only one call is made to the function from within the function 2. Binary Recursion A recursive function which calls itself twice during the course of its execution**.

  1. Tail Recursion** A recursive procedure where the recursive call is the last action to be taken by the function 4. Mutual Recursion A recursive function doesn't necessarily need to call itself. Some recursive functions work in pairs or even larger groups 5. Nested Recursion In nested recursion, one of the arguments to the recursive function is the recursive function itself! 6. Exponential Recursion Recursion where more than one call is made to the function from within itself.

Calculating Fibonacci Numbers

  • (^) Problem: How can we compute the i-th Fibonacci number fi?
  • (^) We will present 3 sample solutions
    • (^) Recursive algorithm ( fibrec )
    • (^) Iterative algorithm ( fibiter )
  • (^) A recursive algorithm fibrec
  • (^) What does “recursive algorithm” mean?
    • (^) Again the recursive definition:
  • (^) For i=0, 1, ... compute