Note: This is a draft, incomplete and in-progress.

In this article, we are going to implement a tree data structure in Python. We'll see how to use the Python Data Model so that our implementation is consistent with other data structures in Python.

PREREQUISITES

  • Familiarity with Python
  • Some Programming Knowledge

Goals

  • Implement a tree
  • Learn more about the Python Data Model

Trees

Trees are used in many areas of computer science, including operating systems, graphics, database systems, and computer networking. Tree data structures have many things in common with their botanical cousins. A tree data structure has a root, branches, and leaves. The difference between a tree in nature and a tree in computer science is that a tree data structure has its root at the top and its leaves on the bottom.

Here's an example:

graph TD 3(John) 3 --> 5(Steve) 3 --> 7(Bob) 7 --> 9(Alice) 7 --> 11(Emma) 7 --> 8(Elliot) 5 --> 13(Alice) 5 --> 15(Jenna)

Notice that all of the children of one node are independent of the children of another node. You can learn more about trees and the different types here.