Docsity

Recursion and Tree Data Structures: Exercise Solutions - Prof. Sakti Pramanik, Assignments of Discrete Structures and Graph Theory

Toggle sidebar
bg1
CSE 260
Class Exercise: Recursion: answer
1. We define the sequence {an}recursively as follows:
a0=a1=a2= 1 and an=anโˆ’1+anโˆ’2+anโˆ’3for nโ‰ฅ3
Give
a4= 5
a5= 9
2. Recursive definition of Factorial function, Fact(N) is given below. Forward recursive
calls for Fact(3) are shown by arrows. Complete the return paths (return path for
Fact(0) is shown) for these recursive calls and the values returned for these recursive
calls.
Fact(N)
{ If N=1 return 1
Else return N*Fact(N-1)
}
-------------------------
Fact(3) --> 6
{ If 3=0 return 1 /
Else return 3*Fact(2) <--
} | /
Fact(2) <--------- / 2
{ If 2=0 return 1 /
Else return 2*Fact(1) <--
} | | 1
Fact(1) <--------- |
{ If 1=0 return 1 ---------
Else return 1*Fact(0) <----
} | | 1
Fact(0) <--------- |
{ If 0=0 return 1 ---------
Else return 0*Fact(-1)
}
3. (a) Recursive definition of tree data structures:
Extended binary tree is defined recursively as follows:
Basis Step: The empty set, โˆ…, is an extended binary tree.
Recursive Step: If T1and T2are 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 T1and the right subtree
T2when these trees are nonempty.
pf2

Partial preview of the text

Download Recursion and Tree Data Structures: Exercise Solutions - Prof. Sakti Pramanik and more Assignments Discrete Structures and Graph Theory in PDF only on Docsity!

CSE 260

Class Exercise: Recursion: answer

  1. We define the sequence {an} recursively as follows:

a 0 = a 1 = a 2 = 1 and an = anโˆ’ 1 + anโˆ’ 2 + anโˆ’ 3 for n โ‰ฅ 3 Give a 4 = 5 a 5 = 9

  1. Recursive definition of Factorial function, Fact(N) is given below. Forward recursive calls for Fact(3) are shown by arrows. Complete the return paths (return path for Fact(0) is shown) for these recursive calls and the values returned for these recursive calls.

Fact(N) { If N=1 return 1 Else return N*Fact(N-1) }


Fact(3) --> 6 { If 3=0 return 1 / Else return 3Fact(2) <-- } | / Fact(2) <--------- / 2 { If 2=0 return 1 / Else return 2Fact(1) <-- } | | 1 Fact(1) <--------- | { If 1=0 return 1 --------- Else return 1Fact(0) <---- } | | 1 Fact(0) <--------- | { If 0=0 return 1 --------- Else return 0Fact(-1) }

  1. (a) Recursive definition of tree data structures:

Extended binary tree is defined recursively as follows:

Basis Step: The empty set, โˆ…, is an extended binary tree.

Recursive Step: If T 1 and T 2 are disjoint extended binary trees, there is an

extended binary tree, denoted by T 1 .T 2 , consisting of a root r together with edges connecting the root to each of the roots of the left subtree T 1 and the right subtree T 2 when these trees are nonempty.

Generate the following extended binary tree using the above recursive definition.

0 A / / 0 B

Basis Step: โˆ… Step 1 (Recursive Step): Tree with node B (root node B and two โˆ… left and right subtrees). Step 2 (Recursive Step): Create tree with root node A and left child is the subtree with root B and the right child is the subtree โˆ….

0 A / / 0 B

(b) If we replace the above Basis Step by the following Basis Step and keep

the Recursive Step same, it becomes the definition of Full Binary Trees.

Basis Step: There is a Full Binary Tree consisting only of a single vertex r.

Show why the tree created in part (a) cannot be created by the definition of Full Binary Trees.

Right child of A is a subtree which is โˆ…. But โˆ… is not defined as a Full Binary Tree.

  1. Consider the predicate parent(x,y): x is the parent of y. Give the rules for defining the following two predicates sibling(x,y): x and y are siblings if they have the same parents cousin(x,y): x, y are cousins if their parents are siblings or cousins Note that the definition of cousin is recursive. sibling(x,y):= parent(z,x), parent(z,y), x 6 = y cousin(x,y):= (parent(z,x), parent(w,y), x 6 = y), (sibling(z,w);cousin(z,w))