4. Stacks - Applications.data.structure.pdf

airaaheer 0 views 152 slides Nov 01, 2025
Slide 1
Slide 1 of 152
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113
Slide 114
114
Slide 115
115
Slide 116
116
Slide 117
117
Slide 118
118
Slide 119
119
Slide 120
120
Slide 121
121
Slide 122
122
Slide 123
123
Slide 124
124
Slide 125
125
Slide 126
126
Slide 127
127
Slide 128
128
Slide 129
129
Slide 130
130
Slide 131
131
Slide 132
132
Slide 133
133
Slide 134
134
Slide 135
135
Slide 136
136
Slide 137
137
Slide 138
138
Slide 139
139
Slide 140
140
Slide 141
141
Slide 142
142
Slide 143
143
Slide 144
144
Slide 145
145
Slide 146
146
Slide 147
147
Slide 148
148
Slide 149
149
Slide 150
150
Slide 151
151
Slide 152
152

About This Presentation

stack operation


Slide Content

Data
STRUCTURES
AND
ALGORITHMS
Stacks
Week 4

STACK
•Suppose you want to put these balls
on this tower.
2

STACK
•First, we put a red ball.
3

STACK
•Now, we put a green ball.
4

STACK
•Next, we put a black ball on top of it.
5

STACK
•At the end, we put the blue ball on top
of it.
6

STACK
•Now, please answer the following questions.
•Can we get the black ball before getting the blue
ball?
•Can we insert a new ball between green and red
ball?
•The answer to both of these questions is: NO
7

STACK
•It is because, there is only one way to remove
or insert a ball.
•We can insert any ball only at the top of other balls
(if any)
•We can remove only the topmost ball at a time.
•We have no direct access to any other balls, except
the one at the top.
•Such a list is called a “Stack”.
8

STACK
•In a stack, we have two operations:
•Push(X)
•Pop()
•Push(X) means to insert “X” at the top of the
stack.
•Pop() means to remove the topmost element
of the stack.
9

STACK
•In a stack, we have two operations:
•Push(X)
•Pop()
•Push(X) means to insert “X” at the top of the
stack.
•Pop() means to remove the topmost element
of the stack.
•If there is nothing in the stack, it is empty.
10

STACK
•Similarly, initially stack is empty.
11

STACK
•Similarly, initially stack is empty.
•After performing the following five operations,
the stack looks like this.
•Push(“First”)
•Push(“Second”)
•Push(“Third”)
•Push(“Fourth”)
•Push(“Last”)
12

Last
Fourth
Third
Second
First

STACK
•Now, we will perform the following operation.
•Pop()
13

Last
Fourth
Third
Second
First

STACK
•Now, we will perform the following operation.
•Pop()
•You will see that the “Last” element has been
popped out first.
•In other words, we can say that the behavior of
Stack is LIFO (Last in First out).
•In other words, FILO (First in Last out).
14

Fourth
Third
Second
First
Last

STACK
The stack is a simple data structure. We may have been using a stack
in our lives without even realizing it!
15

STACK ADT
•A stack is an Abstract Data Type (ADT), commonly used in
most programming languages.
•It is named stack as it behaves like a real-world stack.
•We can place or remove a card or plate from the top of the
stack only.
•At any given time, we can only access the top element of a
stack.
16

STACK ADT
17

Implementation
•A stack can be implemented by:
•Arrays
•Linked List
•In the first case, it will be of fixed size because arrays are basically
static.
•In the second case, stack will be of dynamic size.
•Each has its own merits & demerits.
18

Basic Operations on LIST ADT
•Push() To insert at the top of stack
•Pop() To access and remove the top of stack
•Peek() To access the top of stack without removal
•The stack will be full when its top reaches the maximum limit.
•The stack will be considered empty, if there is no element in the
stack.
19

Array Based Implementation ― Push()
Algorithm Push(value)
//MAX is the maximum size of Array (Stack)
If Top == MAX Then return FALSE
Top++
Stack[Top] = value
return TRUE
End Push
20

Push()
Algorithm Push(value)
//MAX is the maximum size of Array (Stack)
If Top == MAX Then return FALSE
Top++
Stack[Top] = value
return TRUE
End Push
21
Top
MAX = 5
Value = 10
5
4
3
2
1
0

Push()
Algorithm Push(value)
//MAX is the maximum size of Array (Stack)
If Top == MAX Then return FALSE
Top++
Stack[Top] = value
return TRUE
End Push
22
MAX = 5
Value = 10
5
4
3
2
1
0Top

Push()
Algorithm Push(value)
//MAX is the maximum size of Array (Stack)
If Top == MAX Then return FALSE
Top++
Stack[Top] = value
return TRUE
End Push
23
MAX = 5
Value = 10
5
4
3
2
1
0
Top

Push()
Algorithm Push(value)
//MAX is the maximum size of Array (Stack)
If Top == MAX Then return FALSE
Top++
Stack[Top] = value
return TRUE
End Push
24
10
MAX = 5
Value = 10
5
4
3
2
1
0
Top

Push()
Algorithm Push(value)
//MAX is the maximum size of Array (Stack)
If Top == MAX Then return FALSE
Top++
Stack[Top] = value
return TRUE
End Push
25
10
MAX = 5
Value = 10
5
4
3
2
1
0
Top

Push()
Algorithm Push(value)
//MAX is the maximum size of Array (Stack)
If Top == MAX Then return FALSE
Top++
Stack[Top] = value
return TRUE
End Push
26
12
11
-5
4
10
MAX = 5
Value = 5
5
4
3
2
1
0
Top

Push()
Algorithm Push(value)
//MAX is the maximum size of Array (Stack)
If Top == MAX Then return FALSE
Top++
Stack[Top] = value
return TRUE
End Push
27
12
11
-5
4
10
MAX = 5
Value = 5
5
4
3
2
1
0
Top

Array Based Implementation ― Pop()
Algorithm Pop()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
value = Stack[Top]
Top ― ―
return value
End Pop
28

Pop()
Algorithm Pop()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
value = Stack[Top]
Top ― ―
return value
End Pop
29
10Top
MAX = 5
5
4
3
2
1
0

Pop()
Algorithm Pop()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
value = Stack[Top]
Top ― ―
return value
End Pop
30
10Top
MAX = 5
5
4
3
2
1
0

Pop()
Algorithm Pop()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
value = Stack[Top]
Top ― ―
return value
End Pop
31
10Top
MAX = 5
5
4
3
2
1
0
value = 10

Pop()
Algorithm Pop()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
value = Stack[Top]
Top ― ―
return value
End Pop
32
10
Top
MAX = 5
5
4
3
2
1
0
value = 10

Pop()
Algorithm Pop()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
value = Stack[Top]
Top ― ―
return value
End Pop
33
10
Top
MAX = 5
5
4
3
2
1
0
value = 10

Pop()
Algorithm Pop()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
value = Stack[Top]
Top ― ―
return value
End Pop
34
10
Top
MAX = 5
5
4
3
2
1
0
value = 10
return 10

Pop()
Algorithm Pop()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
value = Stack[Top]
Top ― ―
return value
End Pop
35
Top
MAX = 5
5
4
3
2
1
0

Pop()
Algorithm Pop()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
value = Stack[Top]
Top ― ―
return value
End Pop
36
Top
MAX = 5
5
4
3
2
1
0
return NULL

Array Based Implementation ― Peek()
Algorithm Peek()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
return Stack[Top]
End Peek
37
5
2
-3
Top
5
4
3
2
1
0

Linked List Based Implementation ― Push()
Algorithm Push(value)
//Initially Position is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL

Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
38

Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL
Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
39
0412
NULL
Head

Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL
Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
40
0
4
12
NULL
Head

CASE I: Push( ) On top of a stack
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL
Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
41
0
4
12
NULL
Head
Value = 10
Size = 3

CASE I: Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL
Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
42
0
4
12
NULL
Head
Value = 10
Size = 3
NewNode
NULL

CASE I: Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL
Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
43
0
4
12
NULL
Head
Value = 10
Size = 3
10 NewNode
NULL

CASE I: Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL
Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
44
0
4
12
NULL
Head
Value = 10
Size = 3
10 NewNode
NULL
FALSE

CASE I: Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL
Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
45
0
4
12Head
Value = 10
Size = 3
10
NewNode
NULL

CASE I: Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL
Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
46
0
4
12
Head
Value = 10
Size = 3
10
NewNode
NULL

CASE I: Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL
Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
47
0
4
12
Head
Value = 10
Size = 4
10
NewNode
NULL

CASE I: Push( )
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
Head = N
N.next = NULL
Else
N.next = Head
Head = N
End If
Position++
return TRUE
End Push
48
0
4
12
Head
Value = 10
Size = 4
10
NewNode
NULL

CASE II: Push( ) In an Empty Stack
Algorithm Push(value) //Initially Size is 0, when stack is empty
If Size == MAX
return FALSE
Create New Node NewNode
NewNode.data = value
If Head == NULL
Head = NewNode
Else
Top.Up = NewNode
End If
Top = NewNode
Size++
return TRUE
End Push
49
Head
Top
Value = 20
Size = 0
NULL

