so this tutorial will be explaining the concept of trees in python so we are going to have a tree and then we are going to represent it with code in python the reason i'm doing this is because for many data structures like tree and graphs is quite difficult to understand so i want to actually break it down by creating the visualization right here and actually writing the code and then we are going to be doing something like bfas and dfas i'm printing the notes in the tree and then we see how clay it becomes from there so the first thing i want to tell you is that this tree um could be binary tree or could not be binary tree for now let's assume that we work with a binary tree and let's go ahead to uh show all this data into this tree so let's start from g and we start with g and we have t goes here so g becomes the root of the tree and we want to impute c where does c go c is less than g so it's going to go to the left so i'm going to put c right here so this goes so b is less than g and also is less than c so it's going to be exactly here and we go to a a is less than g is less than c is less than b so a is going to be here so we're going to insert e e comes to is less than g what is greater than c so it comes here and we go to d these less than g so it goes this direction is greater than c it goes this direction is lies on e it goes this direction so what that means is that items that are less than the root goes to the left but greater than the root goes to the right so i hope you understand it and let's go to f f is less than g is greater than c and is greater than e so f goes here so i hope by now you can build up the remaining part of the tree so i'm going to simply uh complete it for you so i goes to i is greater than g because g h i so i goes here and h g h i i h is less than i so h goes here j i j is less than i so j goes here k is greater than j so it goes here all right so we build up the tree so the question now is how do we build up this century in python well this is where we need to actually understand the concept of data structures for you to create a data structure you need a different type so we are going to create a type that we are going to call nodes and a node is going to have left child right child and data so for instance if you have a node for instance we have a node like this we just grab a green paint a blue paint so if you have a node like this and it has one two and three you have this data here let's call it whatever call it a and here can be null or it can be something else or it can be anything so this is exactly what a tree looks like so this can be anything it can be a tree as well it can also be anything maybe null so we need to create a data structure that has these three attributes so let's go ahead to write the code right now in python so i'm going to create a class i'm going to call it node and we need a constructor that takes three items so i'm going to say def the constructor is in it uh that's the syntax for the constructor and we need to give it the data for that node for the root node okay all right so we are going to provide the left the right and the data or we are going to specify the left to right and the data in this case we only have the data so we're going to set the left and the right both to null so i'm going to say cell that left is equal to [Music] none [Music] and so i didn't i don't think i need a comma yes self dot right is equal to none and self dot data is equal to data so this is basically how to represent a let's do this basically how to represent a tree in python so basically these are pointers uh to the left subtree the right subtree because the nodes can actually have several trees to the left or to the right okay this is fine so how do we then insert icon actually how do we build up this tree how do we do what we did just now in code so we do need another method called inside that takes data so we need another method i'm going to call it inside the inside and listen start is going to take the data to inside so it's going to take data to inside now since we are talking of a binary tree we now need to do a bit of logic here so once you have this data to install you want to check first if there data in the root right so you want to check if there's already existing data if there's no data in the root then you simply going to set the root node here so i'm going to first say if self dot data if cell data is none so we are going to simply set the root of the tree by saying you sell the data is equal to normally set the root of the tree by saying self.data is therefore to data however if there is something there so in this case if if we have checked the root node and there is something there then we're going to check the left and the right okay so assuming we have this g here inside that g okay and we want to insert c what do we do we're going to check compare this c with g and see if it is greater or smaller if it's smaller than what is in g we simply put it as a left child if it's greater than what is in g then we put it in the in the in the right child of the of the tree so let's just do that check at this point so i'm going to say else so this else means that there is data in the root and we simply have to check the left and the right subtree so i'm going to say if data the data we want to insert is less than cell dot data so if it slice it means that we're going to insert it if it's less take note if this data we want to insert is less slice than whatever it is we're going to put it to the left so we are going to do one more if statement if cell dot left is none then we are going to set this data to be uh the the left child so i'm going to say cell dot left is equal to node and specify the data okay so what what happened here is that once we check and there is no uh item on the left child we simply set that left to the node however if we want to check and there is some item here you know we can check again is there something here and here if we continue to check what does that tell you you need to use recussion so you don't continue to check so once it's null when static but if it's not null there's something there so else there is something there we are going to recursively insert so i'm going to say cell dots left dot inside and specify the data so it's going to recursively it's going to recursively continue to check the left subtree in this case it's going to be this it's going to recursively insert here so it's going to once it has this it's going to recussion is going to take care of exploring the remaining nodes uh that are children or descendants of c okay so i'm going to say early data is greater than cell dot data in this case now i want to insert to the right right because it's greater now and we're going to do exactly the same thing with the if cell dots that's right is none okay we are going to say self.write is equal to the new node we have using the data and then we say else else it means that there's something in the right tree we are going to then recursively insert the new node so i'm going to say cell dots write dot inserts and specify the data we want to insert okay all right so this is how to build up this tree and i think everything should be fine at this time i'm going to now create a new tree and insert all this item into this space so i'm going to start with creating a root node root is equal to node uh we are going to start by saturn g g all right so these are new our root nodes so these are characters so i'm going to enclose in single quotes and the next thing we want to do is root dot inserts and we insert c and so on and so forth let me just kind of copy and paste and then change up our things a little bit so i'm gonna copy so we build up our tree at this point so this is exactly what to do to build a tree in python right so i hope this is clay now the next thing we are going to be talking about in the next class we are going to be seeing how we can do bright first such and actually print out the items of this tree in a bright force manner that is we are going to print this these days these days in a bright first manner and in the late night tutorial next following tutorial we're going to now do depth first so we're going to do these these these days we're going to do this this is this and so on so for now i'd like to thank you for viewing for viewing please remember to subscribe to my channel and leave me a comment if this has been informative for you i remain kind of the genius and i'm always there for you