The Wayback Machine - https://web.archive.org/web/20240624010821/https://www.geeksforgeeks.org/applications-of-bst/
Open In App

Applications of BST

Last Updated : 09 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

 Binary Search Tree (BST) is a data structure that is commonly used to implement efficient searching, insertion, and deletion operations. The key feature of a BST is that it is a binary tree, where each node has at most two child nodes, and the value of each node is greater than all the values in its left subtree and less than all the values in its right subtree. This means that the left subtree of a node contains values that are smaller than the node’s value, and the right subtree contains values that are larger. Due to this property, BSTs allow for efficient searching by repeatedly dividing the search space in half, which makes it an important data structure in computer science and many other fields.

  • 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 tree. There must be no duplicate nodes.

Binary Search Tree – Structure

A BST supports operations like search, insert, delete, floor, ceil, greater, smaller, etc in O(h) time where h is height of the BST. To keep height less, self balancing BSTs (like AVL and Red Black Trees) are used in practice. These Self-Balancing BSTs maintain the height as O(Log n). Therefore all of the above mentioned operations become O(Log n). Together with these, BST also allows sorted order traversal of data in O(n) time.

  1. A Self-Balancing Binary Search Tree is used to maintain sorted stream of data. For example, suppose we are getting online orders placed and we want to maintain the live data (in RAM) in sorted order of prices. For example, we wish to know number of items purchased at cost below a given cost at any moment. Or we wish to know number of items purchased at higher cost than given cost.
  2. A Self-Balancing Binary Search Tree is used to implement doubly ended priority queue. With a Binary Heap, we can either implement a priority queue with support of extractMin() or with extractMax(). If we wish to support both the operations, we use a Self-Balancing Binary Search Tree to do both in O(Log n)
  3. There are many more algorithm problems where a Self-Balancing BST is the best suited data structure, like count smaller elements on right, Smallest Greater Element on Right Side, etc.
  4. One of the most common use cases of BSTs is searching for a particular element in the tree.
  5.  A BST can be used to sort a large dataset. By inserting the elements of the dataset into a BST and then performing an in-order traversal, the elements will be returned in sorted order.
  6. Used in Database indexing.
  7. TreeMap and TreeSet are two  data structures that are internally implemented using self-balancing BSTs, more formally a Red-Black Tree.
  8. BSTs can be used to implement symbol tables, which are used to store data such as variable and function names in a programming language. 

Previous Article
Next Article

Similar Reads

Two nodes of a BST are swapped, correct the BST
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST. Input: x = 20, y = 8 10 / \ 5 8 / \ 2 20Output: 10 / \ 5 20 / \ 2 8 Input: x = 10 y = 5 10 / \ 5 8 / \ 2 20Output: 5 / \ 10 20 / \ 2 8 Recommended ProblemFixing Two swapped nodes of a BSTSolve Problem Approach: The in-order traversal of a BST produces a sorted arr
15+ min read
POTD Solutions | 15 Oct' 23 | Normal BST to Balanced BST
View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Binary Search Trees but will also help you build up problem-solving skills. POTD 15 October: Normal BST t
5 min read
K'th Largest Element in BST when modification to BST is not allowed
Given a Binary Search Tree (BST) and a positive integer k, find the k'th largest element in the Binary Search Tree. For example, in the following BST, if k = 3, then output should be 14, and if k = 5, then output should be 10. We have discussed two methods in this post. The method 1 requires O(n) time. The method 2 takes O(h) time where h is height
15+ min read
Find k-th smallest element in BST (Order Statistics in BST)
Given the root of a binary search tree and K as input, find Kth smallest element in BST. For example, in the following BST, if k = 3, then the output should be 10, and if k = 5, then the output should be 14. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Method 1: Using Inorder Traversal (O(n) time and O(h) auxil
15+ min read
Find the largest BST subtree in a given Binary Tree | Set 3
Largest BST in a Binary Tree | Set 3Method 3 (Shorter, Smarter and More Efficient) In this section, a different O(n) solution is discussed. This solution is simpler than the solutions discussed in Set-1 and Set-2 and works in O(n) time. In this method, we do not need to check explicitly if the binary tree is BST. A Tree is BST if the following is t
8 min read
Sorted order printing of a given array that represents a BST
Given an array that stores a complete Binary Search Tree, write a function that efficiently prints the given array in ascending order. For example, given an array [4, 2, 5, 1, 3], the function should print 1, 2, 3, 4, 5 Solution: Inorder traversal of BST prints it in ascending order. The only trick is to modify recursion termination condition in st
4 min read
Floor and Ceil from a BST
Given a binary search tree and a key(node) value, find the floor and ceil value for that particular key value. Floor Value Node: Node with the greatest data lesser than or equal to the key value. Ceil Value Node: Node with the smallest data larger than or equal to the key value. Example: 8 / \ 4 12 / \ / \2 6 10 14 Key: 11 Floor: 10 Ceil: 12Key: 1
15+ min read
Sorted Array to Balanced BST
Given a sorted array. Write a function that creates a Balanced Binary Search Tree using array elements. Examples: Input: arr[] = {1, 2, 3}Output: A Balanced BST 2 / \ 1 3 Explanation: all elements less than 2 are on the left side of 2 , and all the elements greater than 2 are on the right side Input: arr[] = {1, 2, 3, 4}Output: A Balanced BST 3 / \
15+ min read
Construct BST from given preorder traversal using Stack
Given preorder traversal of a binary search tree, construct the BST.For example, if the given traversal is {10, 5, 1, 7, 40, 50}, then the output should be root of following tree. 10 / \ 5 40 / \ \ 1 7 50 We have discussed O(n^2) and O(n) recursive solutions in the previous post. Following is a stack based iterative solution that works in O(n) time
13 min read
Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
Given a Binary Search Tree (BST), convert it to a Binary Tree such that every key of the original BST is changed to key plus sum of all greater keys in BST. Examples: Input: Root of following BST 5 / \ 2 13 Output: The given BST is converted to following Binary Tree 18 / \ 20 13 Method 1: Solution: Do reverse In order traversal. Keep track of the s
15+ min read
Article Tags :
Practice Tags :