Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Kingdom of Saudi Arabia ‫المملكة العـربية السعودية‬

Ministry of Higher Education ‫وزارة التعليم العالي‬


Prince Sattam bin Abdulaziz University ‫جامعة األمير سطام بن عبدالعزيز‬
College of Computer Engineering and Sciences ‫كلية هندسة وعلوم الحاسب‬
Computer Science Department ‫قسم علوم الحاسب‬
Course Title: Data Structures and Algorithms (CS2321) / 1443 (1)

Homework 4 (Solutions)

(1) (a) A.

(b) G, H, I, L, M, and K.

(2) For node B:

(a) A.

(b) D and E.

(c) C.

(d) 1.

(e) 3.

(3) 4.

(4) Proof is by induction. The theorem is trivially true for h = 0. Assume true for h = 1,2, . . . ,k. A tree of height k + 1 can

have two subtrees of height at most k. These can have at most 2k + 11 nodes each by the induction hypothesis. These

2k + 22 nodes plus the root prove the theorem for height k + 1 and hence for all heights.

(5) (a) - * * a b + c d e.

(b) ( ( a * b ) * ( c + d ) ) - e.

(c) a b * c d + * e -.

(6)

CS2321 – Homework 4 Page 1/15


(7)

(8)

Algorithm find(x,*t)
Input: x is the value to find, t a reference on the root of a subtree.
Output: the reference of the node containing x or NULL if x doesn’t exist in the tree.
1. if (t==NULL)
2. return NULL;
3. else if (x < t->key)
4. return find(x,left(t));
5. else if (x > t->key)
6. return find(x,right(t));
7. else
8. return t;

CS2321 – Homework 4 Page 2/15


(9)

(10)

CS2321 – Homework 4 Page 3/15


(11)

Type of tree Characteristics Example of 8 nodes

1) Each node has at most


2 children

Binary tree 2) Not balanced


3) ⌊log 𝑛⌋ ≤ 𝑑𝑒𝑝𝑡ℎ ≤
𝑛−1

1) Is a Binary Tree
2) For every node X, all
the keys in its left subtree
are smaller than the key
value in X
3) For every node X, all
the keys in its right
Binary Search tree
subtree are larger than
the key value in X

CS2321 – Homework 4 Page 4/15


1) Is a Binary Search Tree
2) For every node in the
tree, the height of the
left and right sub-trees
differ by at most 1
3) ) ⌊log 𝑛⌋ ≤ 𝑑𝑒𝑝𝑡ℎ ≤
⌈log 𝑛⌉
AVL tree

(12)
Let the following AVL tree named T.
11

7 17

5 9 14 20

2 6 10 13 18

a) Draw the obtained trees T1 and T2 (respectively) after inserting in T a new node with key=19 and
after performing the required rotation. Indicate the type of the used rotation.

CS2321 – Homework 4 Page 5/15


T1: After the insertion of a new node with key=19 in T T2 : After left-right double rotation on T1

b) Draw the obtained trees T3 and T4 (respectively) after inserting in T a new node with key=12 and
after performing the required rotation. Indicate the type of the used rotation.
T3: After the insertion of a new node with key=12 in T T4 : After left-left single rotation on T’1

(13*) Fill in the blanks in the following statements (3 marks)


Statements
a. A finding algorithm, applied on a BT of N nodes and H levels, has a running time equal to ……O(N)…….
b. A min value finding algorithm, applied on a BST of N nodes and H levels, has a running time equal to
……O(H)…….
c. A max value finding algorithm, applied on a AVLT of N nodes, has a running time equal to ……O(log(N))…….
d. An inorder traversal algorithm, applied on a BT of N nodes and H levels, has a running time equal to ……
O(N)…….
e. An insertion algorithm, applied on a AVLT of N nodes, has a running time equal to …… O(log(N))…….
f. An postorder traversal algorithm, applied on a BST of N nodes and H levels, has a running time equal to …
O(N)……….

(14*) Let the following binary search tree composed by a set of 10 nodes and referred by a pointer T to
its root. Each node Ai is defined by four attributes as:
Ai={int x, own_address, left_node_address, right_node_address}

CS2321 – Homework 4 Page 6/15


Note that T is a pointer that refer the root and is defined by the address of the referred node
T={root_address}.
A0={5, @1000, @NULL, @5000}
A1={13, @5000, @NULL, @NULL}
A2={15, @3500, @1000, @6700}
A3={24, @6700, @NULL, @NULL}
A4={30, @1200, @3500, @9900}
A5={42, @9400, @NULL, @3100}
A6={44, @6200, @NULL, @NULL}
A7={49, @3100, @6200, @NULL}
A8={50, @9900, @9400, @4600}
A9={59, @4600, @NULL, @NULL}
T={@1200}
a. Determine the root of the binary search tree T. (2 marks)
A4
b. Determine the leaves of the binary search tree T. (2 marks)
A1, A3, A6, A9
c. Determine the nodes, of the binary search tree T, that have two children. (2 marks)
A2, A4, A8
d. Determine the nodes, of the binary search tree T, that have only one left child. (2 marks)
A7
e. Determine the nodes, of the binary search tree T, that have only one right child. (2 marks)
A0, A5
f. Draw the binary search tree T. (3 marks)

Draw T

CS2321 – Homework 4 Page 7/15


g. Transform the binary search tree T to an AVL tree named T’. Explain the made changes. (4 marks)
A0={5, @1000, @NULL, @5000}
Draw T’
A1={13, @5000, @NULL, @NULL}
A2={15, @3500, @1000, @6700}
A3={24, @6700, @NULL, @NULL}
A4={30, @1200, @3500, @9900}
A5={42, @9400, @NULL, @NULL}
A6={44, @6200, @9400, @3200}
A7={49, @3100, @NULL, @NULL}
A8={50, @9900, @6200, @4600}
A9={59, @4600, @NULL, @NULL}
T={@1200}

Explain:_______________________________________________________________________

_____________________________________________________________________________

(15) Let the following binary search tree composed by a set of 13 nodes and referred by a pointer T to
its root. Each node Ai is defined by four attributes as:
Ai={int x, own_address, left_node_address, right_node_address}
Note that T is a pointer that refer the root and is defined by the address of the referred node
T={root_address}.
A0={80, @1000, @3500, @6200}
A1={50, @5000, @NULL, @NULL}
A2={70, @3500, @5000, @NULL}
A3={200, @6700, @1300, @1200}
A4={220, @1200, @NULL, @NULL}
A5={90, @9400, @NULL, @NULL}
A6={100, @6200, @9400, @9900}
A7={140, @3100, @NULL, @NULL}
A8={120, @9900, @NULL, @NULL}
A9={180, @4600, @NULL, @NULL}
A10={130, @7300, @1000, @6700}
A11={170, @8100, @NULL, @4600}
A12={150, @1300, @3100, @8100}
T={@7300}

h. Draw the binary search tree T. Display T using in-order traversal.

CS2321 – Homework 4 Page 8/15


Draw T Display of T using in-order traversal

50-70-80-90-100-120-130-140-150-170-180-200-220

i. Transform the binary search tree T to an AVL tree named T’. What is the type of
transformation you made? Determine and highlight, in the previous drawing, the nodes that
change positions.

