A weight balanced tree is a type of balanced binary search tree that ensures the weights or sizes of its subtrees are approximately equal. This balance factor helps to improve the efficiency of various operations, such as searching, inserting, and deleting elements from the tree.

## What is a Binary Search Tree?

A binary search tree (BST) is a binary tree where each node has a key greater than all keys in its left subtree and smaller than all keys in its right subtree. This property makes searching for an element in the BST efficient, as it allows us to eliminate half of the remaining search space at each step.

## Understanding Weight Balanced Trees

A weight balanced tree extends the concept of a BST by considering the weights or sizes of its subtrees. The weight or size of a subtree is defined as the number of nodes present in that subtree. The goal of maintaining weight balance is to ensure that for any node in the tree, the difference between the weights of its left and right subtrees is within a certain range.

To achieve this balance, weight balanced trees use different strategies during insertion and deletion operations. These strategies involve performing rotations to restructure the tree and maintain its weight balance.

### Insertion Operation

When inserting a new node into a weight balanced tree, we first perform a regular BST insertion. After this step, we check if any node violates the weight balance property. If so, we perform rotations to adjust the structure and restore balance.

- If the left subtree’s weight exceeds twice the right subtree’s weight plus 1, we perform a
**right rotation**. - If the right subtree’s weight exceeds twice the left subtree’s weight plus 1, we perform a
**left rotation**. - If the left subtree’s weight exceeds the right subtree’s weight by 1, we perform a
**right-left rotation**. - If the right subtree’s weight exceeds the left subtree’s weight by 1, we perform a
**left-right rotation**.

### Deletion Operation

During deletion, we again perform a regular BST deletion. If so, we apply rotations to restore balance.

**left-right rotation**.

**right-left rotation**.

## Advantages of Weight Balanced Trees

The use of weight balanced trees offers several advantages:

__Better Performance:__Weight balanced trees maintain approximate balance between subtrees, which improves search time complexity and reduces height imbalances.__Easier Maintenance:__The algorithms for insertion and deletion in a weight balanced tree are relatively simpler compared to other self-balancing tree structures.

## Conclusion

A weight balanced tree is a type of binary search tree that maintains balance by considering the weights or sizes of its subtrees. By ensuring the weights of the left and right subtrees are approximately equal, weight balanced trees provide efficient search and manipulation operations. The use of rotations during insertion and deletion helps to maintain weight balance and ensure optimal performance.