CASEII: Push( )
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
N.next = NULL;
Else
N.next = Head;
End If
Head = N
Size++
return TRUE
End Push
50
Head
Value = 20
Size = 0
NULL

CASEII: Push( )
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
N.next = NULL;
Else
N.next = Head;
End If
Head = N
Size++
return TRUE
End Push
51
Head
Value = 20
Size = 0
NULL
NewNode

CASEII: Push( )
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
N.next = NULL;
Else
N.next = Head;
End If
Head = N
Size++
return TRUE
End Push
52
Head
Value = 20
Size = 0
NULL
20 NewNode

CASEII: Push( )
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
N.next = NULL;
Else
N.next = Head;
End If
Head = N
Size++
return TRUE
End Push
53
Head
Value = 20
Size = 0
NULL
20
NULL
NewNode
TRUE

Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
N.next = NULL;
Else
N.next = Head;
End If
Head = N
Size++
return TRUE
End Push
54
Head
Value = 20
Size = 0
20
NULL
NewNode

CASEII: Push( )
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
N.next = NULL;
Else
N.next = Head;
End If
Head = N
Size++
return TRUE
End Push
55
Head
Value = 20
Size = 1
20
NULL
NewNode

CASEII: Push( )
Algorithm Push(value) //Initially Size is 0, when stack is empty
N = New Node
If N == NULL
return FALSE
N.data = value
If Head == NULL
N.next = NULL;
Else
N.next = Head;
End If
Head = N
Size++
return TRUE
End Push
56
Head
Value = 20
Size = 1
return TRUE
20
NULL
NewNode

Linked List Based Implementation ― Pop()
57
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
58
Top
20
NULL
Size = 1
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
59
Head
Top
20
NULL
Size = 1
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop
FALSE

Pop()
60
Top
20
NULL
Size = 1
value = 20Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
61
Top
20
NULL
Size = 1
value = 20Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
62
Top
NULL
Size = 1
value = 20Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
63
Top
NULL
Size = 0
value = 20
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop
64
Top
NULL
Size = 0
value = 20
return 20

Pop()
65
Size = 4
0
4
12Top
10
NULL
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
66
Size = 4
0
4
12Top
10
NULL
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop
FALSE

Pop()
67
Size = 4
Value = 12
0
4
12Top
10
NULL
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
68
Size = 4
Value = 12
0
4
12Top
10
NULL
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop
FALSE

Pop()
69
Size = 4
Value = 12
0
4
12Top
10
NULL
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
70
Size = 4
Value = 12
0
4
12Top
10
NULL
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop
N

Pop()
71
Size = 4
Value = 12
0
4
12
Top
10
NULL
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop
N

Pop()
72
Size = 4
Value = 12
0
4
Top
10
NULL
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
73
Size = 3
Value = 12
0
4
Top
10
NULL
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Pop()
74
Size = 3
Value = 12
0
4
Top
10
NULL
Algorithm Pop( )
If Size == 0 Then return NULL
value = Top.data
If Size == 1
Delete Top
Top = NULL
Else
N == Top
Top = Top. Next
Delete N
End If
Size ― ―
return value
End Pop

Applications of Stack
•Stacks can be used for expression evaluation.
•Stacks can be used to check parenthesis matching in an expression.
•Stacks can be used forConversion from one form of expression to
another.
•Stacks can be used for Memory Management.
75

Applications of Stack
•Stack data structures are used in backtracking problems.
•Many compilers use a stack for parsing the syntax of expressions.
•Stack is used to reverse a string or any other array.
•Stack is used to keep information about the active functions or
subroutines.
•Keeping track of operations for managing the UNDO function.
76

Practice Question
•We have implemented a stack using arrays and singly linked list.
•Construct algorithms for basic stack operations if stack is
implemented using a doubly linked list.
77

Applications of Stack
•Stacks can be used for expression evaluation.
•Stacks can be used to check parenthesis matching in an expression.
•Stacks can be used forConversion from one form of expression to
another.
•Stacks can be used for Memory Management.
78

Applications of Stack
•Stack data structures are used in backtracking problems.
•Many compilers use a stack for parsing the syntax of expressions.
•Stack is used to reverse a string or any other array.
•Stack is used to keep information about the active functions or
subroutines.
•Keeping track of operations for managing the UNDO function.
79

The Call Stack
•Your computer uses a stack internally called the call stack. Let’s see it
in action. Here’s a simple function:
•This function greets you and then calls two other functions. Here are
those two functions:
80

The Call Stack
•Let’s walk through what happens when you call a function.
•Suppose you call greet(“maggie”).
•First, your computer allocates a box of memory for that function call.
81

