## Self-Balancing Binary Search TreesData Structures are a specified way to organize and store data in computers in such a manner that we can execute operations on the stored data more effectively and efficiently. Binary Search Trees (BSTs) are vital in performing efficient operations among the different data structures available. In the following tutorial, we will learn about the So, let's get started. ## Understanding the Binary Search TreesA
Let us now have a look at some of the key terms associated with Binary Search Tree: - A
**leaf**is a node having no children. - The number of nodes on the path from the root node down to (and including) that specific node is known as the
**depth of a node**. - The
**maximum depth of a binary tree**refers to the depth of its deepest left. Thus, we can also consider it as the**height of the tree**. - A Tree is said to be
**balanced**if, for every node in the tree, the height of its left and right sub-trees**differs by at most 1**.
## Operations of the Binary Search TreeA Binary Search Tree generally supports operations like Insertion, Deletion, and Searching. The Time complexity of each operation depends upon the height of the tree, which means that the operation will have to traverse all the nodes present on the path from the root to the deepest leaf in the worst case. Let us consider a problem as an example where the tree is heavily skewed. A figure demonstrating the same is shown below:
Although the tree shown in the above figure is a valid Binary Search Tree, it is ineffective. If we want to insert, search, or delete a node from the above tree, we may be traversing every node present. Thus, the n is the number of nodes in the tree.However, we are aiming for a balanced tree. A balanced tree is a tree where, for every node, the height of its left and right sub-trees differs by, at most O(log in the worst case. We can think of it as an important improvement from _{2}?n)O(n).## Understanding the Self-Balanced Binary Search TreeOne way to ensure that the tree is always balanced is by the implementation of a Self-Balancing Binary Search Tree. A Self-Balancing Binary Search Tree is a Binary Search Tree (BST) that inevitably attempts always to maintain its height to a minimum, ensuring that its operations will maintain a worst-case time complexity of Let us understand the above statement mathematically for better knowledge. A Binary Tree with height h can have at most
Hence, with respect to the Self-Balancing Binary Search Trees, the minimum height must be equal to the floor value of -1, 0, or + 1. This value is called the Balance Factor.
## How do Self-Balancing Binary Search Trees balance?Regarding self-balancing, Binary Search Trees perform rotations after the execution of an insert and delete operations. The following are the two types of rotation operations that can be performed to balance the Binary Search Trees without breaching the property of the Binary Search Tree: - Left Rotation
- Right Rotation
Let us now understand the above two operations in brief. ## Left RotationThe Left Rotation is one of the operations of the Binary Search Tree where we push a node, N, down to the left in order to balance the tree. This rotation assumes that node N has the right child (or sub-tree). The right child, R, of node N, becomes the parent node of N, and the left child of R becomes the new right child of node N. This rotation is used specifically when the right sub-tree of node N has a considerably (depending on the tree's type) greater height than its left sub-tree. Let us now consider the following figure illustrating the same.
In the above figure, when we left rotate about node A, node B becomes the new root of the subtree. Node A becomes the left child of node B, and the sub-tree, y, becomes the right child of node A. ## Right RotationThe Right Rotation is another operation of the Binary Search Tree where we push a node, N, down to the right in order to balance the tree. This rotation assumes that node N has a left child (or sub-tree). The left child, L, of the node N, becomes the parent node of N, and the right child of L becomes N's new left child. This rotation is specifically used when the left sub-tree of node N has a considerably (depending on the tree's type) greater height than its right sub-tree. Let us now consider the following figure illustrating the same.
In the above figure, when we right rotate about node B, node A becomes the new root of the subtree. Node B becomes the left child of node A, and the sub-tree, y, becomes the left child of node A. ## Note:Once the rotations are done, the in-order traversal of nodes in both the previous and final trees will be the same, and the Binary Search Tree property will retain. ## Understanding the Types of Self-Balancing Binary Search TreesThe following are some data structures that implement this type of Tree: - 2-3 Tree
- Red-Black Tree
- AA Tree
- AVL Tree
- B Tree
- Scapegoat Tree
- Splay Tree
- Tango Tree
- Treap
- Weight Balanced Trees
Let us now discuss these trees in brief. ## 2-3 TreeA - Two Children and a Data Element, or
- Three Children and two Data Elements, or
- Leaf nodes are at the same level having no child but either one or two Data Elements.
Let us consider an example of a 2-3 Tree:
From the above figure, we can observe that this tree satisfies all the rules mentioned earlier.
- Space -
**O(?n)** - Search -
**O(log**_{2}?n) - Insert -
**O(log**_{2}?n) - Delete -
**O(log**_{2}?n)
## Red-Black TreeA - Every node is colored either Black or Red.
- The Roots of the Tree is always Black.
- All the leaves of the Tree that are NIL must be Black.
- No Two Red nodes should be adjacent, meaning that a red node can neither have a red child nor a red parent.
- Every path from a node to its descendant NIL nodes has the same number of Black nodes.
The following is an example of the Red-Black Tree:
- Space -
**O(?n)** - Search -
**O(log**_{2}?n) - Insert -
**O(log**_{2}?n) - Delete -
**O(log**_{2}?n)
## AA TreeThe - The Level of every leaf node in an AA Tree is one.
- The Level of every left child is precisely one less than that of its parent.
- The Level of every right child is less than or equal to that of its parent.
- The Level of every right grandchild is strictly less than that of its grandparent.
- Every node of a Level greater than one has two children.
The following is an example of an AA Tree:
The AA Tree guarantees fast operations with time complexity of ## AVL TreeThe
The Balance Factor for an AVL Tree is an integer ranging from -1 to 1. The following is an example of an AVL Tree:
- Space -
**O(?n)** - Search -
**O(log**_{2}?n) - Insert -
**O(log**_{2}?n) - Delete -
**O(log**_{2}?n)
## B-TreeA As per the definition of Knuth, a B-Tree of order m is a tree that satisfies some properties as follows: - Every node of a B-Tree has at most
**n**children. - Every non-leaf node (except root) has at least
**[n/2]**child nodes. - If the root is not a leaf node, it has at least two children.
- A non-leaf node with
**m**children consists of**m - 1**keys. - All leaves exist at the same level and carry no information.
Let us consider the following example of a B-Tree:
B-Trees are well suited for storage systems that read and write a reasonably large block of data. These Trees are also used in databases and file systems.
- Space -
**O(?n)** - Search -
**O(log**_{2}?n) - Insert -
**O(log**_{2}?n) - Delete -
**O(log**_{2}?n)
## Scapegoat TreeA The balancing concept is to ensure that the nodes are
- Space -
**O(?n)** - Search -
**O(log**_{2}?n) - Insert -
**O(log**_{2}?n) - Delete -
**O(log**_{2}?n)
- Space -
**O(?n)** - Search -
**O(log n)** - Insert -
**Amortized O(log n)** - Delete -
**Amortized O(log n)**
## Splay TreeA For example, whenever we perform a standard Binary Search for a particular data element, followed by the rotations of a tree in a particular order such that this data element is placed as the root. We could also use a Top-Down algorithm to combine the search and reorganization operations into a single phase. Splaying depends upon three factors while accessing a data element (for example, - Whether the data element,
**x**, is the left or right child of its parent node,**p**. - Whether the parent node,
**p**, is the root or not, and if not. - Whether the parent node,
**p**, is the left or right child of its parent,**q (the grandparent of x**).
On the basis of the above factors, we have categorized the rotations into different types: - Zig Rotation [Right Rotation]
- Zig Zig Rotation [Two Right Rotation]
- Zag Rotation [Left Rotation]
- Zag Zag Rotation [Two Left Rotation]
- Zig Zag Rotation [Zig followed by Zag]
- Zag Zig Rotation [Zag followed by Zig]
- Space -
**O(?n)** - Search -
**O(log n)** - Insert -
**O(log n)** - Delete -
**O(log n)**
- Space -
**O(?n)** - Search -
**Amortized O(log n)** - Insert -
**Amortized O(log n)** - Delete -
**Amortized O(log n)**
## TreapThe Every node of a Treap consists of two values: **KEY:**The**Key**is a value that follows standard Binary Search Tree ordering.**PRIORITY:**The**Priority**is a randomly assigned value that follows the max-heap property.
Let us consider the following example of a Treap:
The above figure exemplifies a Treap with an alphabetic key and numeric max heap order.
- Space -
**O(?n)** - Search -
**O(log**_{2}?n) - Insert -
**O(log**_{2}?n) - Delete -
**O(log**_{2}?n)
- Space -
**O(?n)** - Search -
**O(?n)** - Insert -
**O(?n)** - Delete -
**O(?n)**
## Weight Balanced TreesThe A node of a Weight Balanced Tree has the following fields: - Key of any ordered type
- Value (Optional Field) for mappings
- Left and Right pointers to the nodes
- Size of Integer type
## Applications of Self-Balancing Binary Search TreesWe can use the Self-Balancing Binary Search Trees in a usual manner to construct and maintain order lists like priority queues. We can also use these trees for associative arrays; the key-value pairs are inserted in an ordered based on the key alone. In this capacity, the Self-Balancing Binary Search Trees have multiple pros and cons over their primary opponent, hash tables. An advantage of Self-Balancing Binary Search Trees is that they allow fast (in fact, asymptotically optimal) enumeration of the data elements in key order, which is not possible in hash tables. A Disadvantage of these trees is that their lookup algorithms get more complex when there may be numerous data elements with the same key. These trees have better worst-case look-up implementation than hash table We can use the Self-Balancing Binary Search Tree to implement any algorithm that needs mutable ordered lists in order to attain the best possible worse-case asymptotic execution. For example, suppose we try implementing the sorting operation of binary tree using a Self-Balanced Binary Search Tree. In that case, we have an easy-to-define yet asymptotically optimal Self-Balancing Binary Search Trees are flexible data structures. It is easy to extend them to record extra data or perform new operations in an efficient way. For example, we can record the number of nodes in each sub-tree having a specific property, enabling us to count the number of nodes in a specific key range with that property in ## The ConclusionIn this tutorial, we have learned about the Self-Balancing Binary Search Trees in detail. We have also discussed different types of Self-Balancing Binary Search Trees. |