Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

An overview of Balanced Binary Search Trees, focusing on AVL Trees and Red-Black Trees. the concepts, search, insertion, removal, and balancing strategies. AVL Trees guarantee a height invariant, while Red-Black Trees are a more common and simpler implementation.

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

myfuture
myfuture 🇺🇸

4.4

(18)

258 documents

1 / 55

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Balanced Binary Search Trees
Pedro Ribeiro
DCC/FCUP
2020/2021
Pedro Ribeiro (DCC/FCUP) Balanced Binary Search Trees 2020/2021 1 / 48
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37

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