















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 55
This page cannot be seen from the preview
Don't miss anything!
Pedro Ribeiro
DCC/FCUP
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!
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
The smallest element is... in the leftmost node The biggest element is... in the rightmost node
Searching for values in binary search trees:
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
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
Removing values from binary search trees:
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).
A nice visualization of search, insertion and removal can be seen in:
https://www.cs.usfca.edu/˜galles/visualization/BST.html
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
A simple strategy: reconstruct the tree once in a while
On a ”perfect” binary tree with n nodes, the height is... O(log(n))
Simple case: how to balance the following tree (between parenthesis is the height):
This operation is called a right rotation
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