Don’t worry, this section won’t be on the quiz, but we wanted to give you an example of how you can take your basic Tree data structure, which only allows Strings as a data type, and convert it into a generic Tree class. In other words, a generic Tree is one that can have nodes of any data type, so long as they are all the same type in a given tree.

With little change to the code base, the Tree now supports a wide range of data types (String, Int, Float, Double, Character, etc). You can copy the code and play around with other data types as well in your own IDEs off platform. Try CGColor or Bool, what do you need to do to make those data types work?

Hint: Conform to another protocol, specifically Comparable. If you add other data types, consider how they will be printed… Do you need to add another extension of CustomStringConvertible to display useful information?

While some of these ideas may seem deep down the rabbit hole, showing that you have at least thought about these kinds of situations or have at least conceptual familiarity can help set you apart from your peers during an interview.


Take a look at the codebase on the right and see how we took our TreeNode and Tree class from String specific to generic. Play around with it here or pull it over to Playgrounds on Xcode and build upon it there.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?