Understanding Binary Search Trees: Adding Nodes and Applications

Kacper Bąk
2 min readMar 13, 2023
Photo by Aaron Burden on Unsplash

Finding a concrete example of how to use tree numbers in an application can be a challenge. This is understandable, as context is key when it comes to the application of any data structure. In this article, I will provide an explanation of binary search trees and how to add a new node to them.

Binary search trees are a common data structure used in computer science. They are a type of tree in which each node has at most two children, called left and right subtrees. The key property of a binary search tree is that the left subtree of a node contains only nodes with keys smaller than the node’s key, and the right subtree contains only nodes with keys larger than the node’s key.

One of the most significant advantages of binary search trees is that they allow efficient search and insertion of elements. This is due to a property of binary search that allows implementing a binary search algorithm that can locate a given key in logarithmic time.

To add a new node to the binary search tree, I must first locate its corresponding position based on the key value. I started with the root node and compared the key value with the root key. If the key is smaller than the root key, I move to the left subtree, and if it is larger, I move to the right subtree. I repeat this process recursively until I find an empty position in the tree where we can add a new node.

I also noticed that adding a new node to the binary search tree requires rebuilding the entire tree. This is because the property of the binary search tree must be maintained at all times, and adding a new node can potentially affect the structure of the entire tree.

In summary, binary search trees are a powerful data structure with many applications in computer science. They allow efficient search and insertion of elements, making them ideal for tasks such as maintaining a sorted list of data. Adding a new node to a binary search tree requires careful consideration of its key value and corresponding position in the tree.

--

--