Docsity

Balanced Binary Search Trees: AVL Trees and Red-Black Trees, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Balanced Binary Search Trees: AVL Trees and Red-Black Trees and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Balanced Binary Search Trees

Pedro Ribeiro

DCC/FCUP

Motivation

Let S be a set of ”comparable” objects/items: I (^) Let a and b be two different objects. They are ”comparable” if it is possible to say that a < b, a = b or a > b. I (^) Example: numbers, but we could have other data types (students with names and numbers, teams with points and goal-average,.. .)

A few possible problems of interest: I (^) Given a set S, determine if a certain item is in S I (^) Given a dynamic set S (that changes with insertions and removals), determine if a certain item is in S I (^) Given a dynamic set S, determine the min/max item in S I (^) Given a dynamic set S, determine the elements in a range [a, b] I (^) Sort a set S I (^)...

Binary Search Trees!

Binary Search Trees - Overview

For all nodes of tree, the following must hold: the node is bigger than all nodes in the left subtree and smaller than all nodes in the right subtree

Binary Search Trees - Example

The smallest element is... in the leftmost node The biggest element is... in the rightmost node

Binary Search Trees - Search

Searching for values in binary search trees:

Binary Search Trees - Search

Seaching for values in binary search trees:

Searching in a binary search tree (true/false to check if exists) Search(T , v ): If Null(T ) then return false Else If v < T .value then return Search(T .left child, v ) Else If v > T .value then return Search(T .right child, v ) Else return true

Binary Search Trees - Insertion

Inserting values in binary search trees:

Insertion on a binary search tree Insert(T , v ): If Null(T ) then return new Node(v) If v < T .value then T .left child = Insert(T .left child, v ) Else If v > T .value then T .right child = Insert(T .right child, v ) return T

Binary Search Trees - Removal

Removing values from binary search trees:

Binary Search Trees - Execution Time

How to characterize the execution time of each operation? I (^) All operations search for a node traversing the height of the tree

Complexity of operations in a binary search tree Let h be the height of a binary search tree T. The complexity of finding the minimum, maximum, or searching for an element, or inserting or removing an element in T is O(h).

Binary Search Trees - Visualization

A nice visualization of search, insertion and removal can be seen in:

https://www.cs.usfca.edu/˜galles/visualization/BST.html

Balancing Strategies

There are many strategies to guarantee that the complexity of the search, insertion and removal operations are better than O(n)

Balanced Trees: (height O(log n)) I (^) AVL Trees I (^) Red-Black Trees I (^) Splay Trees I (^) Treaps

Other Data Structures: I (^) Skip Lists I (^) Hash Tables I (^) Bloom Filters

Balancing Strategies

A simple strategy: reconstruct the tree once in a while

On a ”perfect” binary tree with n nodes, the height is... O(log(n))

Balancing Strategies

Simple case: how to balance the following tree (between parenthesis is the height):

This operation is called a right rotation

Balancing Strategies

The relevant rotation operations are the following: I (^) Note that we must not break the properties that turn the tree into a binary search tree

Right Rotation

Left Rotation