{"id":11671,"date":"2023-01-18T06:43:28","date_gmt":"2023-01-18T06:43:28","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=11671"},"modified":"2023-01-24T09:54:47","modified_gmt":"2023-01-24T09:54:47","slug":"avl-tree-in-data-structure","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/","title":{"rendered":"AVL Tree in Data Structure"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg\" alt=\"\" \/><\/p>\n<p>In this article, we will learn what is AVL tree in data structure, what are different rotations in the AVL tree, the operations of the AVL tree in data structure, and the program to perform various operations on the AVL tree in data structure.<\/p>\n<h2>What is the AVL tree in data structure?<\/h2>\n<p>The name AVL comes from the inventor of this algorithm GM Adelson, Velsky, and EM Landis. The AVL tree is also called a Height Balanced Binary Search Tree. It is called a height-balanced binary search because the balance factor of each node in the AVL tree is 1,0, or -1. The <strong>balance factor<\/strong> of any node is the subtraction of the height of the left sub-tree and the height of the right sub-tree. Let\u2019s see how to calculate the balance factor with an example.<\/p>\n<p><strong>Balance factor of node = height of left sub-tree of &#8211; height of right subtree<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983292-avl%20tree%20in%20data%20structure1.png\" alt=\"\" \/><\/p>\n<p>In the above example, we can see that the height of the left sub-tree is 2, and the height of the right sub-tree is also 2. So, our balance factor for the root node will be 2-2 = 0.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983301-avl%20tree%20in%20data%20structure2.png\" alt=\"\" \/><\/p>\n<p>In the above example, we can see that the height of the left sub-tree is 2, and the height of the right sub-tree is also 1. So, our balance factor for the root node will be 2 &#8211; 1 = 1.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983301-avl%20tree%20in%20data%20structure3.png\" alt=\"\" \/><\/p>\n<p>In the above example, we can see that the height of the left sub-tree is 1, and the height of the right sub-tree is also 2. So, our balance factor for the root node will be 1 &#8211; 2 = -1.<\/p>\n<p>Now, we know what is balance factor of the node in the AVL tree is. Let\u2019s see an example of an AVL tree in data structure with a balance factor for each node.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983308-avl%20tree%20in%20data%20structure4.png\" alt=\"\" \/><\/p>\n<p>In the above example, we can say the above binary search tree is the AVL tree because the balance factor of each node is either 1 or 0, or -1. Every AVL tree is always a binary search tree but every binary search tree is not always an AVL tree.<\/p>\n<h2>Rotations in AVL tree:<\/h2>\n<p>There are four types of rotation in the AVL tree:<br \/>\n<strong>1. LL Rotation:<\/strong> this rotation is performed when a newly inserted node is in the left sub-tree of the left subtree of the particular node.<br \/>\n<strong>2. RR Rotation:<\/strong> this rotation is performed when a newly inserted node is in the right sub-tree of the right subtree of the particular node.<br \/>\n<strong>3. LR Rotation:<\/strong> this rotation is performed when a newly inserted node is in the right sub-tree of the left subtree of the particular node.<br \/>\n<strong>4. RL Rotation:<\/strong> this rotation is performed when a newly inserted node is in the left sub-tree of the right subtree of the particular node.<\/p>\n<h3>LL Rotation:<\/h3>\n<p>LL Rotation is performed when a newly added node violates the property of the AVL tree which means it changes the balance factor of a node out of range -1 to 1. Let\u2019s see an example to perform LL Rotation in the AVL tree.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983309-avl%20tree%20in%20data%20structure5.png\" alt=\"\" \/><\/p>\n<p>In the above example, we have inserted a new node 10 to the left of left to node 30. We can see that after inserting a new node the balance factor of node 30 becomes 2 and the tree becomes unbalanced. To make this tree balanced tree we will apply LL Rotation on the tree. We will apply the right rotation and after that, we can see the resultant tree becomes balanced.<\/p>\n<h3>RR Rotation:<\/h3>\n<p>RR Rotation is performed when a newly added node violates the property of the AVL tree which means it changes the balance factor of a node out of range -1 to 1. Let\u2019s see an example to perform RR Rotation in the AVL tree.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983313-avl%20tree%20in%20data%20structure6.png\" alt=\"\" \/><\/p>\n<p>In the above example, we have inserted a new node 30 to the right of right to node 10. We can see that after inserting a new node the balance factor of node 10 becomes -2 and the tree becomes unbalanced. To make this tree balanced tree we will apply RR Rotation on the tree. We will apply the left rotation and after that, we can see the resultant tree becomes balanced.<\/p>\n<h3>LR Rotation:<\/h3>\n<p>LR Rotation is performed when a newly added node violates the property of the AVL tree which means it changes the balance factor of a node out of range -1 to 1. Let\u2019s see an example to perform LR Rotation in the AVL tree.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983314-avl%20tree%20in%20data%20structure7.png\" alt=\"\" \/><\/p>\n<p>In the above example, we have inserted a new node 20 to the right of the left to node 30. We can see that after inserting a new node the balance factor of node 30 becomes -2 and the tree becomes unbalanced. To make this tree balanced tree we will apply LR Rotation on the tree. We will apply the left rotation and after that, we will apply the right rotation. Now the tree becomes unbalanced like in the first case and we have to apply LL Rotation. We can see the resultant tree becomes balanced.<\/p>\n<h3>RL Rotation:<\/h3>\n<p>RL Rotation is performed when a newly added node violates the property of the AVL tree which means it changes the balance factor of a node out of range -1 to 1. Let\u2019s see an example to perform RL Rotation in the AVL tree.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983319-avl%20tree%20in%20data%20structure8.png\" alt=\"\" \/><\/p>\n<p>In the above example, we have inserted a new node 20 to the left of the right to node 30. We can see that after inserting a new node the balance factor of node 30 becomes -2 and the tree becomes unbalanced. To make this tree balanced tree we will apply RL Rotation on the tree. We will apply the right rotation and after that, we will apply the left rotation. Now the tree becomes unbalanced like in the second case and we have to apply RR Rotation. We can see the resultant tree becomes balanced.<\/p>\n<h2>AVL tree in data structure example with dry-run<\/h2>\n<p>Let\u2019s take an example to understand the AVL tree in data structures.<\/p>\n<p><strong>Insertion:<\/strong><br \/>\nLet\u2019s see how an insertion operation is performed in the AVL tree.<br \/>\nElements =  17  18  19  11 10<br \/>\nWe will try to add each element one by one<\/p>\n<p><strong>insert 17:<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983319-avl%20tree%20in%20data%20structure9.png\" alt=\"\" \/><\/p>\n<p>We can see that we have inserted 17 in the tree and it becomes the root of the tree. The tree is also balanced.<\/p>\n<p><strong>insert 18:<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983319-avl%20tree%20in%20data%20structure10.png\" alt=\"\" \/><\/p>\n<p>After inserting 18, we can see that still, the tree is balanced thus we don\u2019t need to modify it.<\/p>\n<p><strong>insert 19:<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983319-avl%20tree%20in%20data%20structure11.png\" alt=\"\" \/><\/p>\n<p>After inserting 19, we can see that the balance factor of 17 becomes -2 and the tree becomes unbalanced. Now, we will apply RR rotation to the tree and we can see that it becomes a balanced tree after applying RR rotation.<\/p>\n<p><strong>insert 11:<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983329-avl%20tree%20in%20data%20structure12.png\" alt=\"\" \/><\/p>\n<p>After inserting 11, we can see that the tree is still a balanced tree. So, we do not need to modify it.<\/p>\n<p><strong>insert 10:<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983330-avl%20tree%20in%20data%20structure13.png\" alt=\"\" \/><\/p>\n<p>After inserting 10, we can see that the balance factor of 17 and 18 becomes 2 and the tree becomes unbalanced. Now, we will apply LL rotation after that, the tree becomes balanced.<\/p>\n<p><strong>Deletion:<\/strong><br \/>\nLet\u2019s see how a deletion operation is performed in the AVL tree.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983330-avl%20tree%20in%20data%20structure14.png\" alt=\"\" \/><\/p>\n<p>We have taken the above example to the AVL tree. Now, let\u2019s try to delete a node from the tree.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983330-avl%20tree%20in%20data%20structure15.png\" alt=\"\" \/><\/p>\n<p>In the above image, we can see that we are trying to delete node 19 from the tree.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983330-avl%20tree%20in%20data%20structure16.png\" alt=\"\" \/><\/p>\n<p>We can see that after the deletion of node 19, the balance factor of node 18 becomes 2 and the tree becomes unbalanced. Now, we will apply LL rotation to make the tree balanced.<\/p>\n<h2>Program of the AVL tree in data structure:<\/h2>\n<p>Let\u2019s see how to write a program of the AVL tree in data structure.<br \/>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11617 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_11617 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_11617 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11617 .wpsm_nav-tabs > li.active > a, #tab_container_11617 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11617 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_11617 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_11617 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11617 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11617 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11617 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11617 .wpsm_nav-tabs > li > a:hover , #tab_container_11617 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_11617 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_11617 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_11617 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_11617 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_11617 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_11617 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_11617 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11617 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_11617 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11617 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_11617 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_11617\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11617\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11617_1\" aria-controls=\"tabs_desc_11617_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_11617\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_11617_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Node {\r\n    int data, height;\r\n    Node left_child, right_child;\r\n\r\n    Node(int val){\r\n        data = val;\r\n        height = 0;\r\n    }\r\n}\r\n\r\nclass AVL_tree{\r\n    Node node;\r\n\r\n    int get_height(Node root){\r\n        if(root == null)\r\n            return -1;\r\n        return root.height;\r\n    }\r\n\r\n    int get_balance_factor(Node root){\r\n        if(root == null)\r\n            return 0;\r\n        return (get_height(root.left_child) - get_height(root.right_child));\r\n    }\r\n\r\n    Node LL_rotation(Node root){\r\n        Node child = root.left_child;\r\n        root.left_child = child.right_child;\r\n        child.right_child = root;\r\n        root.height = Math.max(get_height(root.left_child), get_height(root.right_child)) + 1;\r\n        child.height = Math.max(get_height(child.left_child), get_height(child.right_child)) + 1;\r\n        return child;\r\n    }\r\n\r\n    Node RR_rotation(Node root){\r\n        Node child = root.right_child;\r\n        root.right_child = child.left_child;\r\n        child.left_child = root;\r\n        root.height = Math.max(get_height(root.left_child), get_height(root.right_child)) + 1;\r\n        child.height = Math.max(get_height(child.left_child), get_height(child.right_child)) + 1;\r\n        return child;\r\n    }\r\n\r\n    void pre_order(Node root) {\r\n        if (root != null) {\r\n            System.out.print(root.data + \" \");\r\n            pre_order(root.left_child);\r\n            pre_order(root.right_child);\r\n        }\r\n    }\r\n\r\n    Node insert(Node root, int val){\r\n        if (root == null)\r\n            return (new Node(val));\r\n        if (val &lt; root.data)\r\n            root.left_child = insert(root.left_child, val);\r\n        else if (val &gt; root.data)\r\n            root.right_child = insert(root.right_child, val);\r\n        else\r\n            return node;\r\n\r\n        root.height = Math.max(get_height(root.left_child), get_height(root.right_child)) + 1;\r\n        int b = get_balance_factor(root);\r\n\r\n        if(b &gt; 1){\r\n            \/\/ LL Rotation Case\r\n            if(get_balance_factor(root.left_child) == 1){\r\n                root = LL_rotation(root);\r\n            }\r\n            \/\/ LR Rotation Case\r\n            else{\r\n                root.left_child = RR_rotation(root.left_child);\r\n                root = LL_rotation(root);\r\n            }\r\n        }\r\n        else if(b &lt; -1){\r\n            \/\/ RR Rotation Case\r\n            if(get_balance_factor(root.right_child) == -1){\r\n                root = RR_rotation(root);\r\n            }\r\n            \/\/ RL Rotation Case\r\n            else{\r\n                root.right_child = LL_rotation(root.right_child);\r\n                root = RR_rotation(root);\r\n            }\r\n        }\r\n        return root;\r\n    }\r\n\r\n    public static void main(String[] args) {\r\n        AVL_tree tree = new AVL_tree();\r\n        tree.node = tree.insert(tree.node, 17);\r\n        tree.node = tree.insert(tree.node, 18);\r\n        tree.node = tree.insert(tree.node, 19);\r\n        tree.node = tree.insert(tree.node, 11);\r\n        tree.node = tree.insert(tree.node, 10);\r\n        System.out.println(\"Pre-order Traversal of the AVL Tree is : \");\r\n        tree.pre_order(tree.node);\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_11617 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_11617 a\"),jQuery(\"#tab-content_11617\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>Pre-order Traversal of the AVL Tree is: \n18 11 10 17 19 <\/code><\/pre>\n<p><strong>Time complexity of the AVL tree:<\/strong><\/p>\n<ul>\n<li><strong>Traversal: O(n)<\/strong> where n is the number of totals node of the tree<\/li>\n<li><strong>Insertion: O(logn)<\/strong> because the max height of the AVL tree will be logn<\/li>\n<li><strong>Deletion: O(logn)<\/strong><\/li>\n<li><strong>Searching: O(logn)<\/strong><\/li>\n<\/ul>\n<h2>FAQs of the AVL tree in data structure:<\/h2>\n<p><strong>1. What is the difference between the binary search tree (BST) and the AVL tree?<\/strong><br \/>\nThe main difference between the binary search tree (BST) and the AVL tree is that the height of the AVL tree is always less than or equal to log(n) while the height of the BST can be n in the worst case (skewed tree).<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983330-avl%20tree%20in%20data%20structure17.png\" alt=\"\" \/><\/p>\n<p><strong>2. Why the height of the AVL tree is always less than or equal to log(n)?<\/strong><br \/>\nWe know the other name of the AVL tree is the self-balanced binary search tree. This means the binary tree will balance the height by itself. Thus, the height of the AVL tree will be always less than or equal to log(n).<\/p>\n<p><strong>3. What is the balance factor in the AVL tree?<\/strong><br \/>\nThe balance factor is used to find the difference in height between the left subtree and the right subtree. It is also used to check whether the tree is balanced or unbalanced. The balance factor of each node of the AVL tree will be either 1 or 0 or -1.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn what is AVL tree in data structure, what are different rotations in the AVL tree, the operations of the AVL tree in data structure, and the program to perform various operations on the AVL tree in data structure. What is the AVL tree in data structure? The name AVL [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[153],"tags":[],"class_list":["post-11671","post","type-post","status-publish","format-standard","hentry","category-tree"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>AVL Tree in Data Structure<\/title>\n<meta name=\"description\" content=\"What is AVL tree in data structure, what are different rotations and operations of the AVL tree in data structure?\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AVL Tree in Data Structure\" \/>\n<meta property=\"og:description\" content=\"What is AVL tree in data structure, what are different rotations and operations of the AVL tree in data structure?\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-18T06:43:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-24T09:54:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"AVL Tree in Data Structure\",\"datePublished\":\"2023-01-18T06:43:28+00:00\",\"dateModified\":\"2023-01-24T09:54:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/\"},\"wordCount\":1476,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg\",\"articleSection\":[\"Trees\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/\",\"name\":\"AVL Tree in Data Structure\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg\",\"datePublished\":\"2023-01-18T06:43:28+00:00\",\"dateModified\":\"2023-01-24T09:54:47+00:00\",\"description\":\"What is AVL tree in data structure, what are different rotations and operations of the AVL tree in data structure?\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Trees\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/tree\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"AVL Tree in Data Structure\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AVL Tree in Data Structure","description":"What is AVL tree in data structure, what are different rotations and operations of the AVL tree in data structure?","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/","og_locale":"en_US","og_type":"article","og_title":"AVL Tree in Data Structure","og_description":"What is AVL tree in data structure, what are different rotations and operations of the AVL tree in data structure?","og_url":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-01-18T06:43:28+00:00","article_modified_time":"2023-01-24T09:54:47+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"AVL Tree in Data Structure","datePublished":"2023-01-18T06:43:28+00:00","dateModified":"2023-01-24T09:54:47+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/"},"wordCount":1476,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg","articleSection":["Trees"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/","url":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/","name":"AVL Tree in Data Structure","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg","datePublished":"2023-01-18T06:43:28+00:00","dateModified":"2023-01-24T09:54:47+00:00","description":"What is AVL tree in data structure, what are different rotations and operations of the AVL tree in data structure?","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674021983219-avl%20tree%20in%20data%20structure.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/avl-tree-in-data-structure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Trees","item":"https:\/\/prepbytes.com\/blog\/category\/tree\/"},{"@type":"ListItem","position":3,"name":"AVL Tree in Data Structure"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=11671"}],"version-history":[{"count":2,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11671\/revisions"}],"predecessor-version":[{"id":11789,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11671\/revisions\/11789"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=11671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=11671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=11671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}