Movatterモバイル変換


[0]ホーム

URL:


Open In App

AVL Tree is aself-balancing Binary Search Tree (BST) where thedifference between heights of left and right subtrees cannot be more than one for all nodes.

Examples:

The above tree is AVL because thedifferences between the heights of the left and right subtrees for every node are less than or equal to1. Below is the example that is not an AVL Tree:

The above tree is not AVL because the differences between the heights of the left and right subtrees for 8 and 12 are greater than 1. It can be defined as a type of binary search tree that has all its nodes with a balance factor of -1, 0, or 1.

What is the balance factor: It is the difference in height between the two subtrees. (Balance Factor: Height of Left subtree - Height of right subtree)

Insertion of Strings in AVL Tree:

Below example demonstration of inserting the days of the week in the order:{Tuesday, Monday, Thursday, Saturday, Sunday, Friday, Wednesday}

Approach:

  • If thenode is empty(NULL), create the node with the first value given.
  • If thenew node is less than theprevious node then insert the new node at the left
  • If thenew node is greater than theprevious node then insert the new node at the right

Check if the tree is balanced:

  • If thebalance factor is greater than1 then it can be either:
    • Left-Left Case (then a single rotation is required to make it balanced).
    • Or Left-Right Case(Double rotation required).
  • If the balance factor is lesser than -1 then it can be either:
    • Right-Right Case (Single rotation required).
    • Or Right-Left Case (Double rotation required)

This process should be repeated for the insertion of all of the remaining nodes.

Points to Remember:

  • Insert nodes accordingly: To make sure that the given tree remains AVL after every insertion, augment the standard BST insert operation to perform some re-balancing. Following are two basic operations that can be performed tore-balance a BST without violating the BST property(keys(left) < key(root) < keys(right)).
  • Check the Balance Factor: The balance factor should always be -1, 0, 1 if not then there is a need to rotate the tree accordingly.

Steps To Create AVL Tree:

  • So according to the given order, let's insertTuesday, as it has no node so it has abalance factor of0.
  • The next step is to insertMonday to the left of Tuesday as it isalphabetically smaller(M < T). There is a need to check the balance factor of each node before inserting a new node. The tree is balanced as Tuesday is balanced because it has one subtree towards the left:

Balance Factor = height of the left subtree - the height of right subtree = 1 - 0 = 1

Therefore, the tree is balanced. Monday is also balanced because of balance factor 0.

  • Next, insertThursday. It is inserted to the left of Tuesday(as Th < Tu) and it is inserted to the right of Monday(as T>M). Now if the balance factor, is checked it can be seen that the balance factor of Tuesday is 2 so it is unbalanced, so there is a need to rotate the tree to make it balanced.

Note:In case if we don't remember the rules of rotation of AVL tree, still it can be balanced we just need to remember that:Left node < Root < Right node.

Example:

   5                        4
  /                       /   \
 3     ----->      3       5
  \
   4

  • After that, insertSaturday, to the left of Thursday (S < T)and to the right of Monday(M < S).
  • Sunday is inserted to the right of Saturday (Sa < Su). Now the tree is unbalanced, so it is rotated to make it again balanced.
  • Friday is inserted following the same rules and the balance factor of Thursday becomes 2, so rotation is again done.
  • After rotation, now the last nodeWednesday is inserted to the right of Tuesday (as T<W).


If the AVL tree is checked, now the entire AVL tree is height-balanced as all the nodes have balance factors-1,0,1.


Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp