0% found this document useful (0 votes)
42 views12 pages

Ch-3: Search Trees

Advanced Computing Concepts [COMP 8547]. University of Windsor. My notes.

Uploaded by

Rashed Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views12 pages

Ch-3: Search Trees

Advanced Computing Concepts [COMP 8547]. University of Windsor. My notes.

Uploaded by

Rashed Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Class 3 – Search Trees

Preorder (Root, Left, Right)

● Visit the root node.


● Traverse the left subtree in preorder.
● Traverse the right subtree in preorder.

Inorder (Left, Root, Right)


○ Traverse the left subtree in inorder.
○ Visit the root node.
○ Traverse the right subtree in inorder.
Postorder (Left, Right, Root)
○ Traverse the left subtree in postorder.
○ Traverse the right subtree in postorder.
○ Visit the root node.

Preorder: 3 6 5 2 4 9 8
Inorder: 5 6 2 3 9 4 8
Postorder: 5 2 6 9 8 4 3

Binary Search Tree performance


● search = height of tree
● Binary trees do not allow duplicate elements
● min height of BST = O(log n)
● max height of BST = O(n)

Avg case - O(log n)


Worst Case (search, insert, delete): O(n)

Example: visually see how binary tree works


keys = 30, 40, 10, 50, 20, 5, 35

keys = 50, 40, 35, 30, 20, 10, 5

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.

properties of AVL trees:

● Binary search tree property (left < Root < Right):


○ Every node's left child must have a key less than the node's
key, and every node's right child must have a key greater than
the node's key.
● Balance factor property:
○ The balance factor of a node is defined as the height of its right
subtree minus the height of its left subtree. The balance factor
of any node in an AVL tree must be -1, 0, or 1.

𝑏𝑓 = ℎ(𝑙) − ℎ(𝑟) = {− 1, 0, + 1}

● Minimum number of keys:


ℎ/2−1
○ 𝑛(ℎ) > 2
● Height: O(log n)
● Search is the same as BST. Insertion/Deletion is the same but
afterwards balance needs to be restored.
Example:

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

* If 2 nodes become imbalanced, which one should you perform


rotation on?
Imbalanced is caused by insertion or deletion, from there go up, the
first ancestor (closest) is the one you perform rotation on.

note: You have to balance it right after each insertion/deletion.


Red Black Tree
* We do not know how many rotations will take to fix the AVL
trees.
In case of Red-black, we can balance it with only 2 rotations.

Properties:
● The Root and leaves are always black.
● Children of a red node is black.
● All leaves have the same black depth.

* height of a red-black tree is O(log n).


* searching is faster in AVL tree as height is more strict.
* insertion/deletion is more efficient in Red-black.

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.

### Key Features


1. **Self-adjusting**: Splay trees adjust their structure every time an element is
accessed, either by insertion, deletion, or search.
2. **Amortized Efficiency**: While individual operations might take longer, the
average time complexity for a sequence of operations is O(log n).
3. **No Balance Information**: Unlike AVL or Red-Black Trees, splay trees do not
store any balance information explicitly.

### 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.

3. **Zig-Zag**: This is performed when \( x \) is a left child and its parent \( y \) is


a right child, or vice versa. Two rotations are performed.
- **Left Zig-Zag**: If \( x \) is the left child of \( y \), and \( y \) is the right child of
\( z \).
- **Right Zig-Zag**: If \( x \) is the right child of \( y \), and \( y \) is the left child
of \( z \).

#### 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

- **Locality of Reference**: Frequently accessed elements are quick to access


again, making splay trees ideal for certain types of access patterns.
- **Simplicity**: The structure is simpler compared to other balanced trees like
AVL or Red-Black Trees because it doesn't require maintaining balance
information.

### 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:

- **Cache Implementation**: Where recently accessed items are likely to be


accessed again.
- **Data Compression**: Algorithms like dynamic Huffman coding.
- **Network Routing**: Where routing tables might benefit from the self-adjusting
properties.

In summary, splay trees provide a flexible and efficient way to manage


dynamically changing data with patterns of non-uniform access, balancing the
tree on-the-fly based on actual usage.

You might also like