Professional Documents
Culture Documents
New Home Work 04 Key
New Home Work 04 Key
Homework 4 (Solutions)
(1) (a) A.
(b) G, H, I, L, M, and K.
(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 + 11 nodes each by the induction hypothesis. These
2k + 22 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)
(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;
(10)
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
(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.
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
(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}
Draw T
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}
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.
(16)
Part 1:
a. Consider the following AVL tree named T, insert 5, 6 and 25 and perform any necessary rotations (3
marks).
b. Give the pre-order and post-order traversal of your Final AVL tree (T6) that you have created in
part (a) (2 marks).
Part 2:
(17)
We consider le following functions (methods) to manage operations on Binary Search Trees and AVL-
trees
a. Draw the corresponding resulting Binary Search Tree after running each of these
instructions: (1pt for each)
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)
Select (Yes/No) whether or not the tree above might represent each of the following data
structures, and give the reason of your choice?
Binary Search Tree ✓ It's not a BT so it can't be a BST. Node 7 has 3 children
H-D-I-E-B-F-J-G-C-A
Draw the corresponding resulting Binary Search Tree after running each of these instructions:
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);