Great work on building your first non-linear data structure! The fundamentals you learned here will be instrumental in your success, not just in this course but also as you apply your skills in the practical world. Let’s go over a few of the central topics we reinforced through the lesson:

  • A tree contains nodes, specifically:
  • A root node
  • Branch nodes
  • Leaf nodes
  • Branch nodes have children
  • Leaf nodes do not have children
  • We can traverse a tree either depth-first or breadth-first
  • We can define equality by implementing the Equatable protocol and define what equal means to our classes.
  • We can change the way Swift prints a class by implementing the CustomStringConvertible protocol and defining what data to return as the description variable.

As a challenge, what other methods and functionality can you add to make your tree more functional? We thought of a few if you want to challenge yourself:

  • Modify our TreeNodes and the == method to refine equality. If you remember the Poe Family Tree, we still found a way that we could delete people by accident
  • Add a property to the tree that returns the depth, or how many levels deep, the tree is. This is useful in a number of searching and sorting algorithms.
  • Add a find() method that returns an element if it matches the search criteria
  • Add a parent property to TreeNode so you can also traverse the tree from leaf to root. Remember not all nodes have parents (the root node) so it will have to be an optional, much like in a LinkedList or similar linear structure.


Try to implement some of the challenges from above to expand your tree’s capabilities.

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?