Stack and Recursion
Stack
LIFO (last in first out) Structures
Possible operation
PUSH and POP operations
Stack
grows
upwards
6
5
4
3
2
1
6
5
4
3
2
1
Example 1
1 main()cfw_
2
int a = 3;
3
int b = timesFive(a);
4
printf(b =,b);
5
6 int timesFive(int a)
Towers of Hanoi
The Towers of Hanoi is a puzzle made up of three
vertical pegs and several disks that slide on the
pegs
The disks are of varying size, initially placed on
one peg with the largest disk on the bottom with
increasingly smaller ones on top
Stack and Recursion
Stack
LIFO (last in first out) Structures
Possible operation
PUSH and POP operations
Stack
grows
upwards
6
5
4
3
2
1
6
5
4
3
2
1
Example 1
1 main()cfw_
2
int a = 3;
3
int b = timesFive(a);
4
printf(b =,b);
5
6 int timesFive(int a)
Recursion and Iteration
Two ways to solve particular problem
Iteration
Recursion
Iteration:
uses looping to repeat a set of statements
Example: Iterative method
Consider the problem of computing the sum of all
the numbers between 1 and any positive i
Binary search Trees
EXPRESSION TREE
Binary Search Trees
A Binary Search Tree (BST) is a binary tree
with the following properties:
The
key of a node is always greater than the
keys of the nodes in its left subtree
The
key of a node is always smaller tha
Outline
Recursive Thinking
Recursion Vs Iteration
Types of Recursion
Recursive Programming And Analysis
Recursive data
Recursion or Iteration?
Two ways to solve particular problem
Iteration
Recursion
Iterative control structures: uses looping to repea
Binary Trees
Properties of Binary trees
Let
n - number of nodes
ne number of external nodes or leaves
ni number of internal nodes
h height of T
h
+ 1 <= n <= 2h+1 -1
1 <= ne <= 2h
h
<= ni <= 2h 1
log(n+1)
1 <= h <= n -1
Binary Trees
Nodes in trees can
Binary Trees
Data
Linear data structures
structure = Organized data + Allowed
operations
Here are some of the data structures we have
studied so far:
Arrays
Singly-linked
lists and doubly-linked lists
Stacks and queues
These all have the property th
Towers of Hanoi
The Towers of Hanoi is a puzzle made up of three
vertical pegs and several disks that slide on the
pegs
The disks are of varying size, initially placed on
one peg with the largest disk on the bottom with
increasingly smaller ones on top
Computer Programming-II
TA C252
Design and Analysis of Algorithms
S.K. Sahay
BITS-Pilani, Goa Campus, Goa
Sept 23 - Oct. 18, 2008
Design and Analysis of Algorithms
TA C252
CP II
Some problem solving aspects.
Efciency, Complexity and Notations.
Implemen
Recursion and Iteration
Two ways to solve particular problem
Iteration
Recursion
Iteration:
uses looping to repeat a set of statements
Example: Iterative method
Consider the problem of computing the sum of all
the numbers between 1 and any positive i
TA GC 252
Computer Programming II
Linked Lists
MVB
As an abstract data type,
a list is a finite sequence (possibly empty) of elements with
basic operations that vary from one application to another
Basic operations commonly include:
Construction: Allocate
Push Operation ( Dynamic
Memory Allocation )
info
List
4
3
2
next
List
5
Before
4
3
1
5
2
After
1
Pop Operation ( Dynamic
Memory Allocation )
info
List
4
3
next
2
1
Before
info
List
3
2
next
1
After
nil
nil
Pushing an item onto a Stack
Obtain the memory s
TA GC 252
Computer Programming II
More Linked Lists
MVB
Simple Linked Lists
Linked lists like first
9
17
22
26
34
are characterized by:
(1) There is a pointer to the first node in the list.
(2) Each node contains a pointer to the next node in the list.
(3
TA GC252
Computer Programming-II
Storage Issues
MVB
1
Introduction
Arrays are usually created at compile time by
specifying size in the source code and has a fixed
size and can not be modified at run time
The process of allocating memory at compile
time
Example
Example
Illegal
Find out.
Find out.
Find out.
Find out.
Find out the errors
Find out the output
Find out
Error: Illegal
structure
operation
Comparing Structure Variables
Error: Illegal
structure
operation
Pointers to Structure
-> arrow operator or
Queues.
Introduction.
Queues: review.
Queues are linear data structures.
Insertions are done at the end (rear or tail) of a
queue and deletions are done at the head (front) of
the queue.
They follow first in first out (FIFO) order.
Queues are dynamic
Computer Programming 2
TA GC 252
FILE MANAGEMENT IN C
main( )
cfw_
int a=5,b=5,sum;
sum=a+b;
Is it possible to retain the value of sum after program
termination ?
How to input large amount of data to the program?
File is a place on the disk where grou
Functions
Introduction
A function is a self-contained block of statements
that perform a coherent task of some kind.
Classifications:
Library functions
User defined functions
main() is an example of user defined function
Advantages
1. It facilitates
Arrays.
One Dimensional Character
Arrays
Character constants
a
String constants: A string constant is a one
dimensional set of characters terminated by a
null character (\0)
abcd
Character Array: used to store a string
Declaring String Variables
char s
STRUCTURES
Arrays
Used to represent collection of related data items.
All the data items have the same data type.
Ex :Test scores of a student.
Temperature recorded in a week etc.
How to represent
Date day, month and year.
Book author, title, price a
Pointers
Introduction
int x = 30;
Reserve space in memory to hold the integer
value
Associate the name x with this memory location
Store the value 30 at this location
x
30
6500
Location Name
Value at Location
Location Address
Introduction How to
int x
Arrays
Introduction
Definition : An array is a fixed-size
sequenced collection of elements of the
same fundamental data type
Array Types:
One-dimensional Arrays
Two-dimensional Arrays
Multi-dimensional Arrays
One Dimensional Array
A list of items tha
TA C252
Computer Programming - II
Todays Agenda
Course Motivation
Course Objective
Course Outline
Course Administration
Course Motivation
Be a good programmer
Things to be learned
Obviously, a programming language
(For us C)
Important
Whether well pre