Ch-3: Search Trees
Ch-3: Search Trees
Preorder: 3 6 5 2 4 9 8
Inorder: 5 6 2 3 9 4 8
Postorder: 5 2 6 9 8 4 3
Limitations of BST:
1. Skewed Trees: If the data inserted into the BST is sorted or nearly
sorted, the tree can become skewed, meaning it has a long chain of
nodes on one side and a short chain on the other. This can lead to
O(n) time complexity for operations instead of the ideal O(log n).
2.
AVL Trees
AVL trees, named after Adelson-Velsky and Landis, are self-balancing
binary search trees. This means they are binary search trees where the
heights of the left and right subtrees of any node differ by at most one.
This property helps to ensure that the tree remains balanced and the
search, insertion, and deletion operations have a time complexity of
O(log n) in the worst case.
𝑏𝑓 = ℎ(𝑙) − ℎ(𝑟) = {− 1, 0, + 1}
Height of a tree
● Count the height, not number of nodes.
Imbalance:
LL imb
Left of Left imbalance
LR imb
RR imb
RL imb
Special cases:
LL Roations
LR rotation
Properties:
● The Root and leaves are always black.
● Children of a red node is black.
● All leaves have the same black depth.
Insertion
1. Do a regular BST insertion.
2. New node is red.
3. Check red-black properties:
a. red node’s children are black.
b. same black-depth.
Splay tree
A splay tree is a type of self-balancing binary search tree with the unique
property that recently accessed elements are quick to access again. It was
invented by Daniel Sleator and Robert Tarjan in 1985.
### Operations
#### Splaying
The core operation of a splay tree is "splaying," which moves an accessed node
to the root of the tree through a series of tree rotations. This is done to ensure
that frequently accessed nodes are quicker to reach. There are three types of
rotations used during splaying:
1. **Zig**: This is performed when the accessed node \( x \) is a child of the root
node \( y \). A single rotation is performed.
- **Left Zig**: If \( x \) is the left child of \( y \).
- **Right Zig**: If \( x \) is the right child of \( y \).
2. **Zig-Zig**: This is performed when \( x \) and its parent \( y \) are both either
left children or right children. Two rotations are performed.
- **Left Zig-Zig**: If both \( x \) and \( y \) are left children.
- **Right Zig-Zig**: If both \( x \) and \( y \) are right children.
#### Insertion
To insert a new node:
1. Insert it like in a standard binary search tree.
2. Splay the newly inserted node to the root.
#### Deletion
To delete a node:
1. Splay the node to be deleted to the root.
2. Remove the node, which will split the tree into two subtrees.
3. Merge the two subtrees by splaying the largest node in the left subtree (or
smallest in the right subtree) to the root, and then attaching the other subtree.
#### Search
To search for a node:
1. Perform a standard binary search.
2. Splay the accessed node (or the last accessed node during the search) to the
root.
### Advantages
### Disadvantages
- **Worst-case Performance**: A single operation can take O(n) time in the worst
case.
- **Amortized Analysis**: Understanding and analyzing splay trees can be more
complex due to amortized time complexity.
### Applications
Splay trees are particularly useful in scenarios where the access pattern is
skewed, meaning some elements are accessed much more frequently than
others. Examples include: