Balanced binary search tree rotations
Key Takeaways
This video covers balanced binary search tree rotations, a crucial concept in computer science, and explores how tree rotations work to maintain a logarithmic height in proportion to the number of nodes, ensuring fast operations like insertion and deletion. The video delves into the concept of tree rotations, including right rotations, and how they are used to balance binary search trees, making them extremely efficient for various operations.
Full Transcript
hello and welcome back today I'm going to introduce probably one of the most important types of trees in computer science which are balanced binary search trees balanced binary search trees are very different from the traditional binary search tree because they not only conform to the binary search tree invariant but they are also balanced what I mean by balanced is that there is self adjusting to maintain a logarithmic height in proportion to the number of nodes they hold this is very important because it keeps operations such as insertion and deletion extremely fast because the tree is much more squashed in terms of complexity a binary search tree has average logarithmic operations which is quite good however the worst case still remains linear because the tree could degrade into a chain for some inputs one such input is a sequence of increasing numbers to avoid this linear complexity we've invented balanced and higher search trees in which the worst case is logarithmic for all operations which makes them very appealing central to how nearly all balanced binary search tree implementations keep themselves balanced is the concept of tree rotations which is going to be the main topic of this video later we'll actually look at some specific types of balanced binary search trees to see how these rotations come into play so the secret ingredient to most on steiner search tree implementations is a combination of two things one a clever tree invariant and tree rotations a tree invariant is simply a property or a rule that you impose on your tree such that it must be met at the end of every operation to ensure that the invariant is always satisfied a series of tree rotations are norm applied we'll get back to this concept of invariance in the later video so don't worry about them so much for now right now we're going to look at how tree rotations work suppose our invariant is not satisfied and to fix it we need to do a right rotation about node a assuming node a has a left child B we can perform a right rotation to put B where note a was and push node a down to become B's right child when I first learned about this in my undergrad I was mind blown literally I thought it was absurd or even to the point of being illegal that we should be allowed to do this and just move around nodes and a tree but what I realized since is that a transformation like this is totally legitimate since we're not breaking the binary search tree invariant in any way if you inspect the left tree you'll discover that in terms of ordering and placement node D is less than node B is less than E is less than a is less than C then you inspect the right tree and remark that well this is also true a bit more detail on why performing tree rotations is a valid operation first you have to remember that all balanced binary search trees are binary search trees meaning that the binary search tree invariant holds so for every node n the values in the left subtree are less than the value of n and the values and the right subtree are all greater than the value of n so in this sense it doesn't matter what the tree structure itself looks like all that fundamentally matters is that the binary search tree invariant holds this means we are free to shuffle transform or even rotate the values and nodes in our tree as we please as long as the binary search tree environment remains satisfied now let's look at how these rotations are done in great detail for simplicity since the rotations are symmetric our will only do the right rotation and you should be able to figure out the left rotation on your own first have a look at the tree on the right notice that there are directed edges pointing downwards and another node P above a which may or may not exist this is why there is a dotted line on that edge if a node a does have a parent node P then it is important that we take it into account when doing the rotation in either case we start with a pointer reference to note a this is the orange arrow then we'll want a pointer to node B after that set a x' left a pointer to point to B's right child then change B's right pointer to point to node a and we've successfully done a right rotation if we rearrange the nodes this is what we would end up with however notice that there's a slight problem if node a had a parent node then either the parents left or right pointer would still be referencing a this is problematic since B is A's successor after the rotation so we change the link to now point to B this step is usually done on the recursive call back using the return value of the right rotate function we just finished looking at the case where each node has a reference the left and the right child nodes but in some balanced binary search tree implementations it's more convenient for nodes to also have a reference to the parent node this complicates tree rotations because now instead of updating three pointers we need to update six pointers let's have a look in this case where we also have a parent link every node is in a sense doubly-linked we start off with a pointer referencing a and the first thing we'll want to do is also reference node B and node P so we don't lose them as we shuffle around pointers next we'll adjust the left subtree of a to make a x' left pointer reference B's right subtree of course throughout this example assume B is not null if you're paranoid you can add an extra if statement to check for this condition however it would be a mistake to assume B's right subtree is not null we actually have to check against this before setting B's right child's parent to reference a next let's make a the right subtree of be so set B's right pointer to reference a now make a parent pointer a reference be the last thing we need to do is adjust the reference to the parent node P so make B's parent pointer reference P and now the very last thing I promise is to make peas left or right pointer reference the successor node B notice that we need to check if P is not null because it might not exist this would be the case for the root node if we readjust the tree you will see that we correctly did a right rotation there are a lot of steps in doing this right rotation and it's a very error-prone process which is why I wanted to do it in such detail in the next video we'll look at how we can use these three rotations we just learned about to insert nodes into an AVL tree thank you for watching if you learn something please like this video and subscribe for more mathematics and computer science videos thank you
Original Description
Learn about balanced binary search tree rotations
Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: https://amzn.to/3cvMof5
A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: https://amzn.to/3wC2nix
===========================
Related videos:
AVL tree intro: https://www.youtube.com/watch?v=q4fnJZr8ztY
AVL tree insertions: https://www.youtube.com/watch?v=1QSYxIKXXP4
AVL tree removals: https://www.youtube.com/watch?v=g4y2h70D6Nk
AVL tree code: https://www.youtube.com/watch?v=tqFZzXkbbGY
AVL tree source code:
https://github.com/williamfiset/algorithms
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from WilliamFiset · WilliamFiset · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
O(N) Manacher's Algorithm with Mirror Boundary Optimization
Dev.to · Dipaditya Das
Building a Power Grid Inside Minecraft with BFS Algorithms
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Medium · Programming
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Medium · AI
🎓
Tutor Explanation
DeepCamp AI