Vous êtes sur la page 1sur 17

DATA STRUCTURE

PRESENTATION
GROUP MEMBERS

 Faisal Shahzad (38)


 Zeeshan Raza (41)
 Hiba Nawaz (13)
 M. Ahmad (19)
 Fatima (04)
HEIGHTED-BALANCED TREE

 A tree whose sub-trees


differ in height by no
more then one and sub-
trees are height-balanced.
An empty tree is heighted
balanced.
EXAMPLE:

 Consider a heighted balanced tree where following


conditions should be checked to determined if a
binary tree is balanced.
 An empty tree is height balanced . an empty binary
tree is balanced if :
1. Left sub-tree of T is balanced.
2. Right sub-tree of t is balanced.
3. The difference between hights of left sub-tree and
right sub-tree is not more then one.
AVL TREES.

 AVL tree checks the height of the left and the right
subtree and assures that the difference is not more
than 1. An AVL tree is a self-balancing binary search
tree. This was the first data structure that was
invented and is named after their inventors.

Balanced factor formula.


Balanced factor = height of left sub-tree – height of
right sub-tree
=0,-1,+1
BINARY SEARCH TREE

 Binary Search Tree is a node-based binary tree data structure which has
the following properties:

 Node’s Left Child Must Have Value less than its parent value
 Node’s Right Child Must have value greater than its parent value
 The left and right subtree each must also be a binary search tree.
ALGORITHM FOR SEARCH IN BINARY SEARCH TREE

 If root ==Null
Return Null
 If Data==root Data
Return Root Data
 If data < Root data
Return search(Root TO Left)
 If Data > Root Data
Return Search (root TO Right)
HEIGHTED BALANCE TREE/AVL TREE

 ALL Leaf node are at the same level


 Height of left subtree is not more then 1, than the height of
right sub tree
 Balance factor of left sub tree is 0,-1,+1 then tree is height
balanced tree.
INSERTION IN AVL TREE.

 Insertion in AVL tree is performed in the same way as it is performed in the binary search tree. The new node is
added into AVL tree as the leaf node. However it may lead to violation in the AVL tree property and therefor tree
may need balancing.The tree can be balanced by applying rotations. Rotations is required only if the balance factor
of any node is disturbed upon inserting the new node otherwise the rotation is not required.

 Example of insertion
Construct AVL tree by inserting the following data:
14,17,11,7,53,4,13,12,8,60,19,16,20.
DELETION IN AVL TREE.

 Deleting a node from an AVL tree is similar to that in a binary search tree deletion may disturb the balance factor
of an AVL tree and therefore tree needs to be rebalanced in order to maintain the AVLness. For this purpose we
need to perform rotations. If the node which is to be deleted is present in the left subtree of the critical node
then l rotation needs to be applied else if ,the node which is to be deleted is present in the right subtree of the
critical node ,the R ROTATION WILL BE APPLIED.
 Example of deletion
Construct AVL tree by deleting the following data :
8,7,11,14,17
AVL ROTATIONS

 To balanced it self, an AVL tree may perform the following four kids of rotations
 . left rotation
 .right rotations
 .left-right rotations
 .right-left rotation

 The first two rotations are single rotations and the next two rotations are double rotations. To have an
unbalanced tree, we least need a tree of height 2.
LEFT LEFT ROTATION
RIGHT RIGHT ROTATION
LEFT RIGHT ROTATION
RIGHT LEFT ROTATION
DISADVANTAGES OF HEIGHTED BALANCE TREE

 AVL trees have both advantages and disadvantages over other self balancing trees.
 AVL tree is slow because insertion of just one element makes it very slow.
 Deletion operations cost high in AVL trees as they involve a lot of pointer changes and rotations.
 The code of AVL tree is much more complex as compared to other BSTs.
 AVL tree is also a time consuming process and slow down the function as well.
 AVL tree would be a bad choice if you just need to time indexed any elements, let alone a balanced
tree to be used.
THANK YOU
ANY QUESTION?

Vous aimerez peut-être aussi