Augmented Binary Search Tree
A binary search tree (BST) is a rooted binary tree satisfying the BST property: each node’s value is greater or equal to all values in its left subtree, and smaller than the values in its right subtree.
An augmented binary search tree is a binary search tree that stores additional information in its nodes.
In this post I’ll show how to use this data structure to solve the following Leetcode problem :
You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].
With the help of an augmented BST, this problem can be solved in \(O(n \log n)\) average time. Here’s how.
In each node, I am going to store its key, its left child, right child and parent pointer, and an additional number lc: number of nodes in the left subtree of the node.
As I insert a node into the binary search tree, I preserve this property, and use this information to compute how many nodes are to the left of the inserted one:
The average complexity of insertion into a binary tree is \(O(\log n)\), and the worst case is \(O(n)\). Hence the average complexity of this algorithm is \(O(n \log n)\), and the worst case is \(O(n^2)\). However, we can do better with a balanced binary search tree, where the worst-case time of insertion is \(O(n)\). This is the topic of the next post.