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.

Solution: Adding a balance indicator to the printed tree

Solution: Adding a balance indicator to the printed tree - Python Tutorial

From the course: Python Data Structures: Trees

Start my 1-month free trial

Solution: Adding a balance indicator to the printed tree

(upbeat music) - [Instructor] All right. So hopefully this challenge wasn't too difficult. It shouldn't have been as hard as writing the print function in the first place. So I figured I'd give you a bit of a rest after getting through that last chapter. So I want the fairly simple root and I just added an asterisk after each node if the node is in balance. So you can see here, we know this point is imbalanced at the root. And so an asterisk gets added after it, 75, that's another point of imbalance that turned up and that makes sense because it has two children off this side and no children off that side. And this involved writing a two string function on the node class. So it can tell whether or not itself is balanced and if it's not balanced then it returns it state as a string with an asterisk. If it is balanced, it just returns its data as a string. And this is used by the print function and this sort of…

Contents