Placeholder Image

字幕列表 影片播放

  • Riker Shin is one of the most confusing topics and computer science, but it's used everywhere and programming.

  • So in today's video, I'm going to be simple.

  • Find Rikers in for you, and if you're new to the channel, make sure you subscribe for more videos where I simplify the Web for you.

  • Let's get started now.

  • Now, before we jump into any crazy code examples using Rikers in, we first need to understand what Ryker Shin is.

  • And luckily, the definition is very simple thing Idea of Rikers in is a function that calls itself That's it.

  • All has to do is call itself somewhere inside of the function, and immediately you're probably thinking that you just run into an infinite loop of function, calling itself over and over and over again until your computer dies of the world explodes.

  • But the thing about Ryker Shin is that it works a lot like normal loose and that you have some kind of exit conditions that jumps you out of the recursive so that the function stops calling itself over and over again.

  • And the thing that makes Rikers in difficult to wrap your head around is you have to keep track of all the previous calls inside of the Riker Zhan stack when you're trying to plan out of recursive function.

  • So what I'm gonna do in this video is we're gonna take a look at it, a few examples of normal non recursive functions, and we're going to change them to be recursive function so I can show you the difference between the two.

  • And we can also visualize what's going on to start with.

  • We're gonna cover the most basic example, which is a function that counts down from the specified number.

  • You give it all the way down to zero.

  • So let's just take a look at this basic function.

  • All we have is a simple four loop that counts down from my number.

  • As long as we're greater than zero, it's going to attract twenties time to go through a loop, and it's just going to print out that number.

  • And when we get to zero, is just gonna print out who Ray.

  • So let's call that function.

  • We're just going to say countdown and let's say that we give it three.

  • It's going to count it as you see 321 and then Ray, Let's just clear out a council and let's work on implementing this as a recursive function.

  • And I remember in a recursive function instead of using a loop like this, what we're going to do is we're gonna call the same function over and over again.

  • So let's create that function.

  • We're gonna call it here Countdown.

  • We're gonna call it countdown recursive so that we can separate it from our other function.

  • And again, it's going to take a number end that we count down from.

  • And the first thing we need to do in a recursive function is we need to have our claws that breaks us out of the recursive function.

  • So in our example, here we are going to this loop as long as I is greater than zero, which means we're breaking out of this loop when I is less than or equal to zero.

  • So we need to do in here is we need a check.

  • If N is going to be less than or equal to zero, then we want to break out of our recursive function.

  • And to do that we just call the return statement.

  • But we need to actually call this council dot log Ray inside of here is well, because this is what's going to happen at the very end of a recursive function.

  • Just like this happens at the end of our lives after we break out.

  • So now we have a breakout statement which is essentially the exact opposite of the wild condition for a four loop.

  • Then we can actually implement the recursive nature of our function.

  • And the important thing is, if we forget this, our function is just going to call itself over and over and over again and tell your browser in this case, chrome crashes, which is something we don't want.

  • It's not Let's go down here.

  • First thing we want to do is door council dot log.

  • We'll reprint out in because in this case, we're printing out our number here and then the next thing we need to do is we need to call our function again.

  • But what do we pass into this function here?

  • Well, up in this loop, you can see we're subtracting one from I, which means we're just constantly making our number end smaller by one.

  • So we're doing here is we're gonna pass and minus one into our countdown function.

  • Now, we're just gonna say that and see if this actually works.

  • What's called Countdown recursive of three.

  • And you see, it prints out 321 and who rate.

  • So this words exactly the same as our typical countdown function.

  • Let's break it down a little bit further to figure out exactly how this function works.

  • So we're gonna break this down step by step.

  • The first thing that happens is we call Countdown recursive and we call it with the number three.

  • And what this is going to do is is going to go into our loot.

  • It's going to say, is three less than zero.

  • It's not.

  • So we skip all of this, we're gonna log out three.

  • And then after that, Runa called countdown recursive with n minus one, which in our case, is too.

  • This is done inside of countdown recursive.

  • So we're gonna say countdown recursive of two and again.

  • Same exact thing is end less than zero.

  • No, it's not.

  • Okay, So we're going to call this countdown function again.

  • Countdown recursive.

  • But this time with one and then again, same thing.

  • It's not less than or equal to zero.

  • So it's going to call again Countdown recursive.

  • And this time it's going to call it with zero.

  • And now immediately, we come in here and we see Oh, it's less than equal to zero.

  • So we return.

  • So here is what we do.

  • We're returning out of this statement.

  • We're now no longer in this count on recursive zero, and now we move up back into countdown recursive one and our counter recursive one is already at the bottom the function.

  • So we just returns because essentially, when you reach the bottom of your function, it's like having a return at the bottom just like this.

  • So we leave Countdown one, go back to Countdown to which starts right here again heads at the very end of our function because it just finished calling this.

  • So we go back to Countdown three and again at the bottom of function, so it leaves it again.

  • So all of these are returning themselves, so it would look kind of like this.

  • We do our return, and then we do return in return for all these different functions that were calling just like this.

  • So that's kind of how this really basic recursive function works.

  • But as you know, it's not really very useful toe make this recursive.

  • It's so much easier to do it in a loop, and it just makes more sense easier to wrap your head around.

  • So let's look at it.

  • Another example of a recursive function, one where we actually are taking an input and constantly growing our input inside of a recursive function, which is something that's very common in most recursive functions.

  • So here's another example of a very comment iterative approach that we would take to a solution to some of range of numbers.

  • So if we get the number three in here, we want to some 32 and one together, which will give us six.

  • So all we do is we initialize are total to be zero.

  • We lived through all these different numbers, starting at our highest number, going all the way down to we get above zero, subtracting one each time and adding it to our total.

  • And then we return our total with the end.

  • So let's test this function.

  • We can call some range of three as you can give you.

  • See, we get six, which is just three plus two plus one.

  • Now let's work on Iterating this in a recursive function.

  • So we just credit function down here, which is going to be called some range, and this is going to be a recursive.

  • And instead of here, we are again going to take the end value which were passing into our function.

  • And as you can see, the very first thing that we're doing here is we're creating a total variable and setting it to zero.

  • But since we're inside of a recursive function, any variable we set in this function is only available to that one single version of the recursive function and not to all of them.

  • So we actually need to pass this total value to all of our different recursive functions and by default.

  • The first time we called his function, we want our total to be set to zero by default.

  • So now that we have that out of the way, let's create our guard costs which is going to exit is out of the function immediately.

  • So again, we're going to stay here.

  • This is while I is greater than zero, which means we leave our loop when I is less than or equal to zero.

  • So we could just say one end is less than or equal to zero.

  • We want to exit out of our loop where we want to turn our current total.

  • Otherwise, if we're not inside of that guard, cause we want to do here is we want to return our total, which is going to be plus and that's going to be our new total here.

  • And we also need to call this function.

  • So it's called some range recursive and we have our total version right here, which is gonna be a total plus our current number.

  • But what is our end going to be?

  • Our end is just going to be n minus one, which we're going to pass to our function here and it's emulating.

  • Here are looping of just subtracting one every single time.

  • So now it's saved that and call this function some ranger cursive of three and you see we get six.

  • It's working as intended.

  • Well, it's deep dive again into how this function is actually working.

  • Let's go down here.

  • And the first thing we're gonna do is we're gonna call some range recursive which three and that's just going to essentially default are total to zero.

  • Even though we don't have to pass that in and inside.

  • Here, we check.

  • Is our number three less than zero.

  • It's not.

  • So we're calling the exact same function again, and this time it's going to have to and for the value for the total is just going to be zero plus three, which in our case, is going to be three here.

  • Now let's move on to the next generation, which is it's going to be the exact same function again.

  • But this time we're calling it with a number one, and our total here is going to be five.

  • Because it's three plus two again, one is not less than equal to zero.

  • So we're calling this function another time.

  • It's going to be our last time because we have now an end of zero, and our total here is going to be six.

  • So now we can go back up.

  • We can say OK and is less than or equal to zero.

  • So we want to do is we want to return our total, which in our case is six So this is going to do is going to return six.

  • And now we jump back to our some range recursive with the one in five, and it's right here at this point.

  • We just executed the code here, so we're returning.

  • Whatever this function returns in our case, that's returning.

  • Six.

  • So this function is going to return hopes.

  • Return six and again go back up when we call some recursive two and three.

  • It's going to return from this function, and it's going to return whatever that function returns, which in our case is again just going to be six.

  • And as you can imagine, the very last function we call here is gonna do the same thing.

  • It's calling this function and whatever this returns is what this function is returning for us, which is six.

  • So, as you can see, the function that we've covered so far are just easier to do with a four loop.

  • They make more sense that way, and that's perfectly normal.

  • Rikers in is not something you use all the time, but the next function that we're going to be covering is something that you almost always need to use.

  • Rikers in for because it makes your coach so much easier to follow.

  • So let's look at that now.

  • So here we can see our function, which is called Print Children.

  • And as you can see, there's no implementation for because to do this with Loops, as I said, is incredibly complex and almost impossible on.

  • What we want to do with our function is we want to take this tree here, and we just want to print all of the Children all the way down as deeply nested as they go.

  • So if we, for example, passed this tree, we have John, which has the Children Jim and Zoe.

  • And then we also want to print Zoey's Children, which is gonna be Kyle and Sophia.

  • And to do this with the Loop.

  • As I said, it's difficult.

  • So let's look at how to do it with a recursive function, which is actually pretty straightforward.

  • So let's create that function, which is just going to be print Children recursive and again.

  • It's going to take whatever tree we want to pass into it, and the very first thing we need to do is we need to create our guard costs to exit us out of the loop.

  • So we're going to say, if our tree, which in our case is t dot Children, hopes Children dot length is equal to zero.

  • So if our tree, for some reason has absolutely no Children and we just want to return and this is going to prevent us from here when we get to Jim were Kyle or Sophia is just going to exit out because we don't have any Children toe loop through.

  • The next thing that we want to do is actually looked through all the different Children.

  • So we could just say t dot Children dot for each and for each child that we have inside of here.

  • We actually want to run some code, and the first thing we'll do is just print out that child so we could just say child that name instead of here to print it out.

  • And then we want to look through all of that child's Children so we can say print Children recursive and we can pass in that child, and that's all the code we need to right here.

  • Let's just save this and executed to make sure it works.

  • We're gonna pass in that tree variable that we have.

  • And as you can see, it prints out Jim, Zo, Kyle and Sofia Energy.

  • Sit down.

  • Here at the Children are Jim, Zoe, Kyle and Sofia.

  • So, what's again?

  • Deep dive into how this code actually works.

  • Let's make a little bit of room for ourselves.

  • And the first time that we call this, we're gonna be coin print Children.

  • Recursive This is going to be with John's Children.

  • So we're just gonna put in John here.

  • When we get into this, we said that John has two Children, so we're actually gonna be calling this function twice.

  • We're gonna be calling print Children recursive with the gym.

  • We're gonna be calling it again.

  • Print child, recursive lips, recursive.

  • And this time, it's going to be with Zoe.

  • So its first look at how Jim's call gets executed.

  • We can see that Jim has absolutely no Children.

  • So this is just going to return.

  • It's not going to do anything inside of here, so we don't really have to worry about that branch too much.

  • And now it's going to move on to this Zoe branch.

  • And as you can see, Zoe has two Children.

  • So we're gonna we're gonna call this function print Children recursive two times.

  • One time is going to be called with Kyle and Kyle's Children.

  • And the next time it's going to be called here with Sofia.

  • There we go.

  • And both of these don't have any Children.

  • So again, they're just going to return, and they're going to return.

  • And now Zoe is going to return after going through all of her different Children.

  • And then John is lastly going to return when he goes through all of his different Children just like that.

  • And now it's even look a little bit deeper into this.

  • So as we can see, we call this with John.

  • We go through here, he has Children, so we don't actually execute this.

  • So we looked through each one of his Children from their name and call it So the first time we come through here, we call this on Jim.

  • So we completely ignore all of this.

  • Code down here is silly.

  • Right now we just call this with Jim on.

  • We go through Jim's Children so we see that Jim doesn't have any Children, so we exit immediately and then we can move on to our next function, which is the one with Zoe.

  • So we come in here, we see that Zoe does have Children.

  • So we looked through each one of them.

  • And the first thing we do is go through the first child, which is Kyle.

  • And we jump into this again and return because it doesn't have any Children and so on.

  • As we go through Sophia and the rest of them eggs out.

  • And as you could see that you close this off, save it and recall that function of print Children recursive with our tree.

  • You can see that that is working properly.

  • And it's something that, as I mentioned, is very difficult to do.

  • Iterative lee with a normal loop because you don't know how deeply nested these Children could be.

  • They could be nested two levels deep like this where they could be invested, 100 little steep.

  • So writing that with an iterative loop is just much more difficult than this simple code here to print it out, recursive lee and as the real benefit of recursive functions over integrated functions and that's all there is to recursive functions make sure check on my other videos linked over here for more simplified explanations of Web development.

  • Also subscribe to the channel.

  • If you enjoyed this video and want more videos like this thank you very much for watching and have a good day.

Riker Shin is one of the most confusing topics and computer science, but it's used everywhere and programming.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

什麼是遞歸--深入淺出 (What Is Recursion - In Depth)

  • 2 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字