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.

Deleting nodes: Code

Deleting nodes: Code - Python Tutorial

From the course: Python Data Structures: Trees

Start my 1-month free trial

Deleting nodes: Code

- [Instructor] In the last section, we went over the plan for how to delete nodes and repair the tree afterwards. So make sure you've watched and reasonably understand the last video because now we're going to actually be coding it up. So we're going to make a new function in the node class, and we're going to call it delete. Def delete. And that's going to take in a target, which is the target value that we're actually going to be deleting from the tree. And there are three cases here, and this will probably seem pretty familiar at this point. If self.data is equal to target, do the deletion here. Else, if self.right and target is greater than self.data, we recurse down. So, self.right.delete target. If self.left and target is less than self.data, self.left.delete target recurse down on the left side. Before we fill in this part, the part where the deletion actually happens, I wanted to take a step back for a…

Contents