0% found this document useful (0 votes)
2 views

Data Structure Lect12

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Structure Lect12

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Discrete Structure

Lecture 12
Topics

 Recursive Algorithm

 Factorial Example

 Recursive Function

 Example

 Trees
Recursive Algorithm

 An algorithm is called recursive if it solves a problem by reducing it to an


instance of the same problem with smaller input.
Example of Recursive Algorithm

 Give a recursive algorithm for computing n!, where n is a nonnegative integer


 3! = 3 · 2!,
 2! = 2 · 1!,
 1! = 1 · 0!
 0! = 1;

 3 x2 x 1= 6
 You can write like that n(n-1)
Recursive Algorithm for n!

 procedure factorial(n: nonnegative integer)


 if n = 0 then return 1
 else return n · factorial(n − 1)
 {output is n!}
Recursive Picture
Recursive
Sometimes it is difficult to define an object explicitly. However, it may be easy
to define this object in terms of itself. This process is called recursion.

For instance, the picture shown in Previous slide is produced recursively.

First, an original picture is given. Then a process of successively superimposing


centered smaller pictures on top of the previous pictures is carried out.
Recursive Defined Function
We use two steps to define a function with the set of nonnegative integers as its domain:

BASIS STEP: Specify the value of the function at zero.

RECURSIVE STEP: Give a rule for finding its value at an integer from its values at smaller
integers.

Such a definition is called a recursive or inductive definition


Suppose f is defined recursively
Suppose that f is defined recursively by
f (0) = 3,
f (n + 1) = 2f (n) + 3.
Find f (1), f (2), f (3), and f (4).

Solution: From the recursive definition it follows that


f (1) = 2f (0) + 3 = 2 · 3 + 3 = 9,
f (2) = 2f (1) + 3 = 2 · 9 + 3 = 21,
f (3) = 2f (2) + 3 = 2 · 21 + 3 = 45,
f (4) = 2f (3) + 3 = 2 · 45 + 3 = 93.
Extended Binary Tree
The set of extended binary trees can be defined recursively by these steps:

BASIS STEP: The empty set is an extended binary tree.

RECURSIVE STEP: If T1 and T2 are disjoint extended binary trees, there is an


extended binary tree, denoted by T1 · T2, consisting of a root r together with
edges connecting the root to each of the roots of the left subtree T1 and the
right subtree T2 when these trees are nonempty
Extended Binary Tree
Full Binary Tree
The set of full binary trees can be defined recursively by these steps:

BASIS STEP: There is a full binary tree consisting only of a single vertex r.

RECURSIVE STEP: If T1 and T2 are disjoint full binary trees, there is a full binary tree,
denoted by T1 · T2, consisting of a root r together with edges connecting the root to
each of the roots of the left subtree T1 and the right subtree T2.
Full Binary Tree

You might also like