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