# Which Tree Data Structure Is Used by Map?

//

Heather Bennett

Trees are a fundamental data structure in computer science, and they find wide applications in various algorithms and data storage systems. One common use case for trees is in the implementation of maps, which are key-value data structures. In this article, we will explore the tree data structure used by the map.

Trees and Maps:
Trees provide an efficient way to store and retrieve data. They have a hierarchical structure consisting of nodes connected by edges. Each node can have zero or more child nodes, except for the root node which has no parent.

Maps, on the other hand, are abstract data types that store key-value pairs. They allow efficient lookup and retrieval of values based on their associated keys. Maps can be implemented using various data structures, including arrays, linked lists, and trees.

## Binary Search Trees (BST):

One popular tree data structure used for implementing maps is the Binary Search Tree (BST). A BST is a binary tree in which each node has at most two children: a left child and a right child. The left child contains keys smaller than its parent’s key, while the right child contains keys greater than its parent’s key.

The BST property ensures that all keys in the left subtree of a node are less than its key, and all keys in the right subtree are greater than its key. This property allows for efficient search, insertion, and deletion operations.

### Operations on BST:

• Search: To search for a specific key in a BST, we compare it with the current node’s key. If it matches, we return the corresponding value.

If it is smaller, we move to the left subtree; otherwise, we move to the right subtree.

• Insertion: To insert a new key-value pair into a BST, we start by searching for the appropriate position. Once we find the correct location (i.e., a null child), we create a new node and attach it as a leaf node.
• Deletion: Deleting a node from a BST requires careful handling to maintain the BST property. There are three cases to consider: deleting a leaf node, deleting a node with one child, and deleting a node with two children.

## Balanced Search Trees:

While BSTs provide efficient operations in the average case, their performance can degrade in certain scenarios. For example, if keys are inserted in sorted order, the tree becomes unbalanced, resembling a linked list. This leads to poor search performance.

To address this issue, various balanced search tree variants have been developed. Some well-known examples include AVL trees, Red-Black trees, and B-trees. These trees ensure that the height of the tree remains balanced, resulting in improved performance guarantees for search operations.

### AVL Trees:

AVL trees are self-balancing binary search trees that maintain an additional balance factor for each node. This factor represents the difference between the heights of the left and right subtrees. If this balance factor exceeds 1 or -1, rotations are performed to restore balance.

### Red-Black Trees:

Red-Black trees are another type of self-balancing binary search tree. They ensure that no path from the root to any leaf is more than twice as long as any other path. To maintain balance during insertions and deletions, color properties are assigned to nodes (either red or black), and rotations are performed accordingly.

## Conclusion:

In conclusion, maps can be implemented using various tree data structures such as Binary Search Trees (BSTs), AVL trees, or Red-Black trees. These structures provide efficient and flexible ways to store key-value pairs and perform operations like search, insertion, and deletion.

The choice of tree structure depends on factors such as the expected workload, the need for self-balancing properties, and the desired performance guarantees. Understanding the underlying tree data structure is crucial for designing efficient map implementations in programming languages and databases.