Advanced Data Structures / Tower of Hanoi
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this video we're going to take a brief look at a Tower of Hanoi implementation in Ruby and we're doing it here because it implements essentially a stack structure. In fact, three stack structures that are called pegs here. I found this online. It's by an individual called Josh Goebel. He put it on, uh, the website where you can paste up different bits of code. So I'll include it in the working files. It's called TH for the Ruby 18 section. So if we look here, we have a couple of classes defined. We have this class. I'm going to shrink these down so we can see what he's doing here. We have this Hanoi rack class and we have a couple things declared in that. So you can see on Ruby you've got the same class declared twice. So first one, we have a class inside of Hanoi rack declared. That's the peg. And that's where it's essentially implementing a stack structure and we can see that because here we have push and here we have pop and so we're pushing the rings onto the peg and we're popping them back off again utilizing an array as a stack with these two methods. Now, the other method down here simply has to do with moving some of the logic involved and checking where the rings go and it's a little bit involved and I'm not going to really get into here. It isn't germane to our discussion of stacks. And this is the type of file that you can just call with Ruby and it'll run. It'll run, print out here of what the pegs look like. So if I go ahead and run it here it's going to just zip by on the screen really fast. But I'm going to scroll back up here and this Towers of Hanoi is a wonderful implementation of how stacks are actually utilized. Now, the limitation of the Tower of Hanoi game is that you can never put a bigger disk on a smaller disk. So that's where the complication comes in with it. So you can see here the little disk in this one we start here gets moved over to C and then you move that disk over here and then you'll see that little disk move back and that operation just carried back and forth, back and forth a number of times until you eventually move the entire stack over all the way down here, down to C. But it's a wonderful implementation of a stack in code and it's very simple and you can see how they've essentially defined the peg as a class implementing that stack-like behavior. Then down inside here, you're actually doing a push and a pop. And you can see how they use that same name for this particular method and then they called it an array where they're actually storing the stuff. Right here is the actual array where they're utilizing it as a stack. So just a little interesting program for you to take a look at, play around with.
Tutorial Information
| Course: | Programming With Ruby |
| Author: | Al Anderson |
| SKU: | 33788 |
| ISBN: | 1-934743-01-1 |
| Release Date: | 2007-08-21 |
| Duration: | 8.5 hrs / 113 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 