VOOZH about

URL: https://www.geeksforgeeks.org/dsa/avl-trees-containing-a-parent-node-pointer/

⇱ Insertion, Searching and Deletion in AVL trees containing a parent node pointer - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Insertion, Searching and Deletion in AVL trees containing a parent node pointer

Last Updated : 23 Jul, 2025

AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes. The insertion and deletion in AVL trees have been discussed in the previous article. In this article, insert, search, and delete operations are discussed on AVL trees that also have a parent pointer in their structure.

Definition of AVL tree node:

:

👁 Image

Below is the example of an AVL tree containing a parent pointer:

👁 Image

Insert Operation: The insertion procedure is similar to that of a normal AVL tree without a parent pointer, but in this case, the parent pointers need to be updated with every insertion and rotation accordingly. Follow the steps below to perform insert operation:

  • Perform standard BST insert for the node to be placed at its correct position.
  • Increase the height of each node encountered by 1 while finding the correct position for the node to be inserted.
  • Update the parent and child pointers of the inserted node and its parent respectively.
  • Starting from the inserted node till the root node check if the AVL condition is satisfied for each node on this path.
  • If w is the node where the AVL condition is not satisfied then we have 4 cases:
    • Left Left Case: (If the left subtree of the left child of w has the inserted node)
    • Left Right Case: (If the right subtree of the left child of w has the inserted node)
    • Right Left Case: (If the left subtree of the right child of w has the inserted node)
    • Right Right Case: (If the right subtree of the right child of w has the inserted node)

Below is the implementation of the above approach:


Output
Node: 30, Parent Node: NULL
Node: 20, Parent Node: 30
Node: 10, Parent Node: 20
Node: 25, Parent Node: 20
Node: 40, Parent Node: 30
Node: 50, Parent Node: 40

Time Complexity: O(log N), where N is the number of nodes of the tree.
Auxiliary Space: O(1)

Search Operation: The search operation in an AVL tree with parent pointers is similar to the search operation in a normal Binary Search Tree. Follow the steps below to perform search operation:

  • Start from the root node.
  • If the root node is NULL, return false.
  • Check if the current node's value is equal to the value of the node to be searched. If yes, return true.
  • If the current node's value is less than searched key then recur to the right subtree.
  • If the current node's value is greater than searched key then recur to the left subtree.

Below is the implementation of the above approach:


Output
value found

Time Complexity: O(log N), where N is the number of nodes of the tree
Auxiliary Space: O(1)

Delete Operation: The deletion procedure is similar to that of a normal AVL tree without a parent pointer, but in this case, the references to the parent pointers need to be updated with every deletion and rotation accordingly. Follow the steps below to perform the delete operation:

  • Perform the delete procedure as in a normal BST.
  • From the node that has been deleted, move towards the root.
  • At each node on the path, update the height of the node.
  • Check for AVL conditions at each node. Let there be 3 nodes: w, x, y where w is the current node, x is the root of the subtree of w which has greater height and y is the root of the subtree of x which has greater height.
  • If the node w is unbalanced, there exists one of the following 4 cases:
    • Left Left Case (x is left child of w and y is left child of x)
    • Left Right Case (x is left child of w and y is right child of x)
    • Right Left Case (x is right child of w and y is left child of x)
    • Right Right Case (x is right child of w and y is right child of x)

Below is the implementation of the above approach:


Output
Before deletion:
Node: 9, Parent Node: NULL
Node: 5, Parent Node: 9
Node: 0, Parent Node: 5
Node: 6, Parent Node: 5
Node: 10, Parent Node: 9
After deletion:
Node: 6, Parent Node: NULL
Node: 5, Parent ...

Time Complexity: O(log N), where N is the number of nodes of the tree
Auxiliary Space: O(1)

Comment