Open In App

Complexity of different operations in Binary tree, Binary Search Tree and AVL tree

Last Updated : 21 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have a basic idea about Binary Tree, Binary Search Tree, and AVL Tree. The main operations in a binary tree are: search, insert and delete. We will see the worst-case time complexity of these operations in binary trees:

Binary Tree:

In a binary tree, a node can have maximum of two children. Consider the left-skewed binary tree shown in Figure 1:

Complexity Analysis:

  • Searching: For searching element 2, we have to traverse all elements (assuming we do breadth first traversal). Therefore, searching in binary tree has worst case complexity of O(N).
  • Insertion: For inserting element as left child of 2, we have to traverse all elements. Therefore, insertion in binary tree has worst case complexity of O(N).
  • Deletion: For deletion of element 2, we have to traverse all elements to find 2 (assuming we do breadth first traversal). Therefore, deletion in binary tree has worst case complexity of O(N).

Binary Search Tree (BST): 

BST is a special type of binary tree in which the left child of a node has a value less than the parent and the right child has a value greater than the parent. Consider the left-skewed BST shown in Figure 2. 

Complexity Analysis:

  • Searching: For searching element 1, we have to traverse all elements (in order 3, 2, 1). Therefore, searching in binary search tree has worst case complexity of O(n). In general, the time complexity is O(h) where h is the height of BST.
  • Insertion: For inserting element 0, it must be inserted as the left child of 1. Therefore, we need to traverse all elements (in order 3, 2, 1) to insert 0 which has the worst-case complexity of O(n). In general, the time complexity is O(h).
  • Deletion: For deletion of element 1, we have to traverse all elements to find 1 (in order 3, 2, 1). Therefore, deletion in binary tree has worst case complexity of O(n). In general, the time complexity is O(h).

AVL/ Height Balanced Tree:  

AVL tree is a binary search tree with an additional property that the difference between the height of the left sub-tree and the right sub-tree of any node can’t be more than 1

For example, BST shown in Figure 2 is not AVL as the difference between the left sub-tree and the right sub-tree of node 3 is 2. However, BST shown in Figure 3 is an AVL tree

Complexity Analysis:

  • Searching: For searching element 1, we have to traverse elements (in order 5, 4, 1) = 3 = log2n. Therefore, searching in the AVL tree has worst-case complexity of O(log2n).
  • Insertion: For inserting element 12, it must be inserted as the right child of 9. Therefore, we need to traverse elements (in order 5, 7, 9) to insert 12 which has the worst-case complexity of O(log2n).
  • Deletion: For deletion of element 9, we have to traverse elements to find 9 (in order 5, 7, 9). Therefore, deletion in a binary tree has worst-case complexity of O(log2n).

We will discuss questions based on the complexities of binary tree operations:

Que-1. What is the worst-case time complexity for search, insert and delete operations in a general Binary Search Tree? 

  • (A) O(n) for all 
  • (B) O(Logn) for all 
  • (C) O(Logn) for search and insert, and O(n) for delete 
  • (D) O(Logn) for search, and O(n) for insert and delete 

Solution: As discussed, all operations in BST have the worst-case time complexity of O(n). So, the correct option is (A). 

Que-2. What are the worst-case time complexities of searching in a binary tree, BST, and AVL tree respectively? 

  • (A) O(n) for all
  • (B) O(Logn) for all 
  • (C) O(n) for binary tree, and O(Logn) for others 
  • (D) O(n) for binary tree and BST, and O(Logn) for AVL 

Solution: As discussed, search operation in binary tree and BST have worst-case time complexity of O(n). However, the AVL tree has the worst-case time complexity of O(log n). So, the correct option is (D).


Similar Reads

What is AVL Tree | AVL Tree meaning
An AVL is a self-balancing Binary Search Tree (BST) where the difference between the heights of left and right subtrees of any node cannot be more than one. KEY POINTSIt is height balanced treeIt is a binary search treeIt is a binary tree in which the height difference between the left subtree and right subtree is almost oneHeight is the maximum de
2 min read
Difference between Binary Search Tree and AVL Tree
Binary Search Tree:A binary Search Tree is a node-based binary tree data structure that has the following properties: The left subtree of a node contains only nodes with keys lesser than the node’s key.The right subtree of a node contains only nodes with keys greater than the node’s key.The left and right subtree each must also be a binary search t
2 min read
How is an AVL tree different from a B-tree?
AVL Trees: AVL tree is a self-balancing binary search tree in which each node maintain an extra factor which is called balance factor whose value is either -1, 0 or 1. B-Tree: A B-tree is a self - balancing tree data structure that keeps data sorted and allows searches, insertions, and deletions in O(log N) time. Difference between AVL Tree and B-T
1 min read
Time Complexity and Space Complexity
Generally, there is always more than one way to solve a problem in computer science with different algorithms. Therefore, it is highly required to use a method to compare the solutions in order to judge which one is more optimal. The method must be: Independent of the machine and its configuration, on which the algorithm is running on.Shows a direc
14 min read
Different shapes of AVL possible at height h
AVL Tree: It is a self-balancing Binary Search Tree where the Balance Factor cannot be more than one for all nodes. Balance Factor can be defined as the difference between heights of left and right subtree. Example: The task is to find the possible number of different shapes of a minimal AVL tree of height h can be formed. This can be understood by
1 min read
Red Black Tree vs AVL Tree
In this post, we will compare Red-Black Tree and AVL Tree. Red Black Tree: Properties: Self-Balancing is provided by painting each node with two colors(Red or Black).When the Tree is modified, a new tree is subsequently rearranged and repainted.It requires 1 bit of color information for each node in the tree.Time complexity: O(logn). Constraints ma
2 min read
Time and Space Complexity Analysis of Binary Search Algorithm
Time complexity of Binary Search is O(log n), where n is the number of elements in the array. It divides the array in half at each step. Space complexity is O(1) as it uses a constant amount of extra space. AspectComplexityTime ComplexityO(log n)Space ComplexityO(1)The time and space complexities of the binary search algorithm are mentioned below.
3 min read
Complexity analysis of various operations of Binary Min Heap
A Min Heap is a Complete Binary Tree in which the children nodes have a higher value (lesser priority) than the parent nodes, i.e., any path from the root to the leaf nodes, has an ascending order of elements. In the case of a binary tree, the root is considered to be at height 0, its children nodes are considered to be at height 1, and so on. Each
3 min read
Practice questions on Height balanced/AVL Tree
AVL tree is binary search tree with additional property that difference between height of left sub-tree and right sub-tree of any node can’t be more than 1. Here are some key points about AVL trees: If there are n nodes in AVL tree, minimum height of AVL tree is floor(log2n).If there are n nodes in AVL tree, maximum height can’t exceed 1.44*log2n.I
4 min read
Minimum number of nodes in an AVL Tree with given height
Given the height of an AVL tree 'h', the task is to find the minimum number of nodes the tree can have. Examples : Input : H = 0 Output : N = 1 Only '1' node is possible if the height of the tree is '0' which is the root node. Input : H = 3 Output : N = 7 Recursive Approach : In an AVL tree, we have to maintain the height balance property, i.e. dif
7 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg