Lecture No. 24
___________________________________________________________________
Data Structures
Lecture No. 24
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 4
4.4
Summary
Delete on
this side
Fig 24.1
Page 1 of 11
Fig 24.2
The node 4 is the root node, nodes 2 and 6 are on level 1 and nodes 1, 3, 5, 7 are shown
on level 2. Now, if we delete the node 1, the balance of the node 2 is tilted towards right,
it is 1. The balance of the root node 4 is unchanged as there is no change in the number
of levels within right and left subtrees of it. Similarly, there is no change in the balances
of other nodes. So we dont need to perform any rotation operation in this case.
Lets see the second case.
Case 1b: the parent of the deleted node had a balance of 0 and the node was deleted in
the parents right subtree.
Page 2 of 11
In the Fig 24.4 above, the tree on the left contains the balance factor as 1, which means
that the left subtree of the parent node is one level more than the number of levels in the
right subtree of it. When we delete one node from the left subtree of the node, the height
of the left subtree is changed and the balance becomes 0 as shown in the right side tree of
Fig 24.4. But it is very important understand that this change of levels may cause the
change of balance of higher nodes in the tree i.e.
Change the balance of the parent node. May have caused imbalance in higher nodes so
continue up the tree.
So in order to ensure that the upper nodes are balanced, we calculate their balance factors
for all nodes in higher levels and rotate them when required.
Case 2b: The parent of the deleted node had a balance of -1 and the node was deleted in
the parents right subtree.
Similar to the Case 2a, we will do the following action:
Change the balance of the parent node. May have caused imbalance in higher nodes so
continue up the tree.
Page 3 of 11
Fig 24.5
As shown in the left tree in Fig 24.5, the node A is tilted towards right but the right
subtree of A (node B above) is balanced. The deleted node lies in the left subtree of the
node A. After deletion, the height of the left subtree is changed to h-1 as depicted in the
right tree of above figure. In this situation, we will do the following action:
Perform single rotation, adjust balance. No effect on balance of higher nodes so stop
here.
Single rotate
Fig 24.6
Node A has become the left subtree of node B and node 2 left subtree of node B has
become the right subtree of node A. The balance of node B is tiled towards left and
balance of node A is tilted towards right but somehow, both are within AVL limits. Hence,
after a single rotation, the balance of the tree is restored in this case.
Page 4 of 11
Fig 24.7
In the last case 3a, the right subtree of node A was balanced. But in this case, as shown in
the figure above, the node C is tilted towards left. The node to be deleted lies in the left
subtree of node A. After deleting the node the height of the left subtree of node A has
become h-1. The balance of the node A is shown tilted towards right by showing two
triangular knobs inside node A. So what is the action here.
Double rotation at B. May have affected the balance of higher nodes, so continue up the
tree.
double
rotate
Fig 24.8
Node A, which was the root node previously, has become the left child of the new root
node B. Node C, which was the right child of the root node C has now become the right
child of the new root node B.
Page 5 of 11
Fig 24.9
In the figure above, the right tree of the node B has a height of h-1 while the right subtree
is of height h. When we remove a node from the left subtree of node A, the new tree is
shown on the right side of Fig 24.9. The subtree 1 has height h-1 now, while subtrees 2
and 3 have the same heights. So the action we will do in this case is:
Single rotation at B. May have effected the balance of higher nodes, so continue up the
tree.
single
rotate
Fig 24.10
These were the five cases of deletion of a node from an AVL tree. Until now, we are
trying to understand the concept using the figures. You might have noticed the phrase
continue up the tree in the actions above. How will we do it? One way is to maintain the
pointer of the parent node inside each node. But often the easiest method when we go in
downward direction and then upward is recursion. In recursion, the work to be done later
is pushed on to the stack and we keep on moving forward until at a certain point we back
track and do remaining work present in the stack. We delete a node when we reach at the
desired location and then while traversing back, do the rotation operation on our way to
the root node.
Symmetrical to case 2b, we may also have cases 3b, 4b and 5b. This should not be a
problem in doing it yourself.
Page 6 of 11
*
b
+
c
Fig 24.11
g
f
Page 7 of 11
<assign>
<id>
A
:=
<expr>
<expr>
<term>
<term>
<factor>
<id>
Expression grammar
<assign>
<id> := <expr>
<id>
A| B | C
<expr>
<expr> + <term> | <term>
<term>
<term> * <factor> | <factor>
<factor>
( <expr> ) | <id>
<term>
<factor>
<factor>
<id>
<id>
B
Fig 24.12
Page 9 of 11
SELECT
<SelList>
<Attribute>
<FromList>
FROM
<RelName>
<Condition>
<FromList>
AND
<RelName>
StarsIn
title
WHERE
MovieStar
Condition
Condition
<Attribute>
<Attribute>
<Attribute>
LIKE
<Pattern>
Fig 24.13
setName
name
%1960
birthdate
The root node is Query. This node is further divided into SELECT, <SelList>, FROM,
<FromList>, WHERE and <Condition> subnodes. <SelList> will be an Attribute and
finally a title is reached. Observe the tree figure above, how the tree is expanded when we
go in the downward direction. When the database engine does the query process, it makes
these trees. The database engine also performs query optimization using these trees.
Compiler Optmization
Lets see another expression tree here:
Common subexpression:
(f+d*e) + ((d*e+f)*g)
*
d
+
e
*
d
g
f
Fig 24.14
Page 10 of 11
Graph!
Fig 24.15
This figure is not a tree now because it has two or more different paths to reach a node.
Therefore, this has become a graph. The new connection is containing a directed edge,
which is there in graphs.
Optimizer uses the expressions trees and converts them to graphs for efficiency purposes.
You read out from your book, how the expression trees are formed, what are the different
methods of creating them.
Page 11 of 11