From the course: Python Data Structures: Trees

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Rotating trees in Python

Rotating trees in Python - Python Tutorial

From the course: Python Data Structures: Trees

Start my 1-month free trial

Rotating trees in Python

- [Instructor] In the last section we performed some tree rotations on the whiteboard, but now let's actually write them up in Python. So here I have examples of all four unbalanced trees left, left, right, right. Left, right, and right, left. And let's start with the right rotation function. Remember that's the one that fixes the left, left unbalance. And now, unlike previous functions we're not going to write this one in the node or tree classes, although we could, but this function takes in a node object. The one that we want to perform the rotation on and returns a different node object. So the classes can use these functions, but they don't need to be inside the class. So we're going rotate right. And that's going to take in the root. And the first thing we want to do is identify our pivot. Remember that's root that left. So this 20 here, and that's our pivot. And then we want to identify the node that we…

Contents