The Call Stack
•Let’s walk through what happens when you call a function.
•Suppose you call greet(“maggie”).
•First, your computer allocates a box of memory for that function call.
•Now let’s use the memory.
•The variable name is set to “maggie”.
•That needs to be saved in memory.
82

The Call Stack
•Every time you make a function call, your computer
saves the values for all the variables for that call in
memory like this.
•Next, you print hello, maggie!
•Then you call greet2(“maggie”).
•Again, your computer allocates a box of memory
for this function call.
83

The Call Stack
•Your computer is using a stack for these
boxes.
•The second box is added on top of the first
one.
•You print how are you, maggie?
•Then you return from the function call.
•When this happens, the box on top of the
stack gets popped off.
84

The Call Stack
•Your computer is using a stack for these
boxes.
•The second box is added on top of the first
one.
•You print how are you, maggie?
•Then you return from the function call.
•When this happens, the box on top of the
stack gets popped off.
85

The Call Stack
•Now the topmost box on the stack is for the greet
function, which means you returned back to the greet
function.
•When you called the greet2 function, the greet
function was partially completed.
•This is the big idea behind this section: when you call a
function from another function, the calling function is paused
in a partially completed state.
•All the values of the variables for that function are still
stored in memory.
86

The Call Stack
•Now that you’re done with the greet2 function,
you’re back to the greet function, and you pick up
where you left off.
•First you print getting ready to say bye…. You call
the bye function.
•A box for that function is added to the top of the
stack.
87

The Call Stack
•Then you print ok bye! and return from the
function call.
•Bye function is popped out.
•And now you’re back to the greet function.
88

The Call Stack
•In greet function, there’s nothing else to be done
now.
•So, we will pop it too.
89

The Call Stack
•The stack is now empty.
•This stack, used to save the variables for multiple
functions, is called the call stack.
90

The Call Stack with Recursion
•Recursive functions use the call stack too!
•Let’s look at this in action with the factorial function.
•factorial(5) is written as 5!, and it’s defined like this:
5 * 4 * 3 * 2 * 1.
•Similarly, factorial(3) is:
3 * 2 * 1.
91

The Call Stack with Recursion
•Here’s a recursive function to calculate the factorial of a number:
•Now let’s call fact(3) and step through this call line by line and see
how the stack changes.
92

The Call Stack with Recursion
93

The Call Stack with Recursion
94

The Call Stack with Recursion
95

The Call Stack with Recursion
96

Example 5
97
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

Example 5
98
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

Example 5
99
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

A(3)

Example 5
100
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

A(2)
A(3)

Example 5
101
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

A(1)
A(2)
A(3)

Example 5
102
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

A(0)
A(1)
A(2)
A(3)

Example 5
103
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

A(1)
A(2)
A(3)

Example 5
104
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

A(2)
A(3)

Example 5
105
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

A(3)

Example 5
106
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

Example 5
107
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

You can see that for an
Input of size n, we need
a stack of height n+1.

Example 5
108
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

If each block of stack
consists of k memory
locations, then total
space required is k(n+1)

Example 5
109
A( n )
{
If n >= 1
A(n-1);
//do something
}
A(3)
A(2)//do something
A(1) //do something
A(0) //do something
A(0) 1 call
A(1) 2 calls
A(2) 3 calls
A(3) 4 calls
⋮ ⋮
A(n) n+1 calls

S(n) = k(n+1) = O(n)

Example 6
110
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 call
A(1) 3 calls
A(2) 7 calls
A(3) 15 calls
⋮ ⋮
A(n) m calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

Example 6
111
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
S(n) = k(2
n+1
-1) = O(2
n
)

Example 6
112
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

Example 6
113
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2) 7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(3)

Example 6
114
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(2)
A(3)

Example 6
115
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(1)
A(2)
A(3)

Example 6
116
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0)
A(1)
A(2)
A(3)

Example 6
117
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0)
A(1)
A(2)
A(3)
1

Example 6
118
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0)
A(1)
A(2)
A(3)
1

Example 6
119
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0)
A(1)
A(2)
A(3)
1

Example 6
120
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0)
A(1)
A(2)
A(3)
12

Example 6
121
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0)
A(1) A(1)
A(2)
A(3)
12

Example 6
122
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0)
A(1) A(1)
A(2)
A(3)
12

Example 6
123
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0)
A(1) A(1)
A(2)
A(3)
121

Example 6
124
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(1) A(1)
A(2)
A(3)
121

Example 6
125
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(1) A(1)
A(2)
A(3)
121

Example 6
126
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(1) A(1)
A(2)
A(3)
121

