Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Avl Tree

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Link : https://byjus.

com/gate/avl-trees-notes/

Fibonaci Heap:

Fibonacci Heap is a collection of trees with min-heap or max-heap property. In


Fibonacci Heap, trees can have any shape even all trees can be single nodes

Drawback of AVL

Disadvantages of AVL Trees


In addition, AVL trees have high constant factors for some operations. For example,
restructuring is an expensive operation, and an AVL tree may have to re-balance itself
log 2 n \log_2 n log2n in the worst case during a removal of a node.

VL Tree is an essential topic under one of the most important chapters of Computer Science i.e.
Data Structure. And, when it comes to a competitive examination like GATE, you have to dive deep
into this topic to understand it thoroughly. In this article, we have comprised all the pointers related to
the AVL Tree. We hope these notes for CSE topics will help you understand this topic in a better
way.
The term AVL tree was introduced by Adelson-Velsky and Landis. It is a balanced binary search tree
and is the first data structure like this. In the AVL tree, the heights of the subtree cannot be more
than one for all nodes.
The above picture is of an AVL tree. Here, it is clearly visible that the heights of the left and right
subtrees are equal to, or less than one.
The above tree is not an AVL tree. And, the reason is simple. Here, the heights of the left and right
subtrees are higher than 1.

Balance Factor in AVL Tree


In the AVL tree, the term balance factor is very important.
Balance Factor = height(left-subtree) – height(right – subtree)
The balanced factor should be -1, 0 or +1. Otherwise, the tree will be considered an unbalanced
tree.
Example Of AVL Tree & Balance Factor:

In the above example, the height of the left subtree is 3 and the height of the right subtree is 2. That
means the balance factor is <=1 therefore the tree is supposed to be balanced.

Why Do We Need an AVL Tree?


To reduce the issue of time complexity in a binary search tree, the AVL tree was introduced by
Adelson-Velski & Landis. It is a self-balancing tree that helps in reducing the complexity issue.

Operations on AVL Tree


The AVL tree is a balancing binary tree, and therefore it follows the same operations we perform in
the binary search tree.

1. Insertion
2. Deletion
Insertion: The process of insertion is the same as it is executed in the binary search tree. However,
there are chances that it may point to a violation in the AVL tree property, and the tree may require
balancing. To balance a tree we can apply rotations.
Deletion: The process of deletion is the same as it is executed in a binary search tree. It can affect
the balance factor of the tree, therefore, we need to utilize different types of rotations to balance the
tree.

AVL Rotation
 Left rotation
 Right rotation
 Left-Right rotation
 Right-Left rotation

1.
1. Left Rotation: When we perform insertion at the left subtree, then it is a left rotation.
1.
1. Right Rotation: When we perform insertion at the right subtree, then it is a right
rotation.
1.
1. Left-Right Rotation

1. Right-Left Rotation
Figure: Right-Left Rotation

Time Complexity in AVL Tree


Algorithm Average case Worst case

Space o(n) o(n)

Search o(log n) o(log n)

Insert o(log n) o(log n)

Delete o(log n) o(log n)


Heap is a special case of balanced binary tree data structure where the root-node key is compared
with its children and arranged accordingly. If α has child node β then −
key(α) ≥ key(β)
As the value of parent is greater than that of child, this property generates Max Heap. Based on
this criteria, a heap can be of two types −
For Input → 35 33 42 10 14 19 27 44 26 31
Min-Heap − Where the value of the root node is less than or equal to either of its children.

Max-Heap − Where the value of the root node is greater than or equal to either of its children.

Both trees are constructed using the same input and order of arrival.

Max Heap Construction Algorithm


We shall use the same example to demonstrate how a Max Heap is created. The procedure to
create Min Heap is similar but we go for min values instead of max values.
We are going to derive an algorithm for max heap by inserting one element at a time. At any
point of time, heap must maintain its property. While insertion, we also assume that we are
inserting a node in an already heapified tree.
Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
Note − In Min Heap construction algorithm, we expect the value of the parent node to be less
than that of the child node.
Let's understand Max Heap construction by an animated illustration. We consider the same input
sample that we used earlier.

Max Heap Deletion Algorithm


Let us derive an algorithm to delete from max heap. Deletion in Max (or Min) Heap always
happens at the root to remove the Maximum (or minimum) value.
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.

You might also like