0% found this document useful (0 votes)
4 views8 pages

231 ICS202 Assignment 3 V03

The document contains an assignment for a Data Structures and Algorithms course, focusing on recursion, trees, and binary heaps. It includes questions on recurrence relations, AVL tree construction, binary search tree deletions, ancestor methods, and heap sort algorithms. Each question is allocated specific points, indicating its importance in the overall assessment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

231 ICS202 Assignment 3 V03

The document contains an assignment for a Data Structures and Algorithms course, focusing on recursion, trees, and binary heaps. It includes questions on recurrence relations, AVL tree construction, binary search tree deletions, ancestor methods, and heap sort algorithms. Each question is allocated specific points, indicating its importance in the overall assessment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Osama Alkarnawo

ICS 202 – Data Structures and Algorithms


202183150 Fall Semester 2023/2024 (231)
Assignment #3

Question I. (30 points) (Recursion)


1. (10 points) For the method below, write the recurrence relation that represents the running time 𝑇(𝑛) of the
method in terms of the number of times statement S1 is executed. Do not expand the recurrence relation

us
Answer: TCM th
Ten
O nel

since the from to


loop runs o nd

the complexity 13 Test E


n th
Tcu
2. (20 points) Solve the following recurrence relation and express it in terms of the tightest Big O() notation:
𝑇(𝑛) = 2𝑇 + 𝑛, 𝑇(1) = 1

Answer:

Ten 2TCEItn
th
expand 2 2T 4 4
2n
22127C It

2312T Ya 4 3m

Ten TCH tkn


sub K

Tin 2102
I
2 TL

7tnl09z K
in

109am n
Nt 101N
Page 1 of 5
I O noon
Question II. (50 points) (Trees)
1. (20 points) Construct the AVL tree by inserting the following keys into an empty tree, in order.
31, 36, 42, 18, 15, 24, 38, 28,25
RRRontation IL Rot
36121 36 i
3112
36 Col
131221 42 o
42 o
18107
36 Ell
31107 42 o 18111 6115
I 3110
15101
42901

38 RL
R Rot
insert
insert 241
3612 3110
31107 31 l 1801

42107 1 24101138
1811 4801 l 1,801
G 15 1510 2410
3g 1510 24cal
36,22 36101
92C
31
y go 42117
I
24601 38

28 c nsort
inert 25 AL

31111 31
31111
1847 11
18117 38101 380
8
15,24cal 1 2
3
15,1 8 0
2410,36101
0
gza 420
926 2510

Page 2 of 5
2. (10 points) Consider the following BST:

OS

a. (5 points) Delete key 35 by Copying

replace 35

µ by the smallest
right subtree

b. (5 points) Delete key 46 by Merging ( from the original tree, i.e. Key 35 is there)

attach minloft node instead of 46

O
g
Iot ad
small's Page 3 of 5
Question III. (20 points) (Trees)

Write a method that prints all the proper ancestors of a given key in a given binary tree. For example if the key is
55, and the tree is the one given in QII.2, the method should print 60, 35, 46
You may use the method

String ancestors(T key){


String ancestors =””;

return this.ancestors(this.root,key, ancestors);


}

String ancestors(BSTNode<T> root ,T key, String result ){


// write your code here

Page 4 of 5
Question IV. (20 points) (Binary Heaps)
Sort the following array using heap sort algorithm. [120, 110, 310, 30, 50, 70, 90]

I showed tho 120


Stops of
the
sort NIO
woop 110 310
I I
30 70 go
50

maxhoan
building a
310
310,110 12030,50170190

110 120

30 go
to Igo
and replace root by last inserted node
root
potatodoverenso the the heap by y
1 and size

insert at the end of Array


2
the root
Deleted

110
1200
I I
30 70
So
90,110 12030,501701310
Submission Guidelines will be announced later.

Page 5 of 5
percolate up
now

120
I
110 96
I
3 70
50 120
111019030,501707310
repento steps

I
go

3 5011203107
u 70110,901

up
percolate
110
I
70 Do
I
30 150 1129310
110170,9013950
repent story
59
To
90,301119129310
3 50,70
percolate up

199
170 50
30
90,701501301119129310

repeat stops

30,50170199119120310
go

percolate up
70
70,50130190119120310

36
50
repanto 36001 Max heap
Already
50 no wood for percolate

70199119129310
50,30
36
re Ponte stop 1

30150,701901191203107
30
size 131 the
the hoop
Since in Ascending
will be sorted
array now order

final Ansi 30,50 70,901191203107

You might also like