Title: UNIVERSITY OF COLOMBO
1 UNIVERSITY OF COLOMBO SCHOOL OF
COMPUTING
IT 2201 Data Structures and Algorithms
DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY
2Question No12
- Consider the following tree and the four (iv)
statements which follow
3- The above tree is a (i) tree.
- Node Hs parent node is (ii) .
- Node Fs sibling(s) is/are (iii) .
- Node Es proper ancestor(s) is/are (iv) .
- Which of the following are correct answers for
the blank positions?
4- (a) (i) AVL (ii) E (iii) F,G,H,I (iv)
H,I,J,K - (b) (i) Binary (ii) E (iii) G (iv)
H,I,J,K - (c) (i) Binary (ii) E (iii) G (iv) C,
A - (d) (i) AVL (ii) E (iii) G (iv) C,
A - (e) (i) Binary (ii) E (iii) G,H,I (iv) C,A
Correct Answer C
5Question No 19
- Consider the following expression tree
- What is the equivalent expression for the above
tree?
6- What is the equivalent expression for the above
tree? - (a) abcd-
- (b) -abcd
- (c)abcd-
- (d) (ab)(cd-)
- (e) (ab)(c-d)
7Question No 17
- 22,11,18,52,16,27,51,7 is a set of 8 integers.
- If the binary search tree is created using the
above set of integers, what will the resulting
tree be?
8Binary Search Trees
- The binary search tree is a binary tree that
satisfies the search order property. - Search order property
- All elements in the left sub tree of a node n are
less than the contents of n and - all elements in the right sub-tree of node n are
grater than the contents of n. - Note Does not allow duplicates.
9Examples
7
7
9
2
2
5
1
5
1
8
3
3
Two binary trees( only the left tree is a search
tree)
10Applications One way that a binary search tree
can be used to sort data as follows.
- Eg we have list of integers, such as 5,2,8,4,
and 1 that we wish to sort in to ascending order. - This algorithm consists of two stages.
- - Insertion
- -Traversal
11Inserting- inserting integers into a binary
search tree.
- Step 1 If the current pointer is 0. Create a
new node, store the data , and return the address
of the new node. - Step 2 otherwise compare the integer to the
data stored at the current, if the new node is
less than the integer at the current node, insert
the new integer into the left child of the
current node( by recursively applying the same
algorithm), otherwise, insert it into the right
child of the current node.
12Eg 5,2,8,4 and 1
5
2
8
5
5
8
2
2
4
5
5
8
8
2
2
1
1
4
4
13Deleting nodes from binary search tree.
- There are three categories of nodes we may wish
to delete. - Category 1 The node to be deleted has no sons.
- - The node can be deleted without any adjustment
to the tree. - - Delete the leaf and set the pointer from its
parent (if any) to zero.
14Example Category 1
8
11
Delete node with key 1
3
14
9
8
5
15
12
10
6
11
3
14
13
7
9
1
5
15
12
10
6
13
7
15Category 2 The node to be deleted has only one
sub-tree.
- Solution Its only son can be moved up to its
take place. - We just redirect the references from the nodes
parent so that it points to the child.
16Example Category 2
8
11
3
Delete node with key 5
14
9
6
15
1
8
12
10
7
11
3
13
14
9
1
5
15
12
10
6
13
7
17Category 3 The node to be deleted has two
sub-trees.
- Solution this method we shall use is to replace
the node being deleted by the rightmost node in
its left sub-tree - Or Leftmost mode in its right-sub-tree.
18Example Category 3
8
10
3
Delete node with key 11
14
9
15
6
1
8
12
7
11
3
13
14
9
1
5
15
12
10
6
13
7
19Example Category 3
8
12
3
14
Delete node with key 11
9
6
15
1
13
8
10
7
11
3
14
9
1
5
15
12
10
6
13
7
20Question No 17
- 22,11,18,52,16,27,51,7 is a set of 8 integers.
- If the binary search tree is created using the
above set of integers, what will the resulting
tree be?
2122,11,18,52,16,27,51,7
52
11
18
22
22
22
22
11
52
11
11
18
18
2222,11,18,52,16,27,51,7
22
27
22
22
16
11
52
11
52
11
52
18
18
27
18
16
16
2322,11,18,52,16,27,51,7
22
22
51
11
52
11
52
18
27
18
27
16
16
51
2422,11,18,52,16,27,51,7
22
22
7
11
52
11
52
18
27
7
18
27
16
51
16
51
25Correct Answer (b)
22
11
52
7
18
27
16
51
26Question No16
Consider the following tree and the statements
that follow.
27Note h height of the subtree, where ? hgt0,
In the above tree, AVL property is violated at
node(s) (i) . The balanced factor of that
node is (ii) . To restore the AVL property,
(iii) rotation is needed. Which of the
following are the correct terms for the blank
positions. (a) (i) D (ii) 3 (iii) double (b) (i)
A (ii) 2 (iii) single (c) (i) C ii) 2 (iii)
single (d) (i) A (ii) 2 (iii) double (e) (i)
D (ii) 2 (iii) single
28AVL trees(Adelson Velskii and Landis)
- Definition
- An AVL tree is a binary search tree with the
additional balance property that, for any node in
the tree, the height of the left and right
sub-trees can be defer by at most 1. - The height of the empty sub-tree is 1
- Balance factor Height of the left sub-tree
height of the right sub-tree - -1lt balance factor lt1
29AVL Trees
5
Bf-1
5
Bf1
Bf0
10
3
2
15
9
Bf0
1
13
8
30Non-AVL trees
5
-1
6
1
2
0
10
3
3
1
1
1
0
15
1
9
2
0
13
4
0
0
31Algorithm Inserting new nodes into an AVL trees
- Insert the node in the same way as in an ordinary
binary search tree. - Beginning with the new node, trace a path back
towards the root, checking the difference in
height of the sub-trees at each node along the
way. - If you find a node with an imbalance ( a height
difference other than 0,1,-1), stop your trace
at this point.
32Algorithm Inserting new nodes into an AVL trees
- 4 Consider the node with the imbalance and two
nodes on the layers immediately below this point
on the path back to the new node. - If these three nodes lie in a straight line,
apply a single rotation to correct the imbalance. - If these three nodes lie in a dog-leg pattern,
apply a double rotation to correct the imbalance.
33Construction of an AVL trees.
- Eg. 10,20,30,25,27,7,4,23,26,21
- We begin by inserting integer 10 into the root
- This node satisfies the AVL condition.
10
34Now we add another node 20
1
0
Now we add third node, 30
The root node 10 has a deference of 2, which
violates the AVL conditions.
2
1
0
30
35Therefore, we must re-arrange the node to restore
the balance in the tree.
We perform an operation known as the rotation
when the tree goes out of balance. There are two
types of rotation used in AVL trees. Single
rotation and Double rotation. The rules for
deciding which type of rotations to be used.
36- When you have found the first node is out of
balance, restrict your attention to that node and
the two nodes in the two layers immediately below
it. - If these three nodes lie in a straight line,
single rotation is needed to restore the
balance. - If these three nodes lie in a dog-leg pattern
You need double rotation to restore the balance.
37In our example. Node 10 is imbalance , consider
two layers immediately below this node. These
nodes lie in a straight line. So we need a single
rotation to restore the balance
38Single Rotation
- This involves shifting the middle node up to
replace the top node down to become the left
child of the middle.
2
20
1
0
30
10
30
3920
30
10
We continue by adding two more nodes 25 27
20
20
30
10
-2
30
10
25
27
4020
The first imbalance is detected at node 30 these
three nodes form a dog-leg pattern.
30
10
-2
25
27
We require a double rotation to correct the
balance.
Double rotation consists of two single rotations.
These two rotations are in opposite directions.
411St Rotation we rotate the node 27 up to 25, and
25 down to become the left child of 27.
20
30
10
20
27
30
10
-2
25
25
27
422nd Rotation Node 27 rotates up to replace 30
and node 30 rotates down to become the left child
of 27
20
20
30
27
10
10
27
25
30
25
43We continue by adding the nodes 7 and 4 to the
tree.
20
27
10
20
25
30
7
27
10
4
25
30
44The first imbalance is detected at node 10, the
10,7 and 4 lie in a straight line, so a single
rotation is needed.
20
27
7
25
30
4
10
20
27
10
25
30
7
4
45We add the nodes 23,26,21 to the tree.
20
27
7
30
25
4
10
20
27
23
26
7
30
25
21
4
10
27 is the first node where an imbalance occurs
27,25,23 in a straight line
46The middle node (25) rotate up to replace node
27, and node 27 rotates down to become the right
child of 25.
20
25
7
20
27
23
4
10
27
7
30
25
21
26
30
4
10
What happens to 26 ,which is old child of node
25? It must swap over to become the new left
child of 27
23
26
21
47Single Rotation
A
B
C
h
h
h
D
E
Suppose we want to insert a node into the
sub-tree under node E
48The tree is imbalance at node A
A
The two nodes immediately below node A. Nodes
A,C,E are in the straight line. So we need a
single rotation.
B
C
h
We rotate C up to replace A and A down become the
left child of C. Node D must swap over to become
the new child of A
h1
h
D
E
49C
h1
A
B
E
h
h
This tree is balanced.
D
50Double Rotation
This tree is an AVL tree, Assuming all sub-trees
in the rectangular boxes are AVL trees. The
height differences are 0 at node D C and 1 at
Node A.
A
C
B
h
E
h
D
F
h-1
h-1
G
For Example, we want to insert new node into
sub-tree G (or F)
51A
For Example, we want to insert new node into
sub-tree G (or F)
B
C
h
h
D
E
A
B
C
h-1
h-1
F
G
h
h
E
D
The tree is now unbalance (2 at Node A).
Therefore, node A is the first Node where an
imbalance is detected. Node A,C,D form a Dog-leg
pattern. Therefore, a double rotation is needed.
h-1
h
F
G
52The first rotation involves node C and Node D
Node D rotates up to replace C and C rotates down
to become new right child of D. Sub-tree G swaps
over to become the left child of node C.
A
B
C
h
h
E
1st rotation
D
A
B
D
h-1
h
F
h
G
C
h-1
This first rotation does not solve the imbalance
problem since the height differences are still 2
at both nodes A and D.
F
h
h
G
E
53A
2nd Rotation
B
D
h
C
h-1
E
F
h
h
G
Therefore, we perform the 2nd rotation, which is
in the opposite direction to the first one, and
which involves nodes A,D and C
54D
A
C
A
B
D
h
h
h
h-1
h
C
h-1
E
B
F
G
E
F
h
h
The final tree is now balanced.
G
Node D rotates up to replace A. Node A rotates
down to become the left child of D, and Sub-tree
F swaps over to become the new right child of
node A.
55Question No16
Consider the following tree and the statements
that follow.
(h-1)-(h3)
56Note h height of the subtree, where ? hgt0,
In the above tree, AVL property is violated at
node(s) (i) . The balanced factor of that
node is (ii) . To restore the AVL property,
(iii) rotation is needed. Which of the
following are the correct terms for the blank
positions. (a) (i) D (ii) 3 (iii) double (b) (i)
A (ii) 2 (iii) single (c) (i) C ii) 2 (iii)
single (d) (i) A (ii) 2 (iii) double (e) (i)
D (ii) 2 (iii) single
57 UNIVERSITY OF COLOMBO SCHOOL OF
COMPUTING
IT 2201 Data Structures and Algorithms
DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY