About this episodeIn another duo episode, Jason and Patrick give an in-depth introduction to trees, their many types, approaches and functions, and their…
In another duo episode, Jason and Patrick give an in-depth introduction to trees, their many types, approaches and functions, and their importance in modern programming. Also, peppered throughout the episode are the games, books, tools, and ideas that have currently piqued their interest.
This episode touches on the following key topics and ideas:
00:00:17 Avoiding drama at work
00:07:10 News: C++20 (7:10)
00:09:37 News: Play Co-op Diablo II in the browser
00:12:58 Wreckfest
00:15:07 Kaboom
00:17:45 The future of remote work
00:24:46 Jason’s Book of the Show: Debt: The First 5000 Years
00:27:08 fractional-reserve banking
00:31:30 DeFi, distributed finance
00:33:08 Patrick’s Book of the Show: Harry Potter and the Sorcerer's Stone, the Illustrated Edition
00:35:49 (Ad) Audible
00:37:05 Jason’s Tool of the Show: Vagrant
00:41:04 Patrick’s Tool of the Show: Zach Gage Games
00:45:03 (Ad) ConfigCat
00:46:03 feature flags
00:47:03 Trees: why are they important?
00:49:43 The divide and conquer approach
00:51:34 The agglometric approach
00:55:57 Choosing the right tree and algorithm
00:57:56 Keeping trees balanced
01:01:10 binary trees
01:02:52 binary trees and machine learning
01:05:28 b-trees
01:10:04 spatial trees: the k-d tree
01:16:50 k-d trees and multidimension
01:18:42 quadtrees and octrees
01:21:44 r-trees
Resources mentioned in this episode:
Books
- Debt: The First 5000 Years, by David Graeber https://amzn.to/3uKEoe9
- Harry Potter and the Sorcerer's Stone, The Illustrated Edition, by JK Rowling https://amzn.to/2R6ILSs
Games
- Diablo II browser game http://clouddiablo.com/
- Wreckfest https://www.thqnordic.com/games/wreckfest
- Zach Gage Games http://stfj.net/
Tools
- Vagrant https://www.vagrantup.com/
- Kaboom https://replit.com/kaboom
Articles
- Article on C++20: https://oleksandrkvl.github.io/2021/04/02/cpp-20-overview.html
- The debate over remote work: https://www.bbc.com/news/technology-56771539
Get ConfigCat: https://configcat.com/
Get Audible: http://www.audibletrial.com/programmingthrowdown
If you’ve enjoyed this podcast, you can listen to more programming news and updates like this one on Programming Throwdown’s website: https://www.programmingthrowdown.com/
You can also follow Programming Throwdown on
Facebook | Apple Podcasts | Spotify | Player.FM
You can also help support Programming Throwdown through our Patreon.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Trees
To be a pretty cool episode, another duo episode as we talked about a little earlier. We got some awesome folks—literally the company is called Awesome Pros, so check them out if you're interested for your podcast. We got some awesome folks helping us out on the editing recording side, so we're able to produce more content, which is super exciting. We've had a bunch of ideas; we have a whole list of ideas lined up of stuff that we want to chat about. Today, we're going to be talking about trees—computer trees, although there is that whole thing about planting a million trees. Have you heard about that? On
Honestly, I don't know very much about it. I mean, it sounds cool. Sounds like, you know, planting trees sounds like it's pretty, you know, universally like a good thing to do, but no, this is about computer trees. Yeah, we're going to dive really deep into that, but first, we'll talk a little bit about avoiding drama at work. So dramatic. You know, so there is, you know, a lot of sources of drama at work. There are, you know, political things; there are various sorts of social situations or like, I don't know, social phenomena. I mean, there's also just, you know, other than the macro stuff, there's also just like what we call office politics. You know, this person, you know, is in a bind, and they kind of backstab this other person. And then, and then, you know, there's all of this drama. And I'm here to tell you to avoid the drama. And we're gonna give some tips on how to avoid drama at work. You know, maybe we'll just ping-pong back and forth. I mean, my biggest thing, my biggest piece of advice that really like it's—it's actually, you kind of influence people just by being yourself. Right? And so you don't really have to go out of your way to tell people how you feel about things that are, like, besides the work you're doing. Right? So I mean, you know, just by being, you know, yourself and having opinions that you're proud of, you know, and all of that, you know, that—that's kind of, in my opinion, that's kind of enough, right? I mean, you don't then have to, like, engage in like a whole bunch of these other discussions. And so when I see, you might see a lot of things—this is true even in your public life, but especially in your professional life—like you might see a lot of things and feel compelled to say, 'Oh, like, I want to argue with this,' or 'I want to, you know, go back and forth, you know, on this, on this topic, or this issue with this person at work.' And it's really important to just kind of take a step back and say, 'You know, like, but just remember that just your kind of presence and, you know,' and the way that you kind of think about things is already being expressed just through you being yourself; that you don't also have to take that extra step.
Yeah, I think I hear what you're saying. I think what you're saying is like, by rather than explicitly having to state loudly what you think or how you want something to be, what you're saying is like, over time, sort of being consistently acting in a manner which is true to those things that you think, then you kind of express them into your work environment.
Yeah, exactly. And another thing too is, you know, there was a situation a really long time ago where, you know, someone had done something that I felt was not right. Now, of course, like if there are issues with harassment or anything like that, you know, of course, you need to speak up. I mean, that is extremely important. But I'm just saying, I mean, this wasn't anything like that. But it's something where I kind of felt really compelled to start a discussion that was probably not going to be very productive. And I decided, you know, I don't really want to get into this kind of stuff at work. And then actually, what ended up happening is—and this took a little bit; it took, it took a while, you know, it's not something that happened overnight. But eventually, that person, you know, ended up kind of losing a lot of their authority and their seniority and things at work. And the thing I realized from that experience was, you know, at that moment, I felt like there was this kind of real attack on me personally, and that I had to stand up for myself. But then, you know, in hindsight, it was obvious that this person kind of had a problem with everybody; it really had nothing to do with me. I was just one of the people that this person was interacting with. And now there is sort of a tragedy of the commons—like you could say to yourself, 'Well, if no one does anything, then this sort of toxic person might just continue being toxic forever.' And so you do have to kind of balance that out. But at the same time, I think it's so easy to go the other way and kind of get into a really big battle that is ultimately just going to take a lot of your time and energy and resources and will just not really be productive for anybody. This
Well, I mean, I think it's a—you know, actually, I saw a study that said the number two reason why CEOs get fired is actually public scandal, which, you know, the number one reason is performance. So I mean, that's kind of a no-brainer, right? People get fired for performance, especially at that high of a level. But you know, the number two reason is effectively some type of public faux pas or something. And that's huge. When you think about all the other reasons someone could get fired, I mean, they could get fired for, like, maybe they had some financial problem, like they had a financial error. And so like, you know, the SEC gets involved; like there are all these other reasons why a CEO could get terminated. But that was number two, right? And so, so yeah, I think it's a really poignant topic. And I guess the biggest thing is just to always keep a level head and kind of keep your cool. And especially, you know, at work, I think I also don't really understand why people argue political stuff over the internet. But if you're going to do it, at least do it over the internet, not at the water cooler or something.
is much more benign. Okay. No, good stuff. Good stuff. Yeah, yeah. A drama. My completely unrelated to drama is my first link is C++20. New features with example, this article that I found shows almost all of the new C++20 core features and gives a little like code snippet for how each of them works. Now, this is obviously like gonna make sense to a subset of the subset of a subset of our audience. I always find this is like, it's so useful, because to me, I always someone was showing, you know, recently, we went to C++17 at work. And although you know, that's an upgrade and still a few years old, it takes a while for these things to kind of roll out if you're not familiar with C++. And someone was like listing off the new features. And all I'm doing is like, I don't even know what that means. Like, what was it? There was like the ternary operator. And I'm like, I don't even know what the or the trigraph. We knew what the ternary operator is. But there's something like the trigraph operator is been deprecated in C++17. Like, I don't know what that is. Like, go look it up. Is that the thing with the question mark and the colon? That's the ternary operator. That one's still good. Okay. But there's a trigraph operator, which I don't even remember what it is now. It's like question mark, angle bracket, angle bracket, or something. And I didn't even know it was a thing, which is why it's been deprecated. Anyways, so seeing a list like this was super helpful for me to be able to realize like, what these named things actually meant. Because I know it means something very specific and language is very important. And the C++ standards committee, I'm sure, worked very hard to make sure it makes sense. But it doesn't make sense to me. And so seeing the code examples allows me to quickly discount 90% of them as not useful to me. I mean, it helps me to understand, you know, what these new features are. I'm not a big fan of jumping in and using all of the new features, but it's definitely helpful to kind of see them written down. So I really appreciated this article. We'll have a link in the show notes because it's on someone's GitHub page and it's really long. So I'm not going to say it out loud. But if you're used to C++, you're interested in seeing what new features are in 20, check out the show notes and definitely visit this link.
What was the version that added lambdas? That was the last big feature. 13 or 11? I think 11. Oh, it's that old? Now lambdas. And then, well, actually one that came out, it might be in 20, or it might be in 20 where it's not experimental anymore is the file system library. That was another really important one. Oh, 17. Yeah. Okay. Those are the two I remember where I was like, oh, we totally needed this for such a long time. Cool. All right. My news is play co-op Diablo 2 in the browser. So there's always this interesting kind of march towards doing just more and more exotic things in the browser. So this is, I know a while back, MAME was ported to the browser. This is using like mscripten and these other technologies like WebAssembly. And so one of the challenges though, for something like like this is, is, you know, the network stack, right? Because you can't really run a server, for example, in the browser and have other people, or maybe, maybe you can. Let me think about that. Actually, you kind of can using, using WebRTC and stuff like that, but, but it's definitely not a one-to-one mapping between, you know, these low level kind of Windows sockets or Unix sockets and what you can do in the browser. And so, you know, that's kind of been an area where you haven't seen a lot of development. But here they actually have Diablo 2. And I'm assuming they, you know, don't have the source code or anything like that. So this is, you know, just using the Diablo 2 binary, you're able to actually play in the browser and it's able to talk to a Diablo 2, you know, Battle. net server. And you can actually play with other people co-op online without even having to really do it, download anything. You just visit a website, which is pretty wild. I mean, I'll have to do a bit of research into how this actually works. My guess is maybe they hacked the like DOS box emulator. And so, and so they, they somehow wrote a translation layer where when you try to create a Windows socket, it, you know, uses, uses WebRTC, which is just pretty amazing or, or web sockets or something like that. Pretty amazing. Check it out. It's clouddiablo.com. We'll have a link in the show notes, but I thought that was pretty wild achievement there.
head, man. Yeah. I'm going to hit the big four O in I guess 10 months or something. Oh, okay. Yeah. Diablo 2 had, if I remember correctly, Diablo 2 is the one that actually had like an overland map, right? Cause the one I played was just, you were in a town and there was a dungeon and that was it. But then 2 added like a whole world, right?
Yeah. Like you walk around and then you go into dungeons. Yes. Yeah. Yeah. Yeah. The Diablo games are actually pretty fun. I enjoy them. I don't know. I, for me, there's like a whole layer of complexity, but I never really got into it as like, you know, number crunching and min-maxing your character build outs. Like I never got competitive like that. I always just sort of like wandered around and like, you know, smashed a lot of stuff. And I always thought that was fun, but I never got super, super into it, but a lot of people really did. And for me, games that allow both kinds of players are super awesome. There's a lot of recent games that people get really into that. I don't feel support casual or semi-casual play.
Yep. Yeah. I totally agree with that. You know, the games I've been getting into lately have been racing games, and I've been considering buying a steering wheel. The challenge is if you buy a steering wheel, you—you also, they all come with the pedals, and I feel like you can't really use the pedals if you're standing up, and I have a standing desk. So it's like, it's starting to spiral out of control. It's like, okay, first I need to buy a bucket seat, and then I need to buy, you
that would be awesome. Yeah. I've been playing this game Wreckfest. I just want to give a shout out to Wreckfest. They're not a sponsor or anything, but Bugbear is a company, and they made this game Wreckfest. It's really interesting. So you have 24 racers; 20 are what you'd expect—they're just in cars, they're racing around the track, et cetera. The other four are in giant heavy school buses, and their job is to go around the track backwards and wreck like the people in the top three positions. So like, so you'll be driving; it could be an oval or whatever, but like you'll be driving around the track, and all of a sudden like a school bus will just, just crush you. So it's kind of like The Blue Turtle and Mario Kart. Oh,
really, really fun. I highly recommend Wreckfest. I actually, you know, I never buy cosmetic stuff, like just buy a new skin for your car or whatever. I'd never really done that, but I actually went and bought a bunch of cosmetic stuff in Wreckfest. I don't even use it. I just, I've put so many hours into that game that I was like, these guys deserve more than the 20 bucks or whatever it was. Interesting.
I'll have to check that out. So your news article was a Diablo II in the browser talking about browser games, but on a completely different level, this was something I came across that I thought was like super cool. And one of those things, which we have said probably 500 times in the podcast, which is like, I wish this was around when I was learning to program, and that's Kaboom. And the URL is replit.com/Kaboom. And so I guess Replit is, they have a free tier, but it's also like a paid service, like an IDE in the cloud where you can, you know, run code and do development and stuff. I can't vouch for it. I haven't tried that, but this Kaboom thing is—they were able to host a JavaScript game development library. If you do nothing else, search it and watch their opening video, which is like hand-drawn MS Paint pixel art, like with just a hilarious run up, and the audio is made by them. The drawing is made by them. And it's not meant to be the next blockbuster AAA title game engine. It's literally like, 'Oh, I want to drop a character here.' And then it's like, you know, draw the sprites for your character's animation sequence. And you draw them in a little pixel editor and then, you know, draw the wall tile, and you draw the wall tile, and it manages the assets and your video game thing. And it's kind of not really like a—definitely not a no-code way of building it, but just sort of this, like right at your fingertips, like everything sort of kept right there. And I imagine for complex games, it gets out of hand really fast, but for just making super simple, like a side scrolling engine and doing ASCII descriptions of your text level, like, you know, equals signs for the walls and plus signs for where you want item pickups, and doing those kinds of things in a very simple environment, but something that really gets you into programming and allows you to make these—I think they're pretty, look pretty cool, retro nostalgic looking games. I thought this was a really cool thing: Kaboom.js. I actually wonder, this was a way to try it easily in the browser, which is how I was able to play around with it for a few minutes, but they may have a—I'm not a big JavaScript person, so I don't know how people pick up JavaScript libraries normally, but it looks like they also have—you don't need to go only on Replit. They have a way to do it. If you want to just host it yourself and try it, they have it available as well at kaboomjs.com. Oh, that's awesome.
Oh, very cool. You'll have to check this out. All right. My topic is on remote work. And so Patrick and I are both—well, everyone is working remote, but you know, at some point, offices are going to reopen, and I don't know, Patrick, if you've what your plan is or anything like that, but in my case, I'm permanently remote. So this is the real deal forever and ever.
I mean, as far as for who knows, but yeah, I mean, I'm basically a remote employee permanently. And so, you know, this whole like 'Oh, maybe remote wasn't a good idea' article is definitely scary to somebody who's really—there's no going back, but yeah, I think it's an it's a really interesting debate. And honestly, I don't have a lot of answers or insights really. I mean, I think that so far being remote has been really great. I think we might've talked about this. We talked about this. We talked with Kevin, but yeah, we both have loved being remote, but then the question is, you know, when people come back into the office, what's the hybrid solution? That's really what it is where it's maybe there's four people who are all sitting next to each other and another four people who are spread out around the globe. How do you sort of keep the social environment kind of like healthy and fair and all of that in that kind of world? And I think that's what this article is kind of positing. And I don't know if there are really any answers yet.
Yeah. I think that's going to be a hard question for people. Like what is the future look? And then the debate we were even having at work the other day is, is this a kind of one-and-done thing? Or like, is this going to slowly over time return back to normal, or like, is this going to keep creeping up? There will be at least for some while people's concern that it may come back or that you know, the next version, in Silicon Valley, I guess the whole open office, densely packed people, real estate at a super high premium is kind of very incompatible with the view that there might be reasons to not be super close to people. And so it's very interesting to see how we'll run a sort of real life experiment and the various ways of solving the problem over the next couple of years.
is one of those things that's tough, right? Like at best case, I think laws are sort of slower, slower moving, slower to catch up, which makes sense. I actually think that you know, too fast lawmaking, you might end up in a problem. And so I think what will happen is the employees in some ways will sort of express what they're comfortable or not comfortable doing. And especially if there's a like, I think Silicon Valley had gotten quite homogenous in the approach. And I think this will shake it up and allow for some experimentation to take place, which I think traditionally has been where you see a lot of innovation happen, right? And so I think we might be entering an era where you see some interesting innovation around this and a lot of people trying things and the ones that will succeed will attract people to them away from the ones that are on the other end of the spectrum.
Yeah, that makes sense. I mean, one thing that I don't know if I mentioned this on the show, but one thing I've been thinking a lot about is like, is it possible and like sort of safe from a privacy standpoint to decouple your kind of work team and your social team? Like, you know, look at WeWork, for example, right? So WeWork is this company or this idea where, you know, they have a big office and you might go and rent one cubicle in this giant office, and the person next to you and the person on the other side of you work for totally different companies. You're just sharing this like, you know, a physical space, right? And so, you know, if you wanted to go start like a ping pong club, you could do it with the people around you, even though, you know, the 12 of you work for 12 different companies. And so I wonder if that is going to if that idea is really going to fly where, you know, it's kind of have a group of people and you have a whole relationship with them, but then you also have like your work team, which is spread out throughout the whole globe. And it's sort of like two different social networks, right?
Not to get back into the avoiding trauma work you're talking about before, but I think it's been interesting. This thing you're saying a little bit reminds me of something I thought some about, which is the different way different people treat their sort of work team. And some people, the team becomes like an extension of their family, which makes sense. I mean, you used to spend, you know, 40 hours a week minimum with those people basically. And in some ways that's more awake time than, you often spend with other members of your family. And so for some people, they sort of really bring those people in and treat them as family. Other people want to maintain a separation, right? Between their work teammates and they can be sort of work friends with them, but there's a difference between a friend and a work friend, and they maintain that pretty solidly. And then of course, people all along the spectrum. And then of course, people who have no interest in sort of the social aspects at work. And I've always thought it's interesting that how a person approaches that problem impacts how the work evolves, right? So how the manager treats the team and their personal life separation probably colors how they view people on their teams treatment of work stuff. That was a really vague way of saying it. But like, if you come to work and your managers are very like 'Super, everyone's my friend, let's hang out all the time.' And you want to maintain a stronger separation that creates a bit of awkwardness. And so I actually think this thing you're saying, Jason, where like they get separated for ill or good—I'm not sure. But at least it's sort of evens out that some if people realize I'm not going to be at work all the time, or we enter a hybrid or a mostly remote work, then people really understand that separation. And it's sort of somewhat forced in, and you can still have fun with the people at work, but it's a little bit different. And I think that will even out the people who maybe felt trepidation or awkwardness with those relationships before. Yeah, that's
A really good call out. And so then as a thought experiment, what happens to the other people, like the people who, you know, always want to go golfing with the boss or something, but now the boss is, you know, a thousand miles away? Maybe, maybe people that have that personality will just won't be remote, right? They won't be remote employees. Yeah.
Yeah, yeah, it's interesting. Yeah, that's pretty wild. Yeah, I think, yeah, I'm really curious to see what's going to happen. I wonder to what degree technology can help with something like this. Yeah. I mean, it's hard to know because we're not really in that situation yet. Like it's hard to kind of project what that's going to look like and what the social feelings are going to be.
Oh man. All right, cool. So on that note, book of the show, my book of the show is Debt: The First 5,000 Years. I have to confess, I haven't read this yet. I just got it, but I'm always looking out for books on finance, finance, or just the history of finance is just super interesting to me. And somebody at work, you know, sent me a ping maybe a couple of weeks ago. And they said, 'Hey, you know, I read this book, you know, I think it's right up your alley.' And so I'm just catching up with, you know, I finished Anti-Fragile. And so I'm just kind of getting caught up with my current books and jumping into this new book. So yeah, I haven't read it yet, but it does seem really cool. I think that if you think about it, like the whole thing around debt and credit and lending is really, really fascinating. Like, you know, in my case, like I try not to have any debt if possible. I mean, there's things that, you know, there's large purchases that you have to pay over time, like you buy a house or something like that. But, on the credit card and some of these things, you know, I try not to have debt, but at the same time, I don't preload anything either. So the credit card is super, super useful because I can make a payment on something that has zero balance using something that has zero balance and then pay it later. And so, you know, I think, and then, you know, of course, like if for whatever reason I didn't pay it, well, now it's like there's debt. So it's like, you can't really have a credit card without debt. So it's kind of unavoidable if you want to have that level of convenience. And you can kind of ask yourself, I mean, there's a lot of really fundamental questions about debt that I have. And I think this book will clear a lot of them up. Like one of them is, and Patrick, tell me your take on this, but like something is backed by the government. I still don't quite understand why they could charge interest. You know what I mean? Like how can you charge interest on a loan if you're not actually taking any risk? Because, In effect, the person who's paying the loan as like a citizen of a country that's ultimately backing the loan is actually taking the risk and giving you interest. So, that's just one thing, but there's, you know, debt is really fascinating to me. And I think this book is really going to explain a lot of
So one thing is, yeah, I look at it just super objective. I don't have like a horse in the race per se. I just try to learn as much as possible. But the other thing is, what is fractional reserve? Cause you mentioned that a while back and I always meant to ask you off the air and I never got a chance. So what is fractional reserve banking? What does that mean?
So, so you kind of were bringing it up by saying the, you were admitting the realization that the bank borrows money from the government at some level before lending out money. So the naive way of viewing a bank, and I might mess it up. I'm going to try the naive way is that the bank has somebody come in and say, 'I'm going to give you a hundred dollars.' And then they say, 'Cool, we're going to pay you 0.001% interest on your a hundred dollars.' Then they go find a mortgage customer who says, 'Oh, I want to use a hundred dollars to buy a house.' And you go, 'Okay, cool. I'm going to loan out my a hundred dollars to you that I got from this other person.' And then you can use it to pay the person you're buying the house from. And then what I now have is someone who believes the a hundred dollars is at the bank, but in actuality it hasn't, it's been given to someone to buy a house and that person's making payments. And then the hope is that like when the, when the payments come in, that the person doesn't come and ask for their money back before it's there. So you can't loan out money that you don't have unless you have a way of borrowing it. So the question is how much of the amount of money that you have loaned out your obligations, your liabilities for a bank, do you need to have in reserve? And that fraction that you need is sort of governed by the government and saying like, 'You have to have this.' Now, the difference is that you can also sort of borrow money, but what happens with the whole fractional reserve banking if banks borrow money from each other or from the government and keep a small amount in reserve and say someone takes out a loan at Bank A, and then deposits it in Bank B and starts earning interest and then takes it out again and puts it somewhere else. Now, if you ask each of the banks, each of the banks will say like, 'Oh, I have a hundred dollars deposited times,' Three banks, but what if you've just gone in a circle? You actually only ever had a hundred dollars. Oh, and so fractional reserve banking is pointing out this thing that you can sort of artificially create money by not requiring that banks fully back the deposits. Oh, I
And so the government can sort of by controlling what percentage of liabilities are needed to being held and by the interest rate at which they're willing to loan new money, and do these audits, they can sort of have a, I guess not a direct, but an influence over sort of how much total money is circulating out in the environment. So by lowering the interest rates from the government, they can encourage the banks to lend out money because they can lower their rates, which are like a delta on top of the government's rate. And so they can encourage people to take loans by making interest rates lower and lower and thereby cause sort of inflation, right? By the money. There's more money appear apparent money in the system. I probably didn't get that one hundred percent right, but that's the gist.
Oh, I got it. So it's basically just going to whatever the government pays for. It could be anything. Yes. And so, and so, yeah, and so the higher the interest rates go, the more people who are going to do that. And that means there's less people who want to invest in other like private things. And so I think right now the interest rates are very low. So, and so people are always looking for things to invest in. So real estate and other things.
Yep. Okay. Yeah. That's going to get—if we keep going down this path, another few steps, it's going to get dramatic and political. Okay. All right. Let's not do that. The other thing I was going to bring up here, that's interesting. So I've—I've read the first sort of 50 pages of this book. Well, listen to the equivalent of about the first 50 pages of this book, 'cause I have this book on Audible, but I haven't made it through. It's definitely not the most exciting read, at least in the first few pages. But I also am intrigued by this concept. And it also plays into the thing that we've been a common theme here on the—on the show, which is the cryptocurrency and blockchain and this concept of DeFi, this distributed finance. And how would you allow on the blockchain people to borrow money by depositing one asset and taking money out of, you know, some sort of contract and going and using that money and then paying back that debt over time? And how would you handle that if it needed to be done all via algorithm and not by human intervention or judgment? Wow. That's
book. No, no, no. I don't think so. I'm just saying this conversation we're having about fractional reserve banking, inflationary versus deflationary politics, and the common theme of cryptocurrency leads to a very interesting topic. If anyone wants to go sort of look into that and hasn't yet—about this distributed finance, I don't think it's in the book.
in there. Oh yeah. Oh, there you go. I think crypto derivatives would be a pretty interesting thing. But, but then the thing is like, how do you do that? Because derivatives have to trade faster, presumably have to trade at higher cadence than the original thing. And, and crypto is so slow. I mean, you have to somehow make the algorithm, that whole process be much faster. My
book of the show is not well suited to audio edition in the format I'm going to recommend it, but that is Harry Potter and the Sorcerer Stone, the illustrated edition. So we've talked about before, both Jason and I have children, and my children are now old enough to sort of start introducing them to Harry Potter, and they're getting interested in reading. And so I figured to read to them, I would read Harry Potter and the Sorcerer Stone. And then I decided to go in and pick up the illustrated edition just to kind of keep it more engaging for them unless—at least until we saw how it went. And, oh man, I read Harry Potter. I don't even know how long ago. I'm like long ago, back like when they were first coming out.
But going back through it again and reading it, and then also seeing the pictures are so well done in this book. These books aren't cheap. Like I've gotten used to really cheap books. These books are just very well done though. The drawings, even the pages that don't have full color illustration have like texture to the page, and it's not just a black and white. Small knit is like, there are some places where the text is a little hard to read against the background, but just like thematic and nice to look at, and just a really nice book to read and go page by page through. I've really enjoyed that. So Harry
We're going to keep going. So if you've not, if you're interested in Harry Potter or if you have children or if you've never read it before, or if you just really like pictures, I don't think it's probably everyone who's into this. It's probably seen these before, but anyways, my recommendation is Harry Potter and the Sorcerer's Stone.
The, I mean, the book is both large in sort of height and width, and then also it's sort of normally deep. So it's a little bigger than a normal book that you would not want to carry this book to the beach and read it on the beach or whatever. Like it's a bigger book. And then, you know, also, there's, you know, so every page has like some sort of thematic texture drawing to it, if I recall, but then yeah, it's sort of every other page or every third page pair will have like some picture related to the thing off to the side or in the corner.
So I alluded to it and I actually do have Harry Potter and the Sorcerer's Stone on Audible, but it's obviously not illustrated, but it is well narrated, and it's a very pleasant way to consume the book. But if you've never tried Audible before, you can check it out and help sponsor the show by going to audibletrial.com/programmingthrowdown. And there they'll do a free one-month trial of Audible, which you can pick sort of any book that's in Audible, download it, and you get to keep that book. And then you can cancel the trial if you decide you don't want to keep it or keep going and get a book a month. I've done that for a while. You also get access to their sales, and I've built up quite a collection, but honestly, I really enjoyed it. I don't have a bookshelf at home with dozens and dozens and dozens of physical books. It's just not a thing I do anymore. But I do enjoy actually scrolling through my Audible list of books and picking out my recommendations for this show.
Yeah, very cool. And for folks who either already have an Audible account or are interested in that, they can support us on Patreon. So Patreon and Audible are how we can keep the show going and how we were able to get more content. I mean, this episode exists because of your support. And so we really appreciate that. You can reach us at patreon.com/programmingthrowdown. It's time for tools of the show. My tool of the show is Vagrant. Have you ever used Vagrant, Patrick? Or are you new to Vagrant?
Okay. So Vagrant is it's similar to Docker. We talked about Docker on an earlier episode, but it is for virtual machines. So Vagrant is a way to spin up a virtual machine and to, you know, initialize it with, you know, a set of a set of, you know, parameters and a set of scripts. So for example, I use Vagrant a lot when I'm testing new versions of Eternal Terminal. So before I release a new version, which is kind of a big process, you know, it goes to all the Debian and Fedora, like official, you know, like the package systems and it runs through a million different tests. People will like download it. You know, I want to make sure that at least like all the tests run and build and everything. And so I have a set of Vagrant scripts. And so the way this works is similar to Docker, you know, you write a Vagrant file and in that file, you say what the virtual machine should be like—should it be Ubuntu? Should it be Windows? Should it be, you know, Fedora or OpenSUSE or whichever one. And then you specify other things like how many CPUs should the virtual machine have, et cetera. And you can specify scripts. So in my case, the script goes to GitHub, grabs the latest Eternal Terminal release and builds it and runs all the tests and then kind of, you know, exits, right? And so all of that, you know, is nice because, for example, there was a GitHub issue the other day where someone said, oh, the open—I don't know how you pronounce this. It's Open S-U-S-E, Open SUSE. But that variant of Linux, you know, wasn't compiling. And I just pointed them to Vagrant. I said, 'Hey, you know, install Vagrant, run Vagrant up in this directory and you'll see it totally build to work.' And it turns out that the person who actually maintains the OpenSUSE package—who, you know, isn't me, that person, there was some error there. I don't actually know the details, but I was able to just, you know, get anybody to just go on there and run and build. It's just super convenient. Now, you might say to yourself, 'Well, if you have Docker, why do you need this?' right? And the answer is, you know, there is that layer in between Docker and the virtual machine, right? There is the whole—the Docker, like Docker Daemon, right? And so Docker tries to, you know, be really good about, you know, giving you a lot of freedom and flexibility. When you're doing things like building packages, or especially you're doing things which involve low-level networking, you know, that can give you results that aren't really natural. And sometimes you really need like a bare bones virtual machine. And so Vagrant is kind of like the Docker for that, for that kind of stuff. So if you need that, check it out. It's super, super useful, especially if you're deploying software on a bunch of different OSs. You know, I literally have just one script that I run, and I can test every OS. And when the script finishes, I know like every OS works. So it's just a one-liner. And so I'm a big fan of Vagrant. Nice. Maybe, you
So under the hood, Vagrant uses VirtualBox. I think a Vagrant is just like a scripting library for VirtualBox. That's a good way of looking at it. I see. I see. Okay. The other thing it does is it maps like a folder on your computer to a virtual machine. So if I do like Vagrant up in a certain folder, or with a certain Vagrant file, it has a UID for that file. And if the machine already exists in VirtualBox, then it won't have to like totally destroy it and bring it back up and stuff.
like that. Nice. My tool of the show, I actually had one, and then I switched it to a collection because indecisiveness. I was playing a new game by Zach Gage Games, and that was Good Sudoku. So I played Sudoku for a while, like off and on in books, but I never really got into it. It just didn't feel the same. Like I couldn't use my pencil to kind of make notes on iPhone or, you know, on an Android phone. I use an iPhone, but I'm sure it works the same. And so, you know, I kind of never really did it, got into it on mobile. But then I tried this Good Sudoku, and I didn't realize it was by Zach Gage Games, but I tried it, and they just like the kind of UI they built up around playing Sudoku. It's not exactly the same as how I would do it on pencil and paper. It plays a little faster, but that's actually what I want. And I just really thought it was very well done. And then I realized, like I was searching this, you know, Zach Gage Games is the maker of Good Sudoku. And then I'm like, oh, I actually like a lot of these games. So they're the people that made Ridiculous Fishing, really bad chess. So Ridiculous Fishing is like you're a dude on a fishing boat, and then you use like the motion sensor in your phone to drop a line down into the depths of the ocean, catch a fish, and then sort of reel it back in. It's ridiculous. Did you reach the end of that game? No. Uh-uh.
Pretty good. Oh, okay. So really bad chess is, you know, I know Jason is like a master chess person, so we're not going to ignore him for a second. But I'm really bad at chess, actually. So in this game, they don't even try to make an attempt that the sides white and black are even. It does effectively randomly place chess pieces on your side of the board. So you might have like five Queens, and the other person has all pawns, or something really good, and one King, or something, you know, and then you try to play. So the expectation is just to like be a little more goofy and a little free and make it not have to be this super serious chess game. Anyway, so if you've never checked out any of these games—Good Sudoku, Really Bad Chess, Ridiculous Fishing—some are available on iOS, some on the Play Store, go check them out. They're actually this, I didn't know anything. I need to go learn more about this company. But it turns out right before the show, just doing research, it turns out actually really into these games. Now I'm going to follow them and watch for new ones to
You know, I played it once, and I don't do online playing. So I actually don't know if you—I'm sure there's probably like this based on it. There's a way to do it multiplayer. It's just for me, I never always feels embarrassing, even though it's anonymous. Like, I don't want to lose to real people. I don't care about losing to the computer.
But you know, when we—when we first moved, my family first moved to the West Coast, I actually set up a meetup group called Sports for People Who Are Really Bad at Sports. Okay? And it was great, because you know, you kind of self-select for people who aren't going to take it real seriously. And it actually—I was worried that like somebody would see that title and be really good at ping pong, and then just kind of embarrass us and like have to deal with it. But no, actually, like everyone who showed up genuinely was really bad at sports. And we all had a really fun time.
Hey, everybody, we have a new sponsor, and that is ConfigCat. And I'm going to tell you a little bit about what ConfigCat is. It is a feature flag service. You can easily use flags in your code with ConfigCat libraries for Python, nine other platforms, toggle your feature flags visually on the visual dashboard, hide or expose features in your application without redeploying your code, set targeting rules to allow you to control who has access to those new features. ConfigCat allows you to get the features out faster, test in production and do easy rollbacks. With ConfigCat's simple API and clear documentation, you'll have initial proof of concept up and running in just minutes, train new team members in minutes easily. So you don't have to pay extra for growing teams. With a simple UI, the whole team can use it effectively. Whether you're an individual or a team, you can try it out with the forever free plan, release your features faster and with less risk with ConfigCat. Check them out today at configcat.com.
Cool. Yeah. For people who don't know what feature flags are, it's—you know, imagine you have something out in production and you want to change some kind of behavior. So you might say, you know, now use this other database, but then you do that and then it broken and now you've broken it for everybody. And that's—that's kind of a nightmare. So what you want to do instead is you want to try something out on like a few people and then slowly kind of add more and more and more people. And one way to do that is to have a feature flag, have a flag says, if, you know, this is part of my test group of people, then switch to the new database. Otherwise use the old database. And slowly like that, if becomes true for more and more people. And so ConfigCat is a service that kind of handles a lot of that for you. So they can handle deciding who gets the feature and who doesn't. And then you can go in their UI and change all of that and slowly, slowly get it to a hundred percent. And they take a lot of that burden off you. So check them out. They're great. All right. So we could jump into trees. Why are trees important? Why are trees important? So just a quick story, you know, a lot of search indexes and e-commerce sites. So if you go to Amazon or you go to Google or you go to any of these websites, you know, they need to serve content to you really quickly. And the way that you can do that, especially in a place where you're adding a lot of content—very dynamic—you're going to be using hash tables, which we'll talk about another time, and trees, and especially trees. And so for this reason, you could say money grows on trees. If you work for Amazon, money literally grows on B-trees. And so that's why it's so important. If you're going to be doing queries, insertions, you're going to be doing partitioning of data. You are going to use trees a lot, and they are extremely important. You're going to—they're going to come up all the time.
I think it's also one of the like first things people learn in computer science, because I feel, and you get this, like if you look on the internet, you get the—why if I interview at X tech company, do they ask me about binary trees or about linked lists? Or—you know, I somewhat hear, and there's—I don't want to get into the drama or the politics or whatever around all of that. But what I'll say is that, you know, being able to think through trees and their trade-offs and so it's just like it's part of the problem solving, even if like Jason and I were talking before. So some of these trees we're going to talk about, I definitely use and use them all the time, but the sort of basic vanilla binary tree that most people are introduced to first, like I actually have trouble coming up with exactly how I would use that one. And it's not clear that it has a ton of use, but as a learning tool or reasoning tool or thinking tool, or like a minimum backstop, like I got to at least do better than this. It actually serves a really good
purpose. Yeah. Totally. I think that—I mean, the thing that trees teach people at a fundamental level is the idea of sort of divide and conquer and also like algorithmic thinking. So we'll jump into like how trees are built, hopefully you can use that to kind of motivate why they're so important to know about. So, you know, imagine if you have, you know, some set of numbers and you need to put this set of numbers into a tree, and we'll get into why you'd want to do that later. But imagine, you know, your task is sort of doing this. You have this binary tree and you want to—you want to fill it full of these numbers and you want the tree to be pretty balanced and stuff like that. So there's two kinds of ways to do this. One way is a sort of divide and conquer. So you can imagine saying, well, what I'll do is I'll start with all my numbers. So I kind of in a way have like a single node that just has my entire set of numbers in it. And then what I'll do is I'll just split the set into two halves. You know, one half is the bottom 50% of numbers, right? The bottom half of the numbers, if I was to order them, like maybe you'll sort them and then cut that sorted list in half and then say, you know, I'll send that half down the left side of the tree and I'll send the bigger half down the right side of the tree. And maybe I'll take the node that's right in the on the median and make that my node. So, you know, imagine like you have 20 as the root of the tree and everything to the right at 20 is bigger. Everything left at 20 is smaller. And so now you can repeat that process again on the left and right side, right? You have a little less than half the size of your original set, but that process stays the same. And if you were to just go to that—that left branch that has those, that half of numbers and just look at it in isolation, you actually have the same problem as you did in the very beginning, just with fewer numbers. And so trees are a really good way to explain recursion and to, you know, deal with all of that phenomena and to cover a lot of other phenomena in computer science, right? So, so trees are kind of a, you could even think of like the call graph when you're doing recursion and we're doing computation as, as sort of this, this tree, like the history of all the functions you've called. So, so yeah, trees are kind of ubiquitous. And so, you know, it kind of like introduces you to a lot of other areas of computer science. Now, another way to build trees is what we call agglomerative or bottom up. Well, to do the same thing in like an agglomerative way, you might say, I'm going to take all my numbers, I'm going to sort them, and then I'm going to group them into, you know, 16 different groups. And then for each of those groups, I'm going to, you know, pick a number out and put it and call it like the leaf node of that group. And then I'm going to, you know, do that and then kind of go bottom up and then start kind of merging some of these mini trees together. So now it's like, I have a bunch of these smaller trees, I'm going to kind of merge them, merge them, merge them. And then when I'm done, I have one big tree. And that's kind of the agglomerative way of, of assembling these trees. And so, and so, you know, that is also, you know, if you look at, for example, Merge Sort or something like that, you actually see kind of both of these things, right? So in Merge Sort, you start by kind of splitting a list into halves and then having it, having it, having it, just the successive halving algorithm. And then at some point you reach a point where it's just all just lists of size one, and then you start kind of merging, merging, merging. And so, you know, Merge Sort actually has both, you know, a divide and conquer approach and then an agglomerative approach to kind of bring everything back together. But when it comes back together, it comes back sorted.
Yeah. Yeah. So, so it's, so then, then you have this question of like, how do you go about doing that divide, right? Like if it's, if it's small, you know, like for example, you know, you have a hundred items and you're just putting it into some Binary Tree. Well, then you can, you can kind of what we talked about, sort it and then split it. And you'll have this like perfectly balanced tree and it'll look really nice. But in practice, you're dealing with like massive, massive data sets. And so you can't, you can't really, you can't really do that. It doesn't really make sense. Or you might even have things that are multi kind of modal or multi objective. So there might be different ways to split data. You know, maybe you have a list of people and those people have, you know, ages, they have genders, they have other characteristics, and you want to have some Binary Tree that says, you know, that ultimately like results in different equally sized buckets of people. So your Binary Tree might say something like, well, let's first look at everyone who's over 18 is on one side and everyone who's under 18 is on the other side. And it's everyone who has been to my website in the past day is on one side. And so in this kind of decision tree, at the end of all of these decisions, at the end of asking all of these questions, you now have this, you know, end state, which hopefully doesn't have too many people in it. And so, you know, you know, whether you're doing a decision tree like that, or you're doing just a, you know, a sorted tree, but of a really large data set, you can't try every possible way to construct that tree. It's just too expensive. I think it's like NP-hard, like coming up with the best possible decision tree. And so that's where you end up with trying to do some of these approximations. So you might try, you know, at every node in the tree, you might try 50 or 100 different ways of splitting it. Now there might be thousands, but you're just going to try 50. And because you're trying 50, you might not, you know, get the best way of splitting, but hopefully the best one out of 50 is decent. And then when you go to all these children that you've created from the split, you're going to do this again, and you're going to pick 50 at random, a different 50 ways of splitting. And if there was a really good way of splitting that you missed, hopefully like you catch it, you know, at the next level. And so this is how a lot of these decision trees or Binary Trees over a really large data sets are kind of assembled. There have been a lot of other like search based approaches, but it's actually really hard to beat the just a greedy way of doing it.
So to back it off of back to the kind of like the basic stuff, I think Jason was already jumping ahead that we, or at least me, I tend to think of trees as storing like strictly orderable things in the data, like numbers, or strings. And one of the other things to point out is that you may not be exposed to all of them, all up front. So if you are experiencing them over time, having these things that Jason's talking about, and figure out how you split, how do you insert new data, or remove data or modify data, plays a part too, and what tree and algorithm you might select. The other thing that I that has come into play for me is that I often think about first about trees as being sort of no individual allocated nodes with pointers between them. I work in C++. So that's how I think, but they don't have to be actually, in fact, in some cases, in many cases, it may be better served to have an array of all of your data, and then order the stuff in the array where like the zero item in the array is a root node. And then the, you know, first and second are the children nodes. And then you can do sort of what level are you on? And where in the level are you? And then you can do the visitation of the nodes and the search by simply going to the right place in the array. And that has efficiencies for keeping your data, you know, more close to each other more local, and that's more cache efficient. And we're going to get into some other things where that plays a role as well. But I think it starts off when you're first introduced trees, as in my experience, like it's very rigid, they're always Binary Trees. In fact, they're normally always Binary Search Trees, and you always divide them up in a certain way. But actually, the sort of body of all kinds of trees that are used in computer sciences is quite big. And they often share interesting similarities, but also very interesting differences. Yeah, yeah, totally
makes sense. Yeah, a lot of people have invented different trees for different specific use cases. And a lot of it comes down to, you know, being able to split the data in a way that more often than not splits evenly, right? Because what you don't want is, and you'll hear this like trees that are balanced or unbalanced, right? So imagine if, imagine if you have a Binary Search Tree over numbers, but it's completely unbalanced. So the root node has, let's say, you'll say you have the numbers one to 100. The root node has a one, it has just one child to the right, that's a two. And you just do this all the way to 100. Now you actually have a linked list, right? They used a tree idea, but you ended up with a linked list. And you have all the performance problems of a list, right? If you were expecting something to be really, really fast, you're not going to get that, you're not going to meet that expectation, right? And so you want these trees to be balanced, but you also, as we talked about before, like, you can't spend a lot of time building it because you're dealing with such large volume, right? So you can't look at all the items, do many, many passes. And so a lot of these structures that we'll get into have really nice kind of like a really nice propensity to just naturally break data down into ways that are even, you know, for a specific type of
yeah, it's a really good point. So in addition to like choosing the right tree, and people are inventing new types of trees all the time, you know, no matter what you do, especially if data is coming in and going out, you know, the trees will become unbalanced, either when you're building it or when you're, you know, modifying it. And so there is, you know, there are special kind of trees called AVL trees and Red-Black trees. And for things like B trees, there are ways to rebalance them. But as Patrick said, I mean, you know, definitely take a look at if you search for any of these trees on Wikipedia or other places, you'll see really nice kind of visuals of how they're rebalanced. But I remember from, from, you know, undergrad, rebalancing trees is one of the hardest things that I have to implement. And I just remember having so many issues because you have to kind of project yourself into different parts of the tree and like think, okay, at this point of the tree, you know, I, I am like, like I'm at this node in the tree, but because I'm in the middle of this rebalancing, I know this, this and this. And so I have to do that. And so that, that stuff gets really, really difficult. So yeah, I think in general, you know, if you're going to use trees, try to find a library on the web, you know, definitely take the time to understand how they work. But if you're going to put something into production, there's, there should be a really good implementation of a variety of different trees that are, that's publicly available. Looking at Binary Trees specifically, you know, so, you know, a Binary Tree is one where you only have two children. And so you might say to yourself, well, like, why would I, why would I, you know, have that restriction, right? Like what's, what's, why would I limit myself? Right. But there is one thing that binary trees can do really, really well. And that is like upper or lower bound search. Right. So for example, imagine if you have a set of numbers and you're asking the question, you know, what is the biggest number in my set? You know, less than 30, right? That question requires you to, to do kind of an in order search. So you have to kind of look for 30. If you find 30, well, I guess I said less than so. So yeah, even if you find 30, you still have to kind of go backwards and find the biggest number less than 30. So you have to kind of march in sorted order through that structure. Right. And a Binary Tree is really nice because since there's only two children, you know, that the child to the left is smaller and assuming there's no duplicates in the tree, the child to the left is smaller and the child to the right is bigger. And that's always true. And, and you can also do this trick where if you take any point in the tree, if you take the child to the left and then keep going to the right, as many times as you can, you're going to get the number that is smaller than the one you're currently at, but bigger than, than all the other ones. And so that's, that's a unique feature to Binary Trees. And I think that is one of the biggest reasons. Another reason why people use Binary Trees is in machine learning. We talked about there's in machine learning, you're typically not using a tree to store a lot of numbers or something like that. You know, you're using a tree to the structure of the tree to store decisions. And so at the end of you got the, the, the leaf or the end of a Binary Tree, if you're searched through it, you have kind of a list of constraints and you can use that as a, as a way to learn things. Right. So in that case, it's actually pretty difficult to split, um, multiple ways, depending on what kind of decision you're making. So it's, it's decisions like, uh, you know, this number should be greater than X or, you know, this binary number should be set to true. Like these are all kind of binary kind of decisions like thresholds and binary operators. Um, you know, there might be times where you have some kind of categorical thing. So, you know, day of the week, it could be seven choices and you should have seven children, but, but even in those cases, it's, it's kind of hard to calculate whether that is a good decision to put in your tree or not. Versus if you have a binary yes, no decision, it's pretty easy to calculate. Yes. I want to take that decision and put it here in the tree. And so for that reason, a lot of machine learning will use, will use Binary Trees, um, when they're making these decision trees. Um, you'll also hear in machine learning the term Random Forest at the time I thought random forest was this really complicated thing because I'd heard, you know, where it was used. And so I thought, Oh, like one of these days, I really want to learn Random Forest. Um, random forest is very simple. All it is, is if you have a big data set and you're planning on building a decision tree, instead, what you can do is you can split that data set, you know, N different ways and build N different decision trees. And, you know, because of that randomness I talked earlier about where, where you're randomly trying different splits, even if the, the, the, the distributions of the data or the type of the data is the same in each of those groups, you're still going to end up with different decision trees. Um, and then once you have all of these decision trees, when you want to make a decision in the future, you can actually ask all of them and then you can either do some kind of voting or you can take the average or what have
So Jason already spoiled our next topic, but if binary trees have two children at each node, if you generalize that and instead allow sort of more children, there are a variety of things you can come up with, but one of them is a B-tree. And so B-trees say that each node can, and there's some variance here, but basically each node can contain multiple values. And then the child, however many values it has, you have that many children plus one. So let's say you had two values stored there instead of just one, then things before the lowest value, you keep them sorted. And then the thing below the lowest value, you would have reference to the next child there between the two values. You would have a child to the right, the bigger, the biggest, larger than the tube of the bigger, you would have a child to your right. So in that case, you had two values in the node and three children, and you could see this expanding to various sizes: four, five, six, seven. And there's a lot of varieties and some techniques for how you keep them balanced. But the interesting thing about here is when you visit a node, rather than simply making a single decision—do I use this value? Do I go to left or right?—you sort of can consult multiple values. And then also you make, by doing that, you make your tree shallower. So you don't have as many levels of children as you would if you used a binary tree because you're putting more data in the node. Now, the other thing that I already hinted at as well where this starts to matter: and this is one of those things where when I'm doing interviews with people and we do some sort of questions and we're working through a problem, and then I'll often ask them, 'You know, what is what is sort of the runtime?' And they start scratching their head. And this is different for everyone, but I always express to them, 'Look, I'm actually not that' interested in sort of the academic asymptotic runtime because it turns out like in my life most of the time you're dominated by other factors. So I'm looking for you to just express to me like which parts of this algorithm about how many times would this take, where are your bottlenecks? And so one of the things that B-trees really excel at is if you imagine, which was true maybe a lot more true five, 10 years ago than is true today. But if you go back 10 years, RAM was really small, like CPU caches weren't really like they are today. And things were stored on sectors on hard drives. And if you were going to go access something on the hard drive, the hard drive was going to spin up, move the little arm out to the right spot on the moving piece of metal with magnetized bits on it and read in a whole bunch of data in a sector and bring that into memory. And that was an extremely slow thing. And so if you were going to do an on-disk search for data, you want to wanted to bring in sort of as much in one go as you can. And if you imagine a binary tree where at each block you have one, and then the next block is who knows where—each time you go to the next node, you are spending a lot of time waiting to get the next node into memory or getting into the CPU. And these B-trees became very valuable by instead saying we're going to store a whole bunch of data in the sector. And then we're going to analyze all that data, increase our chances at any given node of finding ours, or at least cutting down the search set dramatically. So the number of different nodes we have to visit is as small as possible. So for things like file system and file system metadata for things like databases and indexes and databases, like these B-trees get used heavily because you can tune the number of children and you can do sort of more work per node visitation. And if node visitation is a dominating factor, then you would want to choose a B-tree. And then there's techniques for making sure that you keep all the data at a certain spot that you know how to keep it ordered and organized and well balanced. And so there's a whole body of work around B-trees, but it's enormously valuable in this sort of broadening the scope out from just, you know, one value and two children to allowing a more general form and realizing that that may play better with an actual computer architecture. Yeah, really.
Well said. Yeah, that really covers it. And so, yeah, as Patrick said, I mean, pretty much any database that you look at is going to have a B-tree implementation in it. I mean, all of them, they all use B-trees in some way, shape, or form. So now we'll move on to spatial trees. And as we motivated earlier, know, spatial trees often aren't functionally that different or in terms of their idea, but they're just taking advantage of some domain knowledge. In this case, the fact that it's operating over some space or some field, right? And so the simplest one of these that I know of is a KD-tree. I don't actually know where the name KD came from, but effectively it's a binary tree where every split is done on a single axis. And so if you imagine like picture in your mind, you know, a graph, like just with an X and a Y, right? You're graphing some function. If you just have a line, right, you can describe a line just by looking at one of the two letters. So if you look at just a horizontal or a vertical line, you can do that just by looking at one of the two variables, right? So if I have a line like Y equals X or a parabola, like Y equals X squared, I need to be thinking about Y and X at the same time because they kind of relate to each other. So Y equals X squared, know, as X is increasing, Y is really increasing. And so you have this like parabola, right? This line that just shoots upwards, right? But if I have a just a vertical line, that's just like X equals two. And I want all the Y's, right? That's a vertical line. Or if I have a horizontal line, that would be, you know, like Y equals four. And so, and I just want all the X's that are all points on that line. So vertical and horizontal lines are kind of the easiest to express, right? And so KD-tree uses this to have a very simple division of the space. So what a KD-tree will do when you're building one is you'll look over all the points, and let's just keep it at two dimensions, just to keep it simple. So you look over at all these points and then you'll calculate the spread. In other words, you calculate the difference between the smallest point and the biggest point on that dimension. So if you were imagining like a bunch of points, but they're all kind of around the same height, but there are some that are really far to the left, some that are really far to the right. You have this really large horizontal spread, but not a very large vertical spread. And so whichever dimension has the largest spread, that one's going to be chosen as your split dimension. So you're going to choose that dimension, and then you're going to split it right in the middle. Now when I say in the middle, I don't mean like in the middle of the extremes, but you're going to split it such that half the points go on the left and half the points go on the right. And so while you're computing the spread, you can also be finding that middle, finding that median point. So if you imagine the root node, the root node is going to look at your entire data set, find which dimension has the largest spread, and then slice that dimension such that half the points go to one child and half the points go to the other child. And then all that root node needs to keep track of is which dimension it was split on and where in that dimension the split was. Now, then each of the children will do the same thing. And so you'll keep doing this until you get to a node where there aren't that many children. At that point, you'll say, 'Well, there's no point in me splitting again. I'm just going to' have like just a list of points at this, at that level. And so KD-trees are super, super important because there's a really, really cool trick you could do with them. In machine learning, you have this challenge, right? Where let's say you're Amazon, right, and someone types in 'batteries.' And so you need to return back a list of items that you think that person would be interested in, right? But the problem is Amazon has like a billion items, right? Or maybe even if you take it even more extreme, let's say someone just goes to amazon.com, they don't even put a search. So you need to show them some items you think they would like. And there's just so many items, probably a billion SKUs in Amazon's catalog, right? So you can do this trick. Let's say you had a vector—a vector, just like a set of numbers—let's say you had a set of numbers to describe a person, right? And so, you know, that might be sort of like some way of capturing like their history and the things you think they like. And you were to sort of what's called embed that or create just like a single point for that person. And then you also do this for all your different products. So for all your different products, you have kind of like a point or a vector, a list of numbers or a point in a space somewhere describing that product. So now what you need to do is something called nearest neighbors. So you need to say like, 'What are the nearest products, the nearest points, product points to this person point that I've created?' And it turns out if you use a KD-tree, you can do that very, very, very, very quickly. Things like nearest neighbor search can happen extremely quickly in KD-trees because for example, look at that first split—that first split might be like so far away from where the person point is that you could say anything on the other side of that split, there's no way that any of those points are my nearest neighbor. And so that's right away, that's half the data you don't have to look at. Right? And so you can kind of take that sort of intuition that basically, you know, you can kind of follow the KD-tree down to the final part of the KD-tree that has your person in it, your person point in it, and look at the products there. And then maybe look at like some of the products higher up in the tree, but you definitely don't have to look at the entire tree. You can ignore 99% of the tree. And so this is how Amazon does at the end of the day, this is how they can show you stuff so quickly even though they have so many items. And so there's a lot of work that goes into like the embeddings and how you create the tree and everything, but ostensibly, know, they just have a giant KD-tree that they've implemented. And when you come to Amazon, they find the nearest things to you in that tree. That's how it works.
So to highlight one difference and then talk about one final thing. I think when Jason is describing like this nearest neighbor search on multi-dimensions, which we sort of call these spatial trees because that tends to be one way of thinking about it. But when we're talking up front about B-trees and binary trees, we're talking about a single number line, basically, where all of your elements live on. And when we talk about multi-dimensional, it's having at least two or more dimensions. And the problem there becomes, there isn't strict way of viewing like I'm just going to go in dimension one and then dimension two, like that turns out to be really inefficient. So both Katie, Jason thinks now I'm going to talk about and some others try to divvy up the space in a way that moves back and forth between the dimensions. And as Jason was saying, using the spread, you might divide on the X line, the X line, but then you'll go by the Y line, right? And you're sort of intermixing the two dimensions and trying to sort of interleave so that you can get to your answer a little bit faster. And when you're traversing, the other interesting thing is, you may end up having to go through multiple branches in order to find the nearest neighbor. So once you go down one branch, know how far away the split was from you. And if your answer ends up being further away than that split was, you actually have to go visit the stuff on the other side of that split. And that's true as well for a variety of these spatial trees and doing that nearest neighbor search. And the other thing that both that his KD-tree explanation and my—oh man, I guess I did this out of order. Anyways, the thing I'm about to talk about also support, which is very similar, is for clearing ranges, or if you think about in 2D, it would be rectangles or boxes, right? So if you drew a rectangle and said, 'I want everything inside of this rectangle,' they need to be efficient at doing that. And it's a little hard to describe over audio. But I mean, both of them are well suited to being able to do that. So Jason talked about splitting the data on one of the axes at each level. So instead of looking at your data and sort of splitting based on what the data says, instead, if you come up with predefined splits based on your space, you come up with a different spatial tree called a Quadtree for two dimensions or an Octree for three dimensions. I don't know what it would be for four dimensions. I've never heard of someone doing that, but maybe you could. And so if you imagine in two dimensions having, let's just say a square, right? And you divide that square once in the middle up and down, and once in the middle left and right, then you end up with four quadrants of that space. And now you see why it's called a Quadtree. So the root node would be the base sort of square, then the first set of children there would be four, and one would be the upper left, one would be the upper right, one would be the bottom left, and one would be the bottom right. And what that does is that sort of dimension mixing I was talking about—you get it, right? So as you go, know child 0, 1, 2, 3, you're actually traversing both dimensions sort of intermixed. But the Quadtree, what it does is say, 'If I have a set of points scattered around in my square, I'm going to keep subdividing my space until I get, you could say, just one thing in each square.' So the disadvantage is that as you're cutting down and down and down, if you have two points really close to each other, you might need a lot of splits before you get to the split that separates the two. And so you could have a lot of depth, right? But in traversing, it actually ends up working really similar to how the KD-tree does. The advantage of a Quadtree is that I can figure out where in the tree it goes from the start. So if I know the level I want to go to, like let's say level five split, I can compute in closed form which traversal of the tree I would need to place that element in the tree there. The other nice thing is that if you have dynamic data where your data is moving around, so imagine like someone shoots a laser beam out of your spaceship in your game, and you have your world game split up in a Quadtree, or if it's in three dimensions, an Octree, which is the same thing, but with eight children. If you imagine that laser bolt moving through space and needing to do these nearest neighbor queries, which is what you would want to do if you wanted to see like, 'If I hit something in my current box,' then this traversal, you can figure out in sort of a more direct closed form computation and do the management in a sort of much cheaper way rather than reshuffling and potentially needing to resplit like you would need to for a KD-tree or other kinds of spatial trees that would be used. So they're similar in actually the way you use it, and once you start looking at algorithms between them, like once you figured it out once, it kind of ends up being pretty similar for all of them and how to do these range searches and nearest neighbor searches. I find that these Quadtrees and Octrees are used a lot in games for dynamic data, and the Quadtree, KD-tree—there's also one more that's worth mentioning, I guess, which is like an R-tree, which is taking a group of data and drawing a rectangle around it and then building a tree of rectangles over multiple rectangles. And I won't go into it more than that, and you'll see that also used a lot in sort of mapping data, geographical or Geographic Information Systems (GIS) systems. We'll use these a lot in databases and stuff. Also, I think KD-trees can also be used in video games. I've seen them used for like splitting up a level and understanding which polygons you want to draw or not because the level itself, like the environment, tends to be static, and so you can a priori before you're, like when you're making your level, you can do the split and have it stored so that when you're trying to figure out which polygons to render, you just traverse this pre-made tree rather than having to build it at
Runtime. Yeah, totally. Yeah, I think, yeah, you really hit the nail on the head. I think KD-trees and R-trees—the structure of the tree is dependent on the data. So, you know, as Patrick said, if your data is like really wide, you might have a lot of vertical splits before you start seeing some horizontal splits. If data is really narrow, it's going to be the opposite. And yeah, in the case of the R-tree, you know, if there's a part of the space that's very dense, I mean, imagine building an R-tree of the US, right? You're going to have like a ton of rectangles around like New York City, San Francisco, Los Angeles, right? Because I mean, imagine you're building an R-tree on like over businesses or addresses in the US, right? There's just a lot more addresses in a tiny space in New York City, right? And so The structure of the tree will, you know, even all the way up to the decisions made at the very top will be influenced heavily by the distribution of the data versus like in the case of a Quadtree. You know, now there might be nodes that exist or don't exist because of the data, but the structure, like the way that the splits are done is totally immutable and it has nothing to do with the data, right? And so, you know, that's sort of like a double-edged sword, right? On one hand, if you're not splitting it in an informed way, then you're not going to be very efficient. And so if you're doing something just once, like if you're creating a tree off some data that's not changing and then using it a lot of times—like that's what Amazon is doing, you know, Amazon will create this giant tree, and that might take a lot of time, but then they're going to swap it out with the old tree and then they're going to use that new tree, you know, billions and billions of times. And so they, you know, they can afford to have this sort of frozen set, right? And so, and so they'll use like a KD-tree or an R-tree or something like that. But for games, you know, the content is moving all the time. I mean, there's—there's—as Patrick said, like if you have people walking around on a map shooting laser beams and all these things, you know, then, then you would, you can't have the structure of the tree dependent on these things that are constantly moving around. And so for games, they'll use these Octrees and Quadtrees. And my guess is, and I think you alluded to this, Patrick, is, you know, games will probably need both because the level doesn't move unless you're playing like Minecraft or something, but the level doesn't—geometry doesn't change. And so that would be much better served by something like a KD-tree or R-tree. And, and there's actually more like exotic trees for certain domains. There's like a BSP tree. There's a, you know, vantage point trees. And so they'll use something like that. But then for actually storing all of the dynamic content, we'll use something like an Octree. That was a lot of fun. Yeah. We totally got to geek out on trees. If we missed any trees, let us know. Or if we,
Yeah, that's right. Birch. Yeah. Minecraft taught me a lot about trees. I've been playing, playing that a lot with the kids. And I learned that birch is a thing. So I don't quite know like the order, the Big O notation of a birch. We'll have to figure that one out. But thank you so much, everyone, for all of your support. Let us know what you think about the duo episodes. I mean, we're bringing them back. We still have a lot of really awesome interviews lined up. So, you know, get looking forward to that. But we'll be, you know, kind of intermixing some of these, you know, duo episodes in there and definitely give us some feedback. We really appreciate all the feedback you have been giving, and just keep it up. Let us know what you think about the show. We definitely look at all of them. We've gotten a lot of our show ideas from listeners. This was one that we, we really wanted to do ourselves, but we've taken a lot of really great show topics from listeners. And that's all because of you and, you know, continuing support and feedback. So we appreciate it.