Example 6
127
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(1) A(1)
A(2)
A(3)
1213

Example 6
128
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(1) A(1)
A(2) A(2)
A(3)
1213

Example 6
129
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(1) A(1) A(1)
A(2) A(2)
A(3)
1213

Example 6
130
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0)
A(1) A(1) A(1)
A(2) A(2)
A(3)
1213

Example 6
131
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0)
A(1) A(1) A(1)
A(2) A(2)
A(3)
12131

Example 6
132
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0)
A(1) A(1) A(1)
A(2) A(2)
A(3)
12131

Example 6
133
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0)
A(1) A(1) A(1)
A(2) A(2)
A(3)
12131

Example 6
134
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0)
A(1) A(1) A(1)
A(2) A(2)
A(3)
121312

Example 6
135
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0)
A(1) A(1) A(1) A(1)
A(2) A(2)
A(3)
121312

Example 6
136
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0) A(0)
A(1) A(1) A(1) A(1)
A(2) A(2)
A(3)
121312

Example 6
137
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0) A(0)
A(1) A(1) A(1) A(1)
A(2) A(2)
A(3)
1213121

Example 6
138
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0) A(0) A(0)
A(1) A(1) A(1) A(1)
A(2) A(2)
A(3)
1213121

Example 6
139
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0) A(0) A(0)
A(1) A(1) A(1) A(1)
A(2) A(2)
A(3)
1213121

Example 6
140
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0) A(0) A(0)
A(1) A(1) A(1) A(1)
A(2) A(2)
A(3)
1213121

Example 6
141
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0) A(0) A(0)
A(1) A(1) A(1) A(1)
A(2) A(2)
A(3)
1213121

Example 6
142
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

A(0) A(0) A(0) A(0)
A(0) A(0) A(0) A(0)
A(1) A(1) A(1) A(1)
A(2) A(2)
A(3)
1213121

Example 6
143
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2)7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

You can see that for an
Input of size n, we need
a stack of height n+1.

Example 6
144
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2) 7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

If each block of stack
consists of k memory
locations, then total
space required is k(n+1)

Example 6
145
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}
A(3)
A(2)P(3)
A(0) 1 = 2
0+1
― 1 call
A(1)3 = 2
1+1
― 1 calls
A(2) 7 = 2
2+1
― 1 calls
A(3)15 = 2
3+1
― 1 calls
⋮ ⋮
A(n)m = 2
n+1
― 1 calls
A(2)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)
A(1)P(2)A(1)
A(0)P(1)A(0)A(0)P(1)A(0)

S(n) = k(n+1) = O(n)

Analyzing Time Complexity
T(n) = 2T(n-1) + c; n>=1
T(0) = c  This is the Anchor Condition
-------------------------------------------------------------------------
T(n) =2T(n-1)+c …eq(i)
T(n-1) =2T(n-1-1)+c
=2T(n-2)+c …eq(ii)
T(n-2) =2T(n-2-1)+c
=2T(n-3)+c …eq(iii)
146
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}

Analyzing Time Complexity
Putting eq(ii) in eq(i):
T(n) =2T(n-1)+c
=2[2T(n-2)+c] +c
=2
2
T(n-2)+2c+c …eq(iv)
Putting eq(iii) in eq(iv):
T(n) =2
2
[2T(n-3)+c]+2c+c
=2
3
T(n-3)+2
2
c+2c+c
Suppose we reach the anchor condition after k

calls:
T(n) =2
k
T(n-k)+2
k-1
c+…+2
2
c+2c+c ..eq(v)
147
T(n) =2T(n-1)+c …eq(i)
T(n-1)=2T(n-2)+c …eq(ii)
T(n-2)=2T(n-3)+c …eq(iii)
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}

Analyzing Time Complexity
As we know that T(0) will be the last recursive call, so
for what value of k will T(n-k) become T(0).
n-k=0  n=k
So eq(v) will become:
T(n) = 2
k
T(n-k)+2
k-1
c+…+2
2
c+2c+c
= 2
n
T(0)+2
n-1
c+…+2
2
c+2c+c
= 2
n
c+2
n-1
c+…+2
2
c+2c+c
= c(2
n
+2
n-1
+…+2
2
+2+1)
= c(2
n+1
― 1)
= O(2
n
)
148
A( n )
{
If n >= 1
A(n-1);
print n
A(n-1);
}

Converting
Infix to
Post-fix
Using Stack
149

Evaluation of
Post-fix
Using Stack

Infix to
Pre-fix using
Stack
151

Evaluation of
Pre-fix Using
Stack
152
Tags