Check out a free preview of the full The Last Algorithms Course You'll Want (Part 2) course

The "M-Way Tree Structure" Lesson is part of the full, The Last Algorithms Course You'll Want (Part 2) course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen introduces the concept of M-Way search trees, which are a variation of binary trees that allow for more than two children per node. The instructor explains that M-Way search trees can hold a larger number of items without increasing the height of the tree significantly. They also mention that each node in an M-Way tree will have one more child than the number of items it holds. The instructor clarifies that the value of M determines the maximum number of children a node can have.


Transcript from the "M-Way Tree Structure" Lesson

>> So this is where things get a little bit more wild. This is where we really have to apply all of our great understanding. So, all we've been talking about are binary trees, where it's always this situation where there's zero, one, or two children. But why do we have to keep it that way?

Can't we have more children? Yeah, probably. But can we maintain the binary search mechanism where we can take advantage of fast search, but we have more splits? The answer, of course, is yes. It's called an m-way tree. Very excite. These things are pretty wild. Everyone says they're used in database indexing, and fast searching, and you can make B-trees.

We're gonna get to B-tree, but you can make B-tree lookups and all that fun stuff. But how an m-way search tree works is it follows this exact same principle, except for z may look a little bit different. So you could imagine you have a list of keys at any node.

And so I could have, say I have 10, 20, 30, so this is my root node. Now, that means every child off of 10 is going to be less than, whoopsie, that's the wrong way, is gonna be less than 10. All right, that means every child off of here is going to be greater than 10, but less than 20.

That means every child right here, same thing, is gonna be great, my goodness, it's gonna be greater than 20 and less than 30. And the exact same thing right here, which is everything right here is going to be greater than 30. That was a terrible circle. All right, look at that.

So that is the core concept behind an m-way search tree. One thing that's really nice about an m-way search tree, obviously, you can get a whole more amount of items inside of your tree without a huge amount of height. I think this inherently makes sense, right? Does anyone have any questions, by the way, on this exact item, this m-way kind of setup?

>> Does this mean that every node would have 1 plus the length of the array of children?
>> [SOUND] Precisely, so that's gonna be one of our definitions about to happen. For every item you always have, if you have x items in your m-way tree, you'll have x+1 children.

I think that inherently makes sense, because you have to be able to have sides here and sides here. Now, in a standard m-way tree, that's not necessarily true. Meaning that you don't have to have, this node just doesn't have to exist, right? A standard m-way tree has no bounds as to what you're doing.

Just like a binary tree, you don't have to have both children. You just have to have an adherence to zero, one, or two. An m-way is you have to have an adherence to zero, one, two, three, up to, depending on the m. I forget if the m specifies the keys or the amount of children.

I wanna say m specifies the amount of children, so up to m children, and then you'll have m minus 1 keys. I could be wrong on that one. Now that we got that concept down, we're gonna move on to the more exciting part. So an m-way tree has no rules about how you set it up or anything.

So I'm going to use an m-way tree and we are going to insert a few values. All right, so we'll just do 10, 20, 30, 40, 50, 69, 70. Okay, so let's just pretend we have a three-way tree. And so we'll start off by inserting these keys. So I'm gonna insert a 10 in here.

All right, awesome. Now we're gonna insert a 20. Well, 20 is larger than 10, should I make it its child? Why not, all right? Let's do the 30. Well, 30 is larger than 10. It's larger than 20. You wanna guess the problem we're currently having?
>> We're turning it into a linked list.

>> We're turning it into a linked list, and you're like, whoa, whoa, whoa, whoa, whoa, that's not how you do it. Why don't you put all the things in the same right here? Okay, okay, okay, okay, okay, calm down. We'll go 10. We'll put in 20. We'll put in 30.

We got it. We're full, so now we put in 40. Well, 40 better hang off here. All right, so now we got 40. We got 50. We got 69. Now we put in 70. Guess what? We pretty much have the exact same problem again, right? So no matter how you just have simple rules to an m-way tree, you're gonna run into the exact same problem that you did with the binary tree.

So therefore, what do you think we need for an m-way tree? A tree balancing algorithm. Let's go. Tree balancing algorithm, called it. Man, so exciting.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now