From the course: Python Data Structures: Trees

Building a basic tree - Python Tutorial

From the course: Python Data Structures: Trees

Start my 1-month free trial

Building a basic tree

- [Instructor] I've spent a lot of time in the last three videos talking about trees, binary search trees and how easy they are to build and use. Now, let me prove it to you. Let's start with the node class. This is going to be the basic building block of our trees. And of course nodes aren't very useful without any data, so let's pass some data to it when it's initialized. This is going to be the number that the node represents so self.data equals data. So now we have an object, it has some data, of course nodes also have this property where they have left and right children. So let's add this pointers in there and initialize them to be none at first. Okay, great. So how do we use this class? Let's start by defining a new node and giving it some data. Now if we want to start building a tree, we can attach new nodes to the left and right of our parent node. So node.left equals node five, node.right equals node data value of 15. Note that I'm putting nodes with smaller data, in this case the five to the left and I'm putting nodes with larger data, in this case the 15 to the right. This is because we're building a binary search tree as discussed in the last section, smaller values go to the left, larger values go to the right. And if I wanted to expand the tree a little more, I could do something like this node.left.left equals node two that's going to be our smallest number in the tree. It has to be smaller than five. Node.left.right, so we need something that is smaller than 10 larger than five. Let's give it a six. Node.right.left, let's put a number between 10 in our root and 15. So how about node 13? Node.right.right, so this can be as big as we want. It has to be bigger than 15. Let's go crazy, give it 10,000. Okay, great. So if you want to see data at a particular point or node in the tree, all I have to do is this, print node.right.data. Print node.right.right.data. Okay. We can run it with Python. Great, so this printed out the 15 and the 10,000. Another thing and to note here is that we can really take any node and declare it to be its own tree. Like if we take my tree equals node.left, awesome, my tree is actually a whole new tree unto itself and node.left is the root of my tree. Trees are recursive, it's new trees all the way down. So in order to avoid confusion around what we're considering to be an official tree, maybe add some metadata or helper functions. What we can do is make a really nice simple wrapper class called class tree. And this is just going to take a root node, just like that. We might even have some simple metadata along with the root node. So maybe like a name for our tree and we can give it a default string value of an empty string. Okay, great. So now we have named trees, they have root nodes and we can instantiate it like this, equals new tree and let's pass in node and we want to call it Ryan's tree. Fantastic. Great, so let's do a little bit of testing with this, print myTree.root.left.data, print myTree.root.right.right.data. Oops, had an auto-complete there. Let's print out the name up here. And get rid of that so we're not confused about what's going on. Ryan's tree and print out a couple of nodes. There, so now we have a great little tree with just a few lines of code play around with it, get used to it and then head over to the next section where we're going to start expanding these node and tree classes.

Contents