Professional Documents
Culture Documents
Resursion
Stack
• A stack is also known as a Last-In-First-Out
(LIFO) list.
• A stack is a linear structure in which items are
added or removed only at one end the top.
• The depth of stack is the number of elements
it contains.
• An empty stack has depth zero.
Stack Example
Stack Operations
• Some stack operations:
STACK
1 2 3 4 5 6 7 8
MAXSTK 8
TOP 3
PUSH()
• This procedure pushes an ITEM onto a stack
PUSH(STACK,TOP,MAXSTK,ITEM)
1. IF TOP = MAXSTK, then
POP(STACK,TOP,ITEM)
1. IF TOP = 0, then
Remove Insert
(Dequeue) front rear (Enqueue)
Implementing a Queue
Front:1, Rear:4
using Array
QUEUE
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
Implementing a Queue
using Array
Front:2, Rear:6 QUEUE
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
Circular Queue
• Suppose we want to insert an element into the queue
after it occupies the last part of the array i.e. REAR=n.
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
Double-Ended-QUE
• There are two variations of deque
– Input-restricted deque
An input restricted deque is a deque which allows
insertion at only one end of the list but allows
deletion a both end of the list.
– Output-restricted deque
An outut restricted deque is a deque which allows
deletion at only one end of the list but allows
insertin a both end of the list.
Question
• Consider a circular DEQUE with six memory cells.
DEQUE:_,A,C,D,_,_ LEFT=2, RIGHT=4
a)Add F to the right
b)Two letters are deleted from the right
c)K,L, and M are added to the left
d)One letter on the left is deleted
e)R is added to the left
f) S is added to the right
g)T is added to the right
Priority Queues
• A priority queue is a collection of elements
such that each element has been assigned a
priority and such that the order in which the
elements are deleted and processed comes
from the following rules:
1. An element of higher priority is processed
before any element of lower priority.
2. Two elements of the same priority are
processed according to the order in which they
were add to the queue.
Priority Queues Operations
• insert -Add a new item to the queue.
• Thus 44 is correctly placed in its final position, and the task of sorting the original
list A has been reduced to the task of sorting each of the above sub list.
• The above reduction step is repeated with each sub list containing 2 or more
elements. Since we can process only one sub list at a time, we must be able o
keep track of some sub lists for future processing. This is accomplished by
processing two stacks, called LOWER and UPPER, to temporarily old such sub list.
That is the address of the first and last elements of each sub list, called its
boundary values, are pushed onto the stacks LOWER and UPPER, respectively;
and the reduction set is applied to a sub list only after its boundary values are
removed from the stack.
Quick sort Algorithm
This algorithm sorts an array A with N elements
1. [Initialize] TOP=NULL
2. [Push boundary values of A onto stack when A has 2 or more elements]
If N>1, then TOP=TOP+1, LOWER[1]=1 and UPPER[1]=N
3. Repeat Step 4 to 7 while TOP!= NULL
4. [Pop sub list from stack]
Set BEG=LOWER[TOP], END=UPPER[TOP]
TOP=TOP-1
5. Call QUICK(A,N,BEG,END,LOC)
6. [Push left sub list onto stacks when it has 2 or more elements]
If BEG<LOC-1 then
TOP=TOP+1, LOWER[TOP]=BEG, UPPER[TOP]=LOC-1
[End of If structure]
5. [Push right sub list onto stacks when it has 2 or more elements]
If LOC+1 < END then
TOP=TOP+1, LOWER[TOP]= LOC+1, UPPER[TOP]= END
[End of If structure]
[End of Step 3 loop]
5. Exit
A is an array with N elements. Parameters BEG and END contain the boundary
values of the sub list of A to which this procedure applies. LOC keeps track of
the position of the first element A[BEG] of the sub list during the procedure.
The local variables LEFT and RIGHT will contain the boundary values of the
list of elements that have nor been scanned.
QUICK(A,N,BEG,END,LOC)
We could write:
6! = 6 * 5!
Example 1: factorial
function
In general, we can express the
factorial function as follows:
n! = n * (n-1)!
Iterative
Recursive solution
solution
int fac(int numb){ int fac(int numb){
if(numb<=1) int product=1;
return 1; while(numb>1){
else product *= numb;
return numb*fac(numb-1);numb--;
} }
return product;
}
Recursion
To trace recursion, recall that function calls operate
as a stack – the new function is put on top of the
caller
int result = 1;
while(result >0){
...
result++;
}
Recursion
Similarly, if we use recursion we must be
careful not to create an infinite chain of
function calls:
int fac(int numb){
return numb * fac(numb-1); No termination
} condition
Or:
int fac(int numb){
if (numb<=1)
return 1;
else
return numb * fac(numb+1);
}
Recursion
We must always make sure that the
recursion bottoms out:
• Recursive definition:
– F(0) = 0;
– F(1) = 1;
– F(number) = F(number-1)+ F(number-2);
Example 3: Fibonacci
numbers
//Calculate Fibonacci numbers using recursive function.
//A very inefficient way, but illustrates recursion well
int fib(int n)
{
int f[n+1];
f[0] = 0; f[1] = 1;
for (int i=2; i<= n; i++)
f[i] = f[i-1] + f[i-2];
return f[n];
}
Example 3: Binary Search
– Search for an element in an array
• Sequential search
• Binary search
– Binary search
• Compare the search element with the
middle element of the array
• If not equal, then apply binary search
to half of the array (if not empty)
where the search element would be.
Binary Search with
Recursion
// Searches an ordered array of integers using recursion
int bsearchr(const int data[], // input: array
int first, // input: lower bound
int last, // input: upper bound
int value // input: value to find
)// output: index if found, otherwise return –1
return 0;
}
Binary Search w/o recursion
// Searches an ordered array of integers
int bsearch(const int data[], // input: array
int size, // input: array size
int value // input: value to find
){ // output: if found,return
// index; otherwise, return -1
1
2
3
A B C
– Only one disc could be moved at a time
– A larger disc must never be stacked above a
smaller one
– One and only one extra needle could be used
for intermediate storage of discs
Towers of Hanoi
• From the moves necessary to transfer one, two,
and three disks, we can find a recursive pattern
- a pattern that uses information from one step
to find the next step.
1 21 - 1 = 2 - 1 = 1
2 22 - 1 = 4 - 1 = 3 3 23 - 1 = 8 - 1 = 7
4 24 - 1 = 16 - 1 = 15
5 25 - 1 = 32 - 1 = 31
6 26 - 1 = 64 - 1 = 63
• So the formula for finding the number of steps it takes
to transfer n disks from post A to post C is:
2 n- 1
Binomial coefficient
• Given a non-negative integer n and
an integer k, the binomial coefficient
is defined to be the natural number
• Example: