Log in from a computer to take this course

You'll need to log in from a computer to start Introduction to Non-linear Data Structures in Swift. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

It turns out we are having some trouble staying on track with our programming lessons and need to devise a way to keep track of all the things we need to do, particularly what we need to do next. Why not solve this problem by creating a To-Do list tracker that will let us keep track of the upcoming (or late) tasks and also practice some data structures and algorithms at the same time!

We will implement a MinHeap class. It will keep track of our TaskNodes, a modification of our previous Node and TreeNode classes, in an array. It will also store the number of elements in the heap through a size property and whether or not the heap contains any elements with another property, isEmpty.

We will be working with the Date data type. This structure is found in the Swift package, Foundation, and will be a necessary import at the top of our file. Date represents a single point in time that is independent of any calendar or time zone. Internally it is stored as a floating-point number of seconds from 1/1/2001 at 00:00:00 UTC and will display to the console based on the current operating system (or selected) date/time format. You’ll get to see it in action as we progress.

For now, let’s begin filling out our classes!

Instructions

1.

The editor to the right contains two empty classes, MinHeap and TaskNode. Inside TaskNode create two private instance properties, as constants, task of type String and dueDate of type Date. At the very top of the file, import Foundation so that you have access to the Date structure.

Finally, add an initializer that takes in two arguments, task and dueDate of the appropriate types and sets them equal to the TaskNode instance variables of the same names. Your arguments in the initializer here should omit their labels since they will be used only internally in the MinHeap class.

2.

Inside MinHeap create a private instance variable, heap of type [TaskNode]. Then add an initializer with no parameters that initializes the heap to an empty array.

3.

We also want to keep track of the number of elements in the heap as well. To do this, we will use a computed property so that it is recomputed each time it is accessed. In this way, we ensure that we always have the most accurate size of the structure.

Create a new private computed variable size, of type Int that returns heap.count. Because this is a single line closure, you can omit the return keyword altogether.

4.

Before moving on to the next step, a quick note on abstraction:

You may have noticed that all our instance properties are set to private. This technique of only showing the essential information and hiding internal operations is one of the four pillars of Object-Oriented Programming (OOP), abstraction. While giving our variables and functions public access might make our programming life slightly easier since we never have to worry about whether or not we can access functionality, it opens our code up to exploitation from anyone else that might try to use our code maliciously. As an entry-level software engineer, you will be expected to understand these fundamentals. For a good course on these principles, check out this course, Software Design Principles. Run the code to continue.

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?