 So this time around we're going to be covering trees and ultimately heaps since heaps are just a specialized tree Some of the concepts behind trees this is Going to go a bit more in-depth than lists. They are a rather sophisticated data structure. So It'll be a lot of explaining ad goes fairly simple We want to add a one you add a one at the top add a two the two gets added as a branch So unlike a list they're not chained together They do branch off With the most typical type of tree being a binary tree. So that is each node branches off into two other nodes There are others there are trinaries as Like two two three four trees and they're I've even seen Only once an unbounded tree one that could accept any number of child nodes Given what trees are normally used for you usually just see binary trees But then we want to go out of three three gets added to the other side Now one important thing with trees is generally you try to limit the number of levels And that's to help Improve on what they were created for but if you were to just continuously chain on to the To one node you you just wind up with a list anyways So the you generally try to limit the number of levels When out of four on since the first node is already filled You have to go down to the second level and find the first empty spot And then similarly to add on a five we can add that onto two since the two still has an empty node Oh, yeah, no, no, I'm getting ahead of myself. Okay. Yeah, we're fine as for deleting this one Mostly works as you'd think Until you start deleting nodes that have children So for example if you want to delete a five it's very straightforward. You just clear out that node On the other hand if you want to delete say the two You actually have to you know Store the four delete the two Move the four over and then relink now If you're just the one using the tree, you don't actually have to Mentally consider all this stuff. You just delete two and Did that's what happens? But there's also an operation called prune. Let's say you want to delete two but also delete everything that is a child of two That's what pruning is you go through remove the child nodes of it and then remove two similarly you have graphed where you can take a another tree or part of another tree and graphed it on to the existing tree Now as for different types of trees Similarly to the list there's just a lot of them and it has to deal with why Trees were really created in the first place. I will get into that bit in the use cases but there's a lot of different optimizations for trees which creates just Dozens literally dozens of different types of trees There is one I want to mention here because it behaves a little bit uniquely to the other Trees and that's a heap Heaps are a tree that Really bad definition Enforce the heat policy that is the data In the heap But it'll be easier to just show it That's kind of why these are here. So if we're adding to the heap Let's say we have a 10 You want to add on an 8 that can be added as a child just fine Add on a 5 again that can be added as a child just fine Add on a 2 that's fine add on a 6. That's fine But if we want to add a 7 to what is otherwise a max heap you May notice that each parent is A larger value than its children and each child here is Smaller than 7 so you cannot add 7 as a child Instead what has to be done is Breaking off the 5 Moving the 7 up to be the child of 10 and then making 5 the child of 7 Otherwise all the operations are the same as long as the end result still satisfies the heap principle So as for uses of trees It's a lot just like lists. They are very sophisticated data structures Which makes them useful for a lot of different purposes But you do see them very common for sorting data and that's sort of how they originated especially the heap the heap was created to implement the heap sort algorithm But they're very good for that But they are also useful for the random access of data It's quicker to get to any random spot in a tree than it is in a list and that has to do with Trees different types of trees having different Organizational schemes and you can use that organizational scheme to quickly get to any location Next up I think we're gonna do buffers I'm not sure on that one there though