 is binary heap and just a tiny bit of TDD. So of course, the focus is still all in Ruby. Okay, so let me just, okay. Can you all see my new slide? Did it change? Yep. Yep. Okay. So today my talk is about binary heap with TDD, although I said if I wrote it with TDD, but then actually TDD is just like a small part of it. Okay, so maybe let me introduce myself first for all these people who are the new people that are in this session that I have not met before. So I'm actually a software engineer working at Shopify in the Singapore office. I hardly use social media, but I do have a Twitter account. It can DM me or if you have any questions or you just want to connect, feel free. And then my hobby is long distance running. So if you are on Java, you are a runner also and you are using the running app, you can add me on Java too. Okay. So actually today's talk, like I said just now, is about both binary heap and TDD. So actually the main objective is about learning or revisiting this new, this data structure. Some of y'all might already know what is a binary heap, but then like for me, I came across it in school, but then I totally forgot about it, like right after school or even before school and then I forgot about it. Then until recently, I got reminded again when somebody mentioned it on Twitter, then I got curious and like why not implement it in Ruby. So and then I realized that it's actually a very good opportunity for me to practice some test driven development. So disclaimer is I probably can't go through the entire implementation today, but I'm hoping to reach the point where the tests are written. So because I personally know many people who are familiar with the idea of TDD, but not familiar with actually doing TDD. So doing TDD, writing tests after the code is actually not really considered because you don't really reap the benefits of TDD. So hopefully this session ends at the point where I can get their tests written and then can pick people's interest in practicing TDD on your own time. So let's go into what is binary heap first. So binary heap is actually a data structure that is commonly used to build parity kills. It was first introduced by a Welsh Canadian computer scientist called J.W.J. Williams in 1964 and that's like 50, 60 years ago. And then he introduced it actually as a data structure for Hipsaw. So Hipsaw is another sorting algorithm. And in terms of time complexity, which is like when we talk about algorithms, we always talk about time complexity, right? And then this part will make a lot more sense later, but I just want to put this upfront that like the insert and extracting elements from the heap, actually the time complexity is before log n. So what is actually a binary heap? This is a binary heap. It is a form of a binary tree, but with some rules and constraints. So what are the... I'll be going through the constraints first. What are the... Oh no, rules and constraints is like interchanges. Sorry. So I will be going through like some of the rules. So actually there's only two rules to make a binary tree considered a binary heap. So it's a shape property and the heap property. So the shape property actually just means very simply is the node should be filled from top to down and left to right. So meaning to say that if we have this tree right now, the next node that is going to be added should be at the... under the node 33 because it's top to down left to right. So it should be node 33. And then the second rule is actually the heap property. The heap property means that it requires a consistent order throughout that entire tree, which means either all daughter nodes are less than or equal to the parent node or all daughter nodes be greater or equal to the parent node. So it's a consistent order. And then it's either from the biggest to the smallest or the smallest to the biggest throughout that entire tree. So if we have a parent as the biggest node, which is the top node as the biggest node, then we will call your max heap. And if we have a small smallest number value at the top node, then we call your mean heap. So that's it for the rules. And then when it comes to data structure like a stack, like for example, if we talk about stack, we always, the two main operations is push and pop, right? So there's always some operations you can do on the data structure. So likewise, for binary heap, there's only two main operations also. Of course, there will be like some small ones, but then the two main ones are just inserting and extracting the top node. So how to insert a node in binary tree? So this is where the interesting part is for this data structure. How to insert a node? So for example, we have a node 45 here. And then when we want to insert, like if you remember, just now I said that there's the shape property, it can only, it's from top to bottom and then left to right. So the node has to go under node 36. But then when it goes under node 36, it doesn't fulfill the heap property. So then now we have to consider whether we need to move the node up. So in this case, 45 is bigger than 36 in this max heap, right? So we have to sort the position. And then we do this recursively and then we will sort the position, check whether it needs to move up again, which in this case actually doesn't because 100 is actually bigger than 45. So then if it fulfills the heap property and both the shape property and heap property, then the insert operation is done. So this is the theory of like how to do an insert. And then the next one is how to extract the top node. So which is in this case is to extract the node 100. So in how to do an extract of the top node is actually like we take away the top node and then we replace the top node with the last node. And then we do the opposite of the insert. This time round we bubble it down downwards. So in this case, the 36 can choose to sort place with 55 or 45. But if we sort with 45, then 45 will be on top of 55, which doesn't fulfill the heap property. So in this case, actually, we can only sort with the bigger node, so as to maintain the heap property. So in this case, we sort with 55. And then we check again, so it's recursive downwards this time round. So 36 can sort with 43 or 33. And we choose the bigger one and 36 will sort with 43. And then at this point, the heap property is fulfilled and then the shape property is still there intact. So then the extract operation is done. So in this case, the 100 is successfully extracted from the node. So just to recap what we have gone through about this very simple data structure called binary heap. So we have two rules. So it's the shape property, which is inserting nodes from top down left to right. And then the heap property, which requires a consistent order of nodes or values throughout the tree. There we have two main operations only, which is the insert, a new node, and then extract the top node. So this is like, I would say most of the things you need to know about a binary tree on theory, so on theory. And then next part is writing tests. So later we'll be writing just a simple test. But before that, I want to go through why is writing tests important for me? So personally, I feel the top reasons that why I like TDD is because TDD prevents me from yuckshaving. So there's this term called yuckshaving. I found this definition online, but I know that there's many, many definitions, but this, I found it a bit funny, but although it's a bit crude. So it says that yuckshaving is what you are doing when you are doing something stupid, fiddly, little task that bears no obvious relationship to what you're supposed to be working on. But yet a chain of causal relationship links what you are doing to the original meta task. Basically it just means that you are derailing and you are doing something that doesn't help you in building the feature that you are supposed to build. So TDD basically all in all it helps me focus on building the feature without writing some random code and doing some random tasks. And then before we go into writing the code, actually this is just a tip that actually a binary heap can be easily represented with an array. So we see this array, right? The index zero is the biggest, the top node, and then zero links to the left node is one, the right node is two, and then node one left node is three, right node is four. And then for node two, the is left node is five, and then the right node is six. So actually the formula is simple, it's i 2i plus one for the left node and 2i plus two for the right node. So this is just a tip, of course you can implement, represent binary tree however you want in code, but then this just a very simple way of representing a binary tree. Okay, so I know, so let's write some tests for real. I do know that many people say that writing code like in real time on the presentation is disastrous, but let me try. Okay, so let me open my editor. So I actually prepared some test data. So for example, we're going to start with a empty binary heap class. And then we have a initial lies, some seed data if you, if our data structure needs a seed data. So like I said, I'm going to represent the heap in a, using an array. And then of course let's set up our tests, test environment. So we have a described binary heap. And then let the data be, the test data be, in this case, if you see the binary, this is my test data. So if we represent in code, it will be 55, 36, 43 and 33. And then let's set up a test instance of the binary heap. And then we have data. So simply put, we're going to only have two main tests in your, of course, different people, when you are building really building it, you might choose to write more tests for just to ensure that your binary is working as you expected. But then in this case, for this insert test, what you really want to do, I also prepared some outcome. So if this is the test data right now, if we're going to insert a node, a node 45. So this is the outcome. So after we add the node here, and then it bubbles up. So if we do a subject, sorry, I got a, oh no. Okay, okay. Okay, okay, okay, okay, okay, here up. Okay, I'm going to add it in here. Okay, okay, I thought I was talking for the whole time that nobody heard anything. Okay, so back to the writing the test, right? So subject insert, which is our binary heap, we want to insert a 45. And then we can expect, oh, okay, before that, let's add a to array method so that we know we can check on the status of the heap. Okay, so we have to a, and then to equal to, in this case, this our test, the outcome that should happen, right? We have 55, 45, 43, 33 and 36. Okay, so this is our, it can just be a very simple test for our insert method. And I also prepared a outcome for extract. Okay, so in this case, 100 in this data structure that we, this heap that we have when we extract, there's actually two things that we should be testing. One is to testing, test whether the node is actually the one that we want. And then the other thing to test is actually the outcome. This is not node, so this is subject. The outcome. Okay, so it should be to equal to this final tree that we have. So it's very important to not test the implementation details, but to test like the outcome. Okay, so in this case, we just need these two tests to test the two main features of what a binary heap is. So in this case, I'm going to add empty methods so that it doesn't error out. Okay, call this number. And then extract. And then these two are empty methods. So if I'm going to run, wait, that's my terminal. So if I'm going to, wait, let me grab my terminal quick. So if we run our, our test here, binary heap, then we can see that actually is empty. Basically, this is not the outcome that we want. And then the node, we also didn't extract anything because the methods are empty. Yeah. So, so if you have been following what is the theory, right? Actually, in this case, we should be adding the, adding num to the end of the heap, which is the array. And then do some bubbling up according to the, the, the, what is supposed to be the characteristics of the insert operation. And then this one is to pop the first number from the heap or the array, and then sort the place, sort, sort the position with the last number, and then to bubble the, bubble the last number downwards again, over the tree. Okay. So, so I'm not going to go through the implementation, but because I think that this is like where the fun part is, and this is like the purpose of TDD. So like in this case, because we have the, the test here, it can write however, what, however code that you want here, as long as it passed the test, then I think it's probably fine. You have a working version of binary heap, it might not be the most effective or the most optimized way, but then it fulfills all the requirements of a binary heap. So I do have a solution here. Of course, this, this current version, empty version that we have here, I also have included a file for it. And, and then if you are, if you are interested to building it on your own, I will encourage writing more tests for yourself. And then if you are done, you just want to like see my solution, like reference my solution, you can go with, go with the link there. Yep. So, so in this case, I've, like just now I mentioned that writing test helps me keep spoken, keep me focused. So in this case, how does he help me focus is, I just need to pass the test. And that's about the, as long as my test is green. Yeah. So that's all for my talk for today. I hope that if you don't know, or if you don't know, or you don't really practice DDD, you will pick your interest in trying out. I know it might be difficult to, to, to do DDD for big, big, very big code base that doesn't have, already have passed, or it wasn't written in such a way that it's testable. But I will encourage starting from small, small data structures or small things, like, like just implementing a binary heap, then you can really bring out the, how the flow of how DDD, DDD helps with programming. Yep. So that's all for my talk for today. Feedbacks are welcome. Any channels you hear on the Slack group or on GitHub issue is fine. So that's it. Okay. So let's go back. Oh, shit.