Mod 2 (Aad)
Mod 2 (Aad)
AVL TREES
Height balanced Trees
• Height balance tree (self balancing tree) is a binary tree which
automatically maintain height of tree, and its sub tree on each insertion
and deletion of node.
• AVL tree, red-black tree are example of height balanced tree.
• AVL tree uses balance factor to balance the height of tree.
• Balance factor is nothing but difference between height of left subtree and
right subtree.
AVL TREES: Adelson, Velski & Landis
Definition:
• An empty binary tree B is an AVL tree
• If B is non empty with BL and BR its right and left sub trees then B is
AVL tree if
i. BL and BR are AVL trees
ii. |hL-hR|<= 1 where hL and hR are heights of Left and Right
sub-tree respectively
To implement AVL tree, each node must contain a balance factor.
Balance factor bf = hL – hR
Bf of AVL trees can only have values: -1,0,1
Rotations in AVL tress
When we insert a node into AVL tree, it might make the tree unbalanced. To
balance we have to first find the pivot node after calculating balance factor(bf).
The node whose absolute value of bf switches from 1 to 2 is marked as pivot. If
there are more than one such node, choose the node nearest to the newely
inserted node as pivot.
There are basically four types of rotations which are as follows:
L L rotation: Inserted node is in the left subtree of left child of pivot
R R rotation : Inserted node is in the right subtree of right child of pivot
L R rotation : Inserted node is in the right subtree of left child of pivot
R L rotation : Inserted node is in the left subtree of right child of pivot
L L rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree of the
left child of pivot. The tree then needs a right rotation.
RR rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of
the right subtree, then we perform a single left rotation .
LR Rotation
If the tree become unbalanced by an inserted node in the right subtree of left
child of pivot A left-right rotation is a combination of left rotation followed by
right rotation.
RL Rotation
If the tree become unbalanced by an inserted node in the left subtree of right
child of pivot A right-left rotation is a combination of right rotation followed
by left rotation.
AVL TREE INSERTION
Step 1: First, insert a new element into the tree using BST's (Binary
Search Tree) insertion logic.
Step 2: After inserting the elements you have to check the Balance Factor
of each node.
Step 3: When the Balance Factor of every node will be found like 0 or 1 or -
1 then the algorithm will proceed for the next operation.
Step 4: When the balance factor of any node comes other than the above
three values then the tree is said to be imbalanced. Then perform the
suitable Rotation to make it balanced and then the algorithm will proceed
for the next operation.
AVL TREE DELETION
Step 1: Firstly, find that node where k is stored
Step 2: Secondly delete those contents of the node (Suppose the node is x)
Step 3: Claim: Deleting a node in an AVL tree can be reduced by deleting a
leaf. There are three possible cases:
EXAMPLE:
Applications using Disjoint sets:
1. Represents network connectivity.
2. Image Processing.
3. In game algorithms.
4. Kruskal’s minimum spanning tree.
5. Find cycle in undirected graphs.
Both trees have the same rank – the resulting set’s rank is one larger
Both trees have the different ranks – the resulting set’s rank is the larger of
the two. Ranks are used instead of height or depth because path compression
will change the trees’ heights over time.
Worst case complexity: O(LogN)
Path compression:
Path compression` is a way of flattening the structure of the tree
whenever Find is used on it. Since each element visited on the way to a
root is part of the same set, all of these visited elements can be
reattached directly to the root. The resulting tree is much flatter,
speeding up future operations not only on these elements, but also on
those referencing them.
GRAPHS
Representations of Graphs
We can choose between two standard ways to represent a graph G = (V;E)
as a collection of adjacency lists or as an adjacency matrix.
The adjacency-list representation of a graph G= (V, E) consists of an array
Adj of |V| lists, one for each vertex in V . For each u ε V , the adjacency
list
Adj[u] contains all the vertices v such that there is an edge .(u,v) ε E. That
is,
Adj[u] consists of all the vertices adjacent to u in G.
3. Forward edge: from ancestor to descendent. Not a tree edge, though. From
grey node to black node. Forward edges are those nontree edges (u,v)
connecting a vertex u to a descendant v in a depth-first tree. Alternative path
edges included in forward edges.
4. Cross edge: between a tree or subtrees. From a grey node to a black node.
Cross edges are all other edges. They can go between vertices in the same
depth-first tree, as long as one vertex is not an ancestor of the other, or they
can go between vertices in different depth-first trees. Edges that move to a
blackened vertex are cross edges.
EXAMPLE:
TOPOLOGICAL SORTING
• Given a digraph G = (V, E), find a linear ordering of its vertices such that: for
any edge (v, w) in E, v precedes w in the ordering
• A directed graph with a cycle cannot be topologically sorted.
ALGORITHM -1
1. Identify vertices that have no incoming edges. Select one such vertex
2. Delete this vertex of in-degree 0 and all its outgoing edges from the graph. Place it
in the output.