Faculty of Computer Science and Engineering

Department of Computer Science

1/4

DATA STRUCTURES & ALGORITHMS

Tutorial 3 Questions

Recursion and Binary Tree

Part 1. Recursion

Required Questions

Question 1.

What would be the contents of queue Q1 after the following code is executed and the

following data are entered?

1 Q1 = createQueue

2 S1 = createStack

3 loop (not end of file)

1 read number

2 if (number not 0)

1 pushStack (S1, number)

3 else

1 popStack (S1, x)

2 popStack (S1, x)

3 loop (not empty S1)

1 popStack (S1, x)

2 enqueue (Q1, x)

4 end loop

4 end if

4 end loop

The data are: 9, 5, 10, 4, 0, 5, 4, 6, 8, 67, 32, 25, 51, 0, 54, 23, 20, 6, 10

Question 2.

The following algorithm is to reverse a queue

Algorithm reverse (val q <Queue>)

Pre true

Return a reversed queue of q

1 S = createStack

2 Q = createQueue

3 while (not empty(q))

1 dequeue(q,temp)

2 pushStack(S,temp)

4 while (not empty(S))

1 popStack(S,temp)

2 enqueu (Q,temp)

4 return Q

End reverse

Develop a similar algorithm to append a stack to a queue

Algorithm append (val q <Queue>, val s <Stack>)

Pre true

S1=

Q1=

5, 4, 6, 8, 67, 32,9,5

10,6,20,23,54

Algorithm append ((val q <Queue>, val s

<Stack>))

Pre true

Return a reversed queue of q

1 S = createStack

2 Q = creatQueue

3 while(not empty(S))

1 popStack(S,temp)

2 enqueue(q,temp)

4

while(not empty(q))

1

reverse(Q)

5

dequeue(q,temp)

2

enqueue(Q,temp)

6

return Q

End append

Faculty of Computer Science and Engineering

Department of Computer Science

2/4

Return element of s is appended into q with the same order. For

example if q = {1,2,3}, s = {4,5,6} then q = {1,2,3,4,5,6} after

append.

Queue {front rear}

Stack {bottom top}

Question 3.

Consider the following algorithm:

Algorithm fun1 (x <integer>)

1 if (x < 5)

1 return (2 * x)

2 else

1 return (2 * fun1 (x – 2) + 5)

3 end if

end fun1

What would be returned if fun1 is called as

a. fun1 (4)?

b. fun1 (5)?

c. fun1 (8)?

d. fun1 (20)?

Question 4.

Develop recursive algorithm for the following problems.

a. Compute the sum of all numbers from 1 to n, where n is given as parameter.

Algorithm compute (val n <integer>)

Pre n >=0

Return the sum 0 + 1+ 2+ 3+ + n

b. Find and return the maximum element in an array, where the array and its size are

given as parameters.

Algorithm compute (val a <array>, val n <integer> )

Pre n >=0

Return the maximum element in a[]

Advanced Questions

Question 5.

Develop recursive algorithm for the following problems.

a. Find and return an element in an array, where the array and its size are given as

parameters. This element should be in middle position when the array is re-

ordered increasingly.

8

17

47

(2*(2*2

Algorithm compute (x <integer>)

1 if (x =0)

1 return (0)

2 else

1 return (n+compute(n-1))

3 end if

aánh

Algorithm compute (val a <array>, val n <integer> )

1. if (n=0)

1 return a[0]

2.else

return (compute(a,n)>compute(a,n-1))?compute(a,n):compute(a,n-1)

3233

6,5,4

4,5,6

Faculty of Computer Science and Engineering

Department of Computer Science

3/4

Algorithm compute (val a <array>, val n <integer> )

Pre n >=0

Return the the element in the middle position when the array

is reordered increasingly in a[]

For example if a = {4,1,5,2,3}, then the value of the last element should

be returned.

b. Could we design an algorithm for solving question (a) without sorting the array?

Algorithm compute (val a <array>, val n <integer> )

Pre n >=0

Return the the element in the middle position when the array

is reordered increasingly in a[]

Question 6.

Develop algorithms for the following problems. The algorithms must be fully recursive

in that they contain no loops at all (neither for, while or do-while).

a.

Algorithm listnumber (val start <integer>, val end <integer>)

Pre start <=end

Return printout the numbers from start to end to screen

b.

Algorithm mul (val a <integer>, val b <integer>)

Pre a,b >=0

Return the product of two integers a and b. The only two

arithmetic operations that you are allowed to use in this

problem are addition + and subtraction -

c.

Algorithm pow (val a <float>, int b <integer>)

Pre a,b >=0

Return the power a

b

. The only two arithmetic operations that

you are allowed to use in this problem are multiple * and

subtraction -

Question 7.

Develop fully recursive algorithms for the functions reverse and append in Question 2

return (start<=end)?end:listnumber(start-1,end)

if(b=1) return a;

else

return a+mul(a,b-1)

return (b==1)?a:a+mul(a,b-1)

return (b==1)?a:a*pow(a,b-1)

append (Queue*Q,Stack *S)

Pre true

Return a reversed queue of q

S=new Stack();

Q = new Queue();

while(S->top!=NULL)

popStack(S,temp)

enqueue(q,temp)

while(Q->front!=NULL)

reverse(Q)

dequeue(q,temp)

enqueue(Q,temp)

End append

return Q

Faculty of Computer Science and Engineering

Department of Computer Science

4/4

Part 2. Binary Tree

Required Questions

Question 8.

For each of the following key sequences determining the binary search tree obtained

when the keys are inserted one-by-one in the order given into an initially empty tree:

a) 1, 2, 3, 4, 5, 6, 7.

b) 4, 2, 1, 3, 6, 5, 7.

c) 1, 6, 7, 2, 4, 3, 5.

Question 9.

For each of the binary search trees obtained in Question 1, determine the tree obtained

when the root is withdrawn.

Question 10.

Write a global function in pseudocode to generate a BST from an input list by insert

elements in the list into an initial empty BST. Refer to Question 1 for an example.

algorithm generateBSTfromList (val list <List>)

This algorithm generate a BST from the input list

Pre

Post the BST is built by inserting elements in the list into an initial empty tree

one-by-one from the beginning of the list.

Return the BST

end generateBSTfromList

Advanced Questions

Question 11.

Devise an algorithm that takes two values, a and b such that a < b, and which visits all

the keys x in a binary search tree such that a x b.

1

2

3

4

5

6

7

1

2

3

4

5

6

7

1

2

3

4

6

7

1

2

3

4

6

7

5

2

6

7

4

5

3

1

2

3

5

6

7

1. if (subroot is NULL)

1. Allocate subroot

2. subroot->data = DataIn

3. return success

2. else if (DataIn.key < subroot->data.key)

1. return recursive_Insert(subroot->left, DataIn)

3. else if (DataIn.key > subroot->data.key)

1. return recursive_Insert(subroot->right, DataIn)

4. else

1. return duplicate_error

5. End recursive_Insert

## Tài liệu summary of SQL and SQL plus docx

## Tài liệu overview of data modeling and database design pptx

## Tài liệu William Stallings Computer Organization and Architecture P2 pptx

## Tài liệu William Stallings Computer Organization and Architecture P4 docx

## Tài liệu William Stallings Computer Organization and Architecture P5 pptx

## Tài liệu William Stallings Computer Organization and Architecture P6 pptx

## Tài liệu Encyclopedia of Multimedia Technology and Networking docx

## Tài liệu Copy of chuong 2- tôi pham hoc pdf

## Tài liệu Integration of Ordinary Differential Equations part 5 pdf

## Tài liệu Integration of Ordinary Differential Equations part 6 pdf

Tài liệu liên quan