---
title: "Working of a binary search tree (BST), and what is its time complexity for insertion and retrieval?"  
description: "Working of a binary search tree (BST), and what is its time complexity for insertion and retrieval?"  
author: "Steilla Mitchel"  
published: 2023-08-06  
updated: 2023-08-07  
canonical: https://www.mindstick.com/forum/159485/working-of-a-binary-search-tree-bst-and-what-is-its-time-complexity-for-insertion-and-retrieval  
category: "data structure"  
tags: ["data structure", "tree"]  
reading_time: 2 minutes  

---

# Working of a binary search tree (BST), and what is its time complexity for insertion and retrieval?

Working of a [binary search](https://www.mindstick.com/forum/159502/in-a-binary-search-what-exceptions-can-arise-if-the-input-array-is-not-sorted) tree (BST), and what is its time complexity for insertion and [retrieval](https://www.mindstick.com/interview/99/what-s-the-dot-net-datatype-that-allows-the-retrieval-of-data-by-a-unique-key)?

## Replies

### Reply by Aryan Kumar

A [binary](https://www.mindstick.com/forum/34709/please-write-a-program-for-decimal-to-binary-conversion-in-c-sharp) [search](https://www.mindstick.com/articles/65368/best-smo-services-company-in-hyderabad-improve-search-rankings) tree (BST) is a tree data structure that maintains the property that the value of each node is greater than or equal to the values of all of its left child nodes and less than or equal to the values of all of its right child nodes. This property allows for efficient insertion, retrieval, and traversal of the tree.

The working of a binary search tree is as follows:

1. When a new node is inserted into the tree, it is first compared to the root node. If the new node's value is less than the value of the root node, then the new node is inserted as the left child of the root node. If the new node's value is greater than the value of the root node, then the new node is inserted as the right child of the root node.
2. When a value is retrieved from the tree, the algorithm starts at the root node and compares the value to be retrieved to the value of the root node. If the value to be retrieved is less than the value of the root node, then the algorithm recursively searches the left subtree of the root node. If the value to be retrieved is greater than the value of the root node, then the algorithm recursively searches the right subtree of the root node. The algorithm continues to recursively search the tree until the value to be retrieved is found or until the entire tree has been searched.

The time complexity of insertion and retrieval in a binary search tree is O(log n), where n is the number of nodes in the tree. This is because, on average, the algorithm will only have to compare the value to be inserted or retrieved to O(log n) nodes before it finds the appropriate node.

Here are some additional points about binary search trees:

- Binary search trees are a type of balanced tree. This means that the height of the tree is logarithmic in the number of nodes.
- Binary search trees are a very efficient data structure for insertion, retrieval, and traversal.
- Binary search trees are not a good data structure for storing duplicate values.


---

Original Source: https://www.mindstick.com/forum/159485/working-of-a-binary-search-tree-bst-and-what-is-its-time-complexity-for-insertion-and-retrieval

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
