UNIVERSITY OF COLOMBO - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

UNIVERSITY OF COLOMBO

Description:

Two binary trees( only the left tree is a search tree) Applications : One way that a binary search tree can be used to sort data as follows. ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 58
Provided by: noelfe9
Category:

less

Transcript and Presenter's Notes

Title: UNIVERSITY OF COLOMBO


1
UNIVERSITY OF COLOMBO SCHOOL OF
COMPUTING
IT 2201 Data Structures and Algorithms
DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY
2
Question 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
5
Question 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)

7
Question 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?

8
Binary 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.

9
Examples
7
7
9
2
2
5
1
5
1
8
3
3
Two binary trees( only the left tree is a search
tree)
10
Applications 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

11
Inserting- 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.

12
Eg 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
13
Deleting 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.

14
Example 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
15
Category 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.

16
Example 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
17
Category 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.

18
Example 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
19
Example 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
20
Question 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?

21
22,11,18,52,16,27,51,7
52
11
18
22
22
22
22
11
52
11
11
18
18
22
22,11,18,52,16,27,51,7
22
27
22
22
16
11
52
11
52
11
52
18
18
27
18
16
16
23
22,11,18,52,16,27,51,7
22
22
51
11
52
11
52
18
27
18
27
16
16
51
24
22,11,18,52,16,27,51,7
22
22
7
11
52
11
52
18
27
7
18
27
16
51
16
51
25
Correct Answer (b)
22
11
52
7
18
27
16
51
26
Question No16
Consider the following tree and the statements
that follow.
27
Note 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
28
AVL 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

29
AVL Trees
5
Bf-1
5
Bf1
Bf0
10
3
2
15
9
Bf0
1
13
8
30
Non-AVL trees
5
-1
6
1
2
0
10
3
3
1
1
1
0
15
1
9
2
0
13
4
0
0
31
Algorithm 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.

32
Algorithm 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.

33
Construction 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
34
Now 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
35
Therefore, 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.

37
In 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
38
Single 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
39
20
30
10
We continue by adding two more nodes 25 27
20
20
30
10
-2
30
10
25
27
40
20
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.
41
1St 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
42
2nd 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
43
We continue by adding the nodes 7 and 4 to the
tree.
20
27
10
20
25
30
7
27
10
4
25
30
44
The 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
45
We 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
46
The 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
47
Single Rotation
A
B
C
h
h
h
D
E
Suppose we want to insert a node into the
sub-tree under node E
48
The 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
49
C
h1
A
B
E
h
h
This tree is balanced.
D
50
Double 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)
51
A
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
52
The 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
53
A
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
54
D
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.
55
Question No16
Consider the following tree and the statements
that follow.
(h-1)-(h3)
56
Note 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
Write a Comment
User Comments (0)
About PowerShow.com