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.

Challenge: Smarter automated rebalancing

Challenge: Smarter automated rebalancing - Python Tutorial

From the course: Python Data Structures: Trees

Start my 1-month free trial

Challenge: Smarter automated rebalancing

(upbeat music) - [Instructor] In the last section we created a function that would scan a tree for imbalances and then fix them. So this might be a handy thing to do after adding and deleting nodes, when there's a danger that the trees could become imbalanced. But imagine you have a very large tree with thousands or millions of nodes. Scanning every single node to rebalance a tree of this size would get prohibitively difficult, especially if you're frequently adding and removing data to and from it. So imagine, every time you add a piece of data you have to visit every node, check to see if everything's balanced, do the rebalancing, there's no way. So is there any way you can make this easier on yourself? Maybe you don't have to brute force check every node in the tree. And if you don't have to check every node, which nodes do you need to check each time you add or remove a node? So here's the challenge. Update…

Contents