Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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)
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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)
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
Binary Trees
Data
Linear data structures
structure = Organized data + Allowed
operations
Here are some of the data structures we have
studied so far:
Arrays
Singlylinked
lists and doublylinked lists
Stacks and queues
These all have the property th
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
Computer ProgrammingII
TA C252
Design and Analysis of Algorithms
S.K. Sahay
BITSPilani, 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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
TA GC252
Computer ProgrammingII
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
Functions
Introduction
A function is a selfcontained 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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
Arrays
Introduction
Definition : An array is a fixedsize
sequenced collection of elements of the
same fundamental data type
Array Types:
Onedimensional Arrays
Twodimensional Arrays
Multidimensional Arrays
One Dimensional Array
A list of items tha
Birla Institute of Technology & Science, Pilani  Hyderabad
CSE 105

Summer 2010
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