# What Is BTS Data Structure?

//

Heather Bennett

What Is BTS Data Structure?

The Binary Search Tree (BTS) data structure is a type of tree-based data structure that allows for efficient searching, insertion, and deletion operations. It is called a binary tree because each node in the tree has at most two children. The BTS data structure follows a specific ordering property that makes searching and other operations much faster compared to other data structures.

## The Structure of a BTS

A BTS consists of nodes that are connected through edges. Each node contains a key value and two pointers to its left and right child nodes. The key values of the left child nodes are always less than the key value of their parent node, while the key values of the right child nodes are always greater than the key value of their parent node.

To illustrate this concept, let’s consider an example. Assume we have the following numbers: 8, 3, 10, 1, 6, 14, 4, and 7. We can construct a BTS as follows:

```          8
/   \
3     10
/ \      \
1   6     14
/ \
4   7
```

## Searching in a BTS

Searching in a BTS is efficient because we can use the ordering property to our advantage. Starting from the root node, we compare the Target value with the current node’s key value.

If they are equal, we have found our Target. If the Target is less than the current node’s key value, we move to its left child; otherwise, we move to its right child.

This process continues recursively until either we find the Target or reach a leaf node (a node without any children). If we reach a leaf node without finding the Target, it means the Target value does not exist in the BTS.

## Insertion and Deletion in a BTS

Inserting a new node into a BTS involves finding the appropriate position according to the ordering property. We start from the root node and compare the key value of the new node with the current node’s key value.

If it is less, we move to its left child; if it is greater, we move to its right child. We repeat this process until we find an empty spot (a null pointer) where we can insert our new node.

Deleting a node from a BTS requires some additional considerations. There are three cases to consider:

• If the node to be deleted has no children, we can simply remove it by updating its parent’s pointer.
• If the node has only one child, we replace it with its child by updating its parent’s pointer.
• If the node has two children, we need to find either its successor (the smallest value larger than the current node) or predecessor (the largest value smaller than the current node) and replace it with that. This ensures that the ordering property of the tree is maintained.

## Conclusion

In summary, a BTS data structure is an efficient way to store and retrieve data based on their values. Its ordering property enables fast searching, insertion, and deletion operations. Understanding how a BTS works and how to use it effectively can greatly benefit your programming skills.