Draw T’ A0={80, @1000, @3500, @6200{


A1={50, @5000, @NULL, @NULL{
A2={70, @3500, @5000, @NULL{
A3={200, @6700, @4600, @1200{
A4={220, @1200, @NULL, @NULL{
A5={90, @9400, @NULL, @NULL{
A6={100, @6200, @9400, @9900{
A7={140, @3100, @NULL, @NULL{
A8={120, @9900, @NULL, @NULL{
A9={180, @4600, @NULL, @NULL{
A10={130, @7300, @1000, @8100{
A11={170, @8100, @1300, @6700{
A12={150, @1300, @3100,@NULL}
T={@7300}
Transformation made is : Double rotation between X, Y and Z (A3, A12 and A11)
Nodes that change positions are : A3, A10, A11 and A12

(16)
Part 1:
a. Consider the following AVL tree named T, insert 5, 6 and 25 and perform any necessary rotations (3
marks).

T1: After inserting 5 on T T2: After ...left-left-single.... rotation on T1

CS2321 – Homework 4 Page 9/15


T3: After inserting 6 in T2 T4: After ....right-left-double........ rotation on T3

T5: After inserting 25 in T4 T6: After .............no............ rotation on T5

b. Give the pre-order and post-order traversal of your Final AVL tree (T6) that you have created in
part (a) (2 marks).

Pre-order traversal: 5, 4, 3, 8, 6, 10, 25____________________________________


Post-order traversal: 3, 4, 6, 25, 10, 8, 5 ____________________________________

Part 2:

Consider the following tree

CS2321 – Homework 4 Page 10/15


Select (Yes/No) whether or not the tree above might represent each of the following data structures,
and give the reason of your choice? (3 marks)

Data Structures Yes No Reason

Binary Tree V each node has maximum two children

each node has maximum two children also all nodes on


Binary Search Tree V
the left(x)<x ,and nodes on right(x)>x

AVL Tree V the tree is not balanced at node 29

(17)

We consider le following functions (methods) to manage operations on Binary Search Trees and AVL-
trees

Binary Search Tree


createTree(BST) Create an empty binary search tree named BST
insert(BST, X) Add element X to the binary search tree BST
delete (BST, X) Remove element X from the binary search tree BST
AVL Tree
createAVLTree(AVL); Create an empty AVL-tree named AVL
insert(AVL, X) Add element X to AVL
delete(AVL, X) Remove element X from AVL

a. Draw the corresponding resulting Binary Search Tree after running each of these
instructions: (1pt for each)

CS2321 – Homework 4 Page 11/15


Code Graphical tree

Tree BST;
createTree(BST);
insert(BST,8);
insert(BST,5);
insert(BST,1);
insert(BST,6);
insert(BST,3);
insert(BST,11);
insert(BST,9);
insert(BST,10);
insert(BST,13);

OR

delete(BST,8);

b. Draw the corresponding resulting AVL-Tree after running each of these instructions (code
fragments) and select the correct operation with indicating the corresponding node if
applicable: (0.5pt for each)

Code fragments Graphical tree Operations


□Single rotation at node ___
⃝ left – left
AVLTree AVL; ⃝ right - right
createAVLTree(AVL);
insert(AVL,15); □Double rotation at node ___
insert(AVL,20); ⃝ left – right
⃝ right - left
□No rotation
□Single rotation at node _15__
⃝ left – left
insert(AVL,24); ⃝ right - right
□Double rotation at node ___
⃝ left – right

CS2321 – Homework 4 Page 12/15


⃝ right - left
□No rotation
□Single rotation at node ___
⃝ left – left
⃝ right - right
insert(AVL,10); □Double rotation at node ___
⃝ left – right
⃝ right - left
□No rotation
□Single rotation at node ___
⃝ left – left
⃝ right - right
insert(AVL,13); □Double rotation at node __15_
⃝ left – right
⃝ right - left
□No rotation
(18)
a) Consider the following tree

Select (Yes/No) whether or not the tree above might represent each of the following data
structures, and give the reason of your choice?

Data Structures Yes No Reason

Binary Search Tree ✓ It's not a BT so it can't be a BST. Node 7 has 3 children

AVL Tree ✓ It's not a BST so it can't be a AVLT.

b) Consider the following tree T:

CS2321 – Homework 4 Page 13/15


Display T1 using postorder traversal.

H-D-I-E-B-F-J-G-C-A

c) We consider le following functions (methods) to manage operations on Binary Search Trees.

Binary Search Tree


createTree(BST) Create an empty binary search tree named BST
insert(BST, X) Add element X to the binary search tree BST
delete (BST, X) Remove element X from the binary search tree BST

 Draw the corresponding resulting Binary Search Tree after running each of these instructions:

Code Graphical tree

Tree BST;
createTree(BST);
insert(BST,8);
insert(BST,2);
insert(BST,13);
insert(BST,7);
insert(BST,9);
insert(BST,4);
insert(BST,11);
insert(BST,5);
insert(BST,10);

delete(BST,8);

CS2321 – Homework 4 Page 14/15


OR

CS2321 – Homework 4 Page 15/15

You might also like