DEV Community

BillyGoat12
BillyGoat12

Posted on • Updated on

default params, this, and recursion implmenation on tree

Today I will be teaching you all on how to implement default parameters, this and recursion to create a tree data structure. To start we will need to have a deep understanding of a tree database. A tree is a hierarchical data structure consisting of a node with children. The children are trees unto themselves, that is, nodes with children. So basically this is saying that when you create the tree the children are also an instance of the tree if they have children. The tree function is a constructor that creates the nodes(object) we will need to create the “children” in the tree.

Alt text of image

Since a tree is a constructor function we will need to just focus on the object we need to create an instance of first and after we can worry about implementing the methods to that object. Today we will use Functional Shared Instantiation to create a node(object) with the needed methods and properties. The properties we need in this object is value which will represent the value and children which will hold other nodes. The methods that are needed in the object are added and contained.

Alt Text

Now we can finally start implementing our methods since our constructor is returning the object we want it to. We will use the “this” keyword to access the children array in the object from the constructor, because this will reference the object being returned in our constructor. In the children array we will push a new node into that array and since our constructor function creates a new node with all the needed key/value pairs we can just push the result of the constructor function.

Alt Text

For our contain method we will be using default parameters and recursion. Default parameters are parameters that have a defaulted value if no arguments are placed in for that parameter. Recursion is recalling the function in the same function. For the function we would want it to check if something exists in the tree and when we think about it the tree is made up of nodes that were created by the tree constructor so lets start small and have our contain method only check one node. So contain should take in a value to check if that value is in the node and an node. This contain method should return true or false depending on if the value is in the node or not and if no node is is placed in as an argument we can set the default parameters to this which will refer to the node returned from the constructor function.

Alt Text

Now our function checks a node and returns true or fault if the value is the targeted-value and by default it checks the very first node in the tree. Now we can use recursion to have it do the same for all the other nodes. So the first thing we would want to do is loop over the children array in the node and have a if statement saying if the node is in any of the other nodes return true. To do this we will use recursion in our if statement and pass in each child node as the second argument and the if statement will only hit if the function returns true.

Alt Text

In conclusion this is how you will go about start and finishing creating a tree data structure with recursion, this, and default parameters.

Top comments (0)