JavaScript Data Structures
Transcript
Hi, I'm Beau in this video I will be going over some common data structures and algorithms in JavaScript I'll be showing you how to implement them in JavaScript So if you want to see the exact list You can check the description and I have links to all the code in the description as well. So let's get started A stack of books is a great example of the stack data structure If you make a stack of books The topmost book in the stack was the one that you put there last if you remove that book from your stack stop You would expose the book that was put there before The last book stacks are of last in first out type of service The last book you put on top of a stack would be the first book
You take off the stack. Another example of the stack is your browsers back button. If you look up here, we just opened up facebook.com So we add it to the top of the stack of sites that we've already visited previously Would you stack top push push is one of the stack methods you push Facebook on top of the stack? This middle is what it looks like when we are Visiting Facebook and then this bottom image is where we press the back button to navigate back to Twitter So we pop off the most recent URL and we just leave Twitter at the top of it The function is traditionally provided in the stack our push for placing data onto a stack pop for removing the top element of a stack peak for displaying the top element of a stack and length or size for determining how many elements are on a stack a Nice feature of JavaScript. Is that the array? object Already has all the functions we need in order to use it as a stack so you could just use an array as a stack
I will show you how to do this using an array But then I will actually create a stack class and show you how that works, too I'm going to use an array stack to find words that are palindromes a Palindrome is a word that is spelled the same forwards and backwards such as Bob b.o.b. Or racecar RAC eca are okay, so we have our letters equals an empty array and That's the stack Because remember I said arrays are have all the functions of a stack. So look at how this program works first
we're gonna set a word to be anywhere we want and we're gonna choose race car, which is a palindrome and Then we are going to have a variable which is just a empty string for our word or first word We're going to use the stack to fill up the r-word variable with the reverse of the word variable The first thing we're going to do is put the letters of the word into the stack So we have a for loop here and we're going to start at Index 0 and go to the last index of the word length
So an index in a string is just which character we're looking at so we're going to do letters which remember is our stack dot put The step function where you put something on top of the stack and we're going to push word Index I so that's just going to take This for loop is just going to take each Letter of our word because we have an X 0 and X 1 and X 2 each letters new index and push it on to the letter stack Now we're going to pop off the stack in reverse order so we can create the r-word
variable which is the reverse of the word variable another for loop and Now we are going to have the R word and we're going to add one letter at a time from the stack by popping off the top letter and Because they were put in order they're going to be popped off in reversed order Now we have a string that should be the reverse of the first word So word and our words to be reverse of each other now to check it's a palindrome We just say if our word equals equals equals word. It is a palindrome Over else it's not a palindrome. So let's run that and see what happens and Racecard is a palindrome but if we change that to Free code yeah and run that
Nope, it's not a palindrome Okay, that was just a basic usage of a stack using an array now. I will show you how to implement a stack in JavaScript so you can understand stacks a little more You probably would never do that because you can use an array as a staff, but this should possibly help you understand How stacks work a little better So here's where we're going to create the stack, which is going to be this function here We're going to have two variables this that count and this that storage now this storage is an empty object and Count is gonna keep track of how many items are in the stack So here are all the different methods We have pushed this step push is going to add a value to the end of the stack You're gonna pass end the value this that storage or storage is the empty object At the end X this that count we are going to add the value so we're gonna put the value on the top of the stack or the end of the stack and then we're just going to increment
Count up one so now it's showing that we have another item in the stack the next function We're going to remove and return the value at the end of the stack or at the top of the stack Which is the stuff top this is gonna pop an item off So if this that count equals equals zero That means there's nothing of the stack and we're going to return undefined because there is nothing in the stack once we return undefined right We're not gonna do anything it the rest of this code here because we've already returned from this function if we're popping off something We're going to have to decrement the count. So it's a count minus one So one less we're gonna set the result to this that storage which is the object for our stack and index this count which is the last item in the stack and Then we're going to delete that item and we're gonna return result. So you get the last item back
but it will be deleted off of the stack and there's just a few more methods this dot size is going to return this that count which is the number of items in the stack and then the last function is This that peak which is going to return the value at the end of the stack But it will not Remove it like pop so peek and pop are kind of similar but pop removes the item and peek does not remove the item So return this dot storage and then this that count minus one This that count would actually be they'll index one after the final item because what that's where you would add in a new item But you would have to do minus 1 to get the last item And if you're peeking you do not want to pass in the value, so let me take that bad off there So you only pass in the value when you're pushing in something Let's see how this would work down here
Okay, we are going to create a variable called my stack which is a new stack which is what we just defined up there We're gonna push one on to it to the top. Don't push two on the top so it's just a stack with one and then two we're going to Peak and we're gonna have to console dot log this because it's going to just return The top number of the staff which should be two now, we're gonna pop off to so it's going to return to again But it's gonna remove it and if we peek will she be back to one?
And before we run that I forgot these extra parentheses here Because these are functions and need the parentheses at the end of all functions. So let's try that Okay, see we peaked at two we popped off two and then when we peaked again It was one because two was was removed. Now. You can also add other things to the stack Now I'm going to push a string onto the stack and we're going to check the size then want a peak and I'm gonna pop it off anyway a peak again. So let's see what that does So we push the one we push to Repeat with show two we pop whoosh pop off, too we peaked again which showed one so there's only one thing in the stack then we pushed free code camp to string and we
Console dot log the size and now this item to the stack is just one on The bottom and then free code camp on the top We're going to peak which is going to show free coke camp now. We're going to pop Which is going to pop off and show freako camp and they're gonna peak again and we'll see that it's just one on the bottom Well, those are the basics of stack The set data structure is kind of like an array except there are no duplicate items and the values are not in any particular order the Typical use first set is to simply check for the presence of an item I'm going to show you how to implement a set function bf6 actually has a built in set object however, the built in set objects does not contain all the methods that are Common to sets so you may still have to implement part of the set yourself
Depending on what you're gonna use it for when I show you the implementation for the set I will tell you which methods are part of the es6 set and which are not so we're just gonna go through this code We're gonna call it my set. Now we could call it set but I want to make it different than the es6 set So this is my set. So here's just the collection This set is gonna be a collection of items and we're gonna store them in an array which an array can't have duplicate items But we're gonna implement this in such a way that you cannot add duplicate items to this array this method the method Has is gonna check for the presence of an element and then return true or false So you have passed in the element and it's going to do collection that index of element is Not negative one. So if the element is not in the collection, it's gonna return negative one. So if it doesn't Return negative one. It's true. So if it's not negative one, it's true. That means the
Element is in the array else. It's false. Now. We have the values This is going to return all the valleys of the set pretty straightforward. Just return the collection Now I've add we're gonna add an element to this set But first we have to check if the Elm is in the set already so we're gonna call the the method we've already defined that has method and See if the collection has the element if it does not have the element then we can add it We're gonna push that element to that Collection array and we're gonna return true else
We're gonna return false. So if we don't push an element to the collection, we're returning false Now we have removed. This is going to remove an element. So first we just check if the element is in the collection and if it is in the collection We're gonna find out what the index of the element is and then we're gonna remove it splice is means we're gonna take out We're gonna take out an element in the array starting at the index of the element and going for one element we're gonna take out one element and then we're gonna return true or We're gonna return false if that element is not in the collection Then we're just going to return the size of the collection
We're just going to turn collection that length every method we've gone through so far is in the es6 implementation of the set so the es6 set has values add Remove in size except remove is delete in the ex6 Set instead of calling remove you're gonna call delete, but all the other things are included another thing is size is not a method in the es6 set size is a Property that just means that when you're calling it You're not going to put parenthesis at after the the method
Because it's just a property so you can do set that size and sort of set that size with parentheses after it. Okay? now we're going to get into the methods that are not in the es6 implementation of the set, but are Often included in sets the next few methods actually just help you work with Sets and when you're working with two different sets So we have Union this method is going to return the union of two sets So it's gonna combine the sets but leave out any duplicates in the combination of the sets so we're gonna Call the Union method on the original set or I'm gonna pass in the other set that we want to combine We're gonna create a new set which this should be my set same what's down here because we want to make it distinct from The es6 set so we have the the Union set which is just a new set That's what we're going to combine the sets into we have the first step
Which is this stop values and the values were just calling the the values? that up here to return the collection and The second set is going to be the other set that values For a new first set for each and for each value in that set We're gonna do a union set that add to add the value then we do the same thing second set for Each and for each value in the second set we're going to add the value Remember the add method already does not add the value if it's a duplicate So the Union set now will not contain any duplicates and since for the set data structure the order doesn't matter We don't have to have the values in any particular order and then we're going to return the Union set Now we have intersection which is going to return the intersection of the two sets as a new set So we're gonna make a new set here intersection set equals new My set the first set again
We're gonna call the best values to get all the values in the first set now for each value in the first set We are going to check if the other set has the value We are going to add it and then we're going to return the intersection set So the intersection is just all the items that are in both sets And that's we're gonna return that as a new set and this next function We're also gonna have to add them my there. So this is the difference
So the difference between the sets so all the items that are in one set, but not the other set So we're gonna create a new set again the different set. And again, we're gonna get the values of the first step now This is very similar to the intersection. We're going to go through each value in the first set and If the other set does not have the value remember up here we were seeing its other set hand the value now we're gonna use the not operator the exclamation point to see if
The value is not in the set and we're going to add that to the different set then we'll return the different set and the last method we're going to talk about is the subset and This is going to test if the set is a subset of a different set. So it's going to test the first set is Completely contained within the second set. So it's just gonna return true or false so we're gonna pass in the other set we're gonna again get all the values of the first set and
we're going to call this function first set that every and what that Every method is going to do is going to test whether all the elements in the array Pass the tests implemented by the provided function. So we're gonna test if all the elements in the first set Will pass this function which is other set that has value are all the elements in the first set Are they in the second set? So we're gonna see if the first set is a subset of the second set
So that's the set data structure and let's quickly just show you some some uses of the set So we have set a as my new my set set B is new my set We're going to add to set a a look edit set B B we had to set BC or ad to set B a and we're going to add to set B D and now we're gonna see if set a is a subset of set B so is Set a is every item in set a which is actually just the letter a and set B Which it is because it's right here. Let's run that and it should say true. Let's do some other things
Okay, and this is the set a that intersection set B is going to return a new set which we are then going to call the values function so we can see all the valleys in that set and it's just a because the only value that's in both set a and set B is a But we can do a lot of the same things with the built in set I'm gonna copy all this instead of long That's my set or just going to be half set. We're gonna do set C and set D
Okay, so here's one difference with the es6 set Is that when you call the values method instead of returning an array, it's going to return an iterator So you can see objects set iterator here and then you can still iterate through all the items in the array besides that all the other Methods that a set has are very similar to the methis ette that we implemented so we can do set D delete Now this is instead of remove So we're gonna delete the a and then we're going to cancel that log and check if that D has a And we can see it's false because it's been deleted
And We can also try to add D which is already in the set Oh and there is one final thing that's different with the es6 implementation The add method is not gonna return true or false Whether the item has been added or not besides adding the item what it's going to return So whatever it was, it's gonna cancel that log is is the set itself. It's not an array of the set It's going to return the full set. So if we run that you can see here It's going to return the set which is an object And since it's an object, it's not gonna show all the different items in this set. Okay, that's the set data structure
The queue data structure is a way to hold data it's similar to a stack while a stack is first in last out a queue is first in first out an Example in real life is when you were waiting in line to buy something in a store The first person to get in the line is the first person to get to the cash register Another example is a print queue when a lot of people are printing documents at the same printer The documents are printed in the order. They were sent to the print queue in JavaScript just like a stack you can implement a queue with just an array If you want a limit and array to just the traditional queue methods, you must create it yourself Let me show you one such implementation so we have the queue right here, and we're going to have a collection that's going to collect all the items in the queue and this is just kind of a helper function to Print or it's a console that log the collection and here that the main main methods of a queue we have in queue
which is going to push the First item onto the queue and then we have DQ which is going to take an item off the queue so there's two ways to do it with an array items can go into the array at the beginning of the array or Items can go in the array at the end of the array in this implementation items are going into the array at the end of the array and Then they come off of the array at the beginning of the array to put an item onto the queue
We're just going to push that I know that element onto the queue then to DQ. We're going to use The rage IFFT dot shift just pulls off the first item of the array It removes the first item of the array and returns it another cue method is front This is just going to return what item is at the front of the array without Removing the item off of the array so we're just gonna do collection output just going to return what items at the at the zero index of the collection array and
Size we just try to figure out the size of the queue pretty straightforward just collection that length and then is empty Check if it's empty if there's not no items on the queue. So, let's see how that was gonna work here I'm just gonna uncomment that and run the code So we in cute we created a new Q then we Inc we in cute ABC So the line the end of the line is the end of the array the beginning of the line is the beginning of the array so it's gonna print a B C here and then D Q means that the
item at the beginning of the array is gonna to be removed so the a is going to be removed and then we're gonna do cute out front which I forgot to put that console.log here Okay, run that again you'll see that it's going to Check what what elements at the front of the array which is B and I'm gonna print the array again it's just me B and C because we DQ'd a another way to create a queue is a priority queue in a priority queue not only do you pass the element into the queue you also pass the
Priority of the element so if all the priorities are the same number It's going to behave just like a normal queue but when you pass in elements at different priorities The elements that are passed in with a higher priority are sent to the beginning of the queue. So all elements with priority 5 are Ahead of elements with priority 4 but if elements have the same priority, it just behaves like a normal Q So let me explain how the priority queue works first Let me show you an example of this code down here where we're using the priority queue so to create the priority queue and then we're going to in queue something and so we're gonna pass in an array the first Element in the array is the item. We want to put on to the priority queue The second thing in the array is the priority so you can see I'm not pushing them on in the same order two three one but if I run this when we that print the collection it's going to print in the order of the
Priority and just to show another example Let me add an item with the same priority as an item. We already have And if I run that You can see these two items have the same priority So they're in the queue in the order that they were pushed on to the queue So everything's the same on a priority queue except the in queue function So in the in queue function first, we're going to check if the queue is empty if it's empty You're just going to push on the element But if it's not empty you're gonna have to check the priorities to see where to put the element on so we're gonna create a variable just to
Check whether we've added the item to the queue or not And it would fall it starts at false And now we're gonna have to run through each element in the the collection or the queue to check what the priorities are So we have this floor loop that's gonna run through each item in the collection and we're gonna check is the element at index one remember the element that we pass into the queue is an array index zero is the item you want to put into the queue and Index one is the priority. So is the priority of the element we're passing into the queue less than the priority of The item in the collection that we're checking and see we're using this eye from the for loop We're gonna go through and check every item in the collection And we're going to check the index 1 which is the priority of that item And then if the priority is less than the item we're going to add That item or the element to the collection array
That's what this splice is doing and then we're gonna say add it equals true. We're gonna break out of the loop here and Then we're gonna be done except if the element hasn't been added We are going to then push the element to the array and the only thing that's slightly different Is this DQ method? And this is the way I did is kind of optional you could return the entire element with the item and the priority or you can do an idea where I just returned the Item without the priority here. That's just index 0 of this value Which is the the items that we got off the beginning of the array. Well, those are cues and priority queues
A tree data structure is a way to hold data that when visualized looks like a tree you would see in nature now This is actually what we visualized a tree data structure to look like all data points in the tree are called nodes The top of the tree is called the root node and from here It branches out into additional nodes each of which may have more child nodes and so on Nodes with branches leading to other nodes are referred to as the parent of the node of the branches that leads to the child Leaf nodes are nodes at the end of the tree that have no children also Any children of a node are parents of their own sub tree in this video We will be covering a specific type of tree called a binary search tree While the tree data structure can have any number of branches at a single node for instance C the C here? There's F G H. It has three branches at a single node a binary tree. However can only have two branches for every node
So look down here. Here's a binary tree each node. Only has two branches Also binary search trees are ordered each subtree is less than or equal to the painter node and each Right subtree is greater than or equal to the parent node Because they use the principle of binary search on average operations are able to skip about half of the tree so that each lookup Insertion or deletion takes time proportional to the logarithm of the number of items stored in the tree This is much better than the linear time required to find items by key in an unsorted array but slower than the corresponding Operations on a hash table. So let's see how this works in JavaScript here We're going to use classes to create the binary search tree basically We're going to create two classes the no class in the BST or binary search tree class The no class represents each node in the tree and there's any three data properties
We have the data which is what we're actually trying to store and we have this that left and this that right Which are going to point to the left node and the right node? So in the binary search tree, we're going to have the constructor which just creates the the root node Which is the top of the tree which it starts as null and then we're gonna have the add function So this is how we are going to add something to the tree. So we're gonna add the data We're going to get a reference to the root node. But if this is the first node node will be null in that case We're just gonna set the root node to the new note the new data We just put in so new node data So we're just gonna create a node based on that data If it's not the first node We're gonna have to figure out where to put this node in the tree to figure out where to place the new node we are going to use a Recursive function so I'm gonna create this function, which is search tree
We're gonna pass in the node which starts off as a root node If the data we pass in is less than no dot data That means we're going to put the note on the left side of the tree so if The node dot left side of the tree is null we're just going to assign node dot left to the new node And then we'll return but if no doubt left is not null. We're going to return search tree No doubt left that just means we're gonna continue Searching this is where it the recursive nature comes in It's going to run the search tree function again and continue working down the tree to find where to put the node And you can see here else if if the data is more than no data That means we're gonna put the note on the right side So if no dot write equals equals equals null then we just assign no dot right to the new node And we can return else if if the node right does not equal null. We're gonna have to keep searching
So we're gonna return search tree. No dot right so else that means data is not less than no dot data data is not More than no dot data, so they must be equal if they're equal. We're not going to add the data to the tree We're just gonna return null. So this is the search tree function And this is how we initially call the search tree function return search tree node, which starts out as a root node But then it can be called with different nodes as it's going recursively through the tree Let's say you have 50 in your tree and you have 17 in your and you want to add 23 first It's going to see that the note is not null because you have things in your tree and then it's going to run the search Tree function putting in the root node, which is 50, then we'll see if data is less than no dot data Which it is because 23 is less than 50. We're gonna go to the the node dot left if no doubt left is
Null we would put it here, but it's not because there's a 17 here. Remember we're just adding the number 23 So else if if left no doubt left does not equal null, which is true We are going to return the search tree. No dot left So we we are now gonna run this search tree function but passed in the 17 So now we're gonna see does is data less than no data Well now date is 23 but no data is 17. So this is false now We're gonna go down to this is data more than node data. Yeah 23 is more than 17 Well is no dot writeln all in this example? we're saying that 23 isn't there so no dot right would be null and Then we can just set no dot right to be the new node the next functions We're going to talk about our find min and fine max So we're just gonna be find the minimum of the array and finding the maximum of the array if you look at this binary search Tree right here You can see the minimum is all the way on the left side 9 the math is all the way on the right side
76 so just using that knowledge makes it easy to find men and find max So I'm going to set the current node to the root node. And so the minutes can get all the way on the left so While this dot Left does not equal null the current node is going to be current dot left and then at the very end It's gonna return current data. So we're gonna check this if the left side is not null We're gonna go to the next one If it's not learn all we're gonna go to this one if it's not null when we go to this one now The next is null because there's nothing to the left of 9, so we're gonna return current data We're gonna return the 9 cuz that's the data on the very left side fine Max is just the same way but the opposite we're gonna start at current which is going to this dot root Which is gonna start at the top while current dot right does not equal null Well, this does not equal null fit at 72
Then we're gonna go to the next loop current equals current dot right when we go to the next one But now current that right isn't all because there's nothing to the right of 76 so we can just return Current data now we have the find function now is present is very similar But instead of returning the node, we're just gonna return true or false whether the data is in the tree So we're starting at the top They're written out while current that means while there is a current node while current is not null We're going to do the following if data equals equals equals current that data return true That means we found it if we haven't found it we're going to see is data Less than current at data now current equals current out left. So we're gonna start searching on the left side else Well data must be more than current data So we're going to start searching on the right side
and we're going to keep searching and if we never find it if We never find that data equals equals current that data and return true That means is not in the tree and we can return false Okay, the remove function is a little more complicated than the other functions We've covered just like in the add function and their move function. There's gonna be a recursive function So we're gonna create this function here kant's remove node equals function Where we're gonna pass in the node and we're gonna pass in the data which is the data what we're trying to remove so we have this whole function here and Then here's where we're gonna call the function at the end This that root equals remove node and we're gonna pass in this that root and data We're assigning this that root to whatever is returned to this function here We're gonna pass in the root node, because you always start with the root node, and then the data that we're searching for
So, let's see how that works first of all We have to check if we have an empty tree if the node equals null then we have an empty tree and we can return Null now we're gonna see if does data equal no data So we're trying to see if we can find that data in the tree So if we've found the node with the data, this is what we're gonna do There's actually three different options either. No one has no children. That would be just like the 76 if there's no children We just completely delete that node So if no doubt left equals null and no doubt Right equals null then He's there's no children just returned know when we're returning null were setting the node that had that data to null now We're gonna check if the node just has one child if node has no left child. If no dot left equals null That'll be just like this 54 here There's a node on the right but there's no node on the Left if node outlet left equals null then We're just gonna return no dot right?
That means we're gonna replace this node with whatever is on the right, which is 67 So instead of 72 points in 54 that will be replaced with 54 ZnO dot right? Which is 67 and if there's no note on the right, we're gonna do the same thing We're gonna just return the node that's on the left to be the the new node that's being pointed to it gets more complicated When the node has two children like such as v 17 if you want to replace node 17 you can't just Put in 12 here because then what will happen to 23 is you can't just put in 23 here It isn't what will happen to 12. So let's look down here. This picture down here is kind of small Let's say we're trying to remove this 3 here that has the red X in here The way to remove this node right here would be to replace it with this node down here so if we remove 3 we can place we can replace it with 4 and then Everything will be right with the binary search tree. So if you look at what it would become
Over here. We just replaced a 4 down here with the 3 up there But how are we gonna get down to that 4 well first we have to go to the right sub node And then we have to go all the way down to the most left sub node after we've gone to the right sub node So, let's see that we're gonna create in node, which is going to be no dot, right? So in this case the temp if we're trying to delete the 3 the temp node would be no debt Right, which would be the 6 here? Well, no tip node. That left does not equal null tip node equals No dot left. That means we're gonna keep first. We're gonna go to the right of the node We're gonna delete and then we're gonna keep me gonna keep going to the left until we get to the last one on the left Side and this one just happens before there's no more to go down because you just have to go down one
But if there was more to go down to it, just keep hopping down until it got to the most left node Now we're gonna set no dot data to tip. No data. So the node is the three up here So instead of the data of this node, being three the data the note is now four because Tim no data is four now I'm gonna set node dot right to equal and now here we're gonna call the remove node function again this is where it starts becoming recursive and we're gonna pass in the Noda the node on the right and
the tip node data, and this will keep running through the function and Set up the right side of the tree Correctly we see here we were saying if data equals no data Else if data is less than no dot data that just means we have to go to the left side of the tree because it's less and Here we're gonna call we're gonna say that no doubt left equals remove node and we're gonna call this recursive function again and pass a node up left and the data and Then we're gonna return the node else That means data is more than no data when you know dot right and then call this recursive function again And no dot right data, and we're gonna return the num the node So you can see that the leaf is the most complicated one that we've covered especially when one node has two Leafs So let's look at how you use a binary search tree. At least this one that I've created so far So let's open up the console here. I'm gonna do Const BST equals new BST. I've created my binary search tree
we're gonna add four at two six one three five seven and then I'm gonna remove four and then we're going to file we're gonna console that log the min and the max Two times and I'm gonna check to see if 4 is present. Another thing we're gonna do is we're I'm gonna add in you remove Seven and we'll run that again. You can see it first. It's the minimum is one It's gonna console that log max which is seven but then we remove seven and now the max is going to be six and We're gonna see is this present is for present false note 4 is not present because we've removed in This video covered all the key methods common to a binary search tree however in a future video I'll be going over a few other things you such as finding the tree height and Traversing the tree through in order preorder and postorder traversal if you want to play around with this code You can check the link to the code in the description
Finding the tree height and tree traversal Height in a tree represents the distance from the root node to any given leaf node So if you look at this example over here the root node is nine That's height zero, but if you see four and seventeen here, that's height one three six and twenty two Height two five seven and twenty our height three, so it's the distance from the root node to the leaf nodes They're kind of like layers of a cake. And that's how you're gonna count them Different paths in a highly branched tree structure may have different heights but for a given tree There will be a minimum height and a maximum height and if the tree is balanced these values will differ at most by one So before I show you the code to implement those things I'm going to show you the use of the code We're going to go all the way down to the bottom Where we create a new binary search tree and then we add all these values with the those values
They are the same values as in the picture over here. We're going to find the men height We're going to find the map site and we're gonna check if it's balanced Let's just comment out these now. So it's only the men height is one in a console the mattes That is three, and it's not balanced The men height is the distance from the root node to the first leaf node without two children So if you look on here 17 is a root node, without two children. It has a right child, but doesn't have a left child So the minimum height you started at the root node Which is zero and then you count to the next level which is one. So the min height is one Now the max height is just the distance from the root node To whatever the the most bottom node is so five seven and twenty are all at the max height. So zero one
two Three. So the Mac side is three now this tree is not balanced because remember if a tree is balanced the values between the Min height and the max height will be at different most by one you can see that there's a missing Number here. The reason why this tree is not balanced is because there's no number here to the left of 17 But if I uncomment out this code here We're gonna add ten now ten you see when it's being added It's going to add to the left of seventeen because it's more than nine, but it's less than seventeen so the tin will fill this spot right here and Then we're gonna find the min height the max site and then check if it's balanced again
Okay. So now the min height is two and the max height is three the min height is going to be either this three or The ten that we just add that it's not showing up in the picture, but just imagine there's a 10 right here So we have 0 1 And then the min height is this level right here Because this is the first level that there are node without two children if you imagine there's a 10 right here so that's 2 and then 3 is out here right now is Balanced is true the difference between the min height and max height is at most 1 So it's going to have to be either 0 or 1 To be balanced when a tree is balanced then searching through it is much more efficient We're not going to cover this in this video But there are ways that you can make a tree Automatically balance itself when you add new items and when you delete items this creates greater
Efficiency when searching the tree. Okay. Now we're going to look at these last lines. I have to have commented out here these are ways to traverse the tree tree traversal methods can be used to explore tree data structures and basically find all the values in the tree in Depth-first search a given sub trees explored as deeply as possible before the search continues on another sub tree When I show you an example it will that will make more sense, but there's basically three ways that this can be done There's in order traversal Preorder traversal post order traversal and this last one. I'm going to talk about later this level order traversal So let me run this and then I'm going to explain it So here you look at the bottom of the console and you can see what we've logged here

Ask anything
We've read this whole study set, so questions about it get the best answers.