![]() Step10:Īs seen in Image, TH (2,2,1,3) will complete all 3 steps in the same manner as we discussed in previous calls, and at last, it will terminate and the whole call will finish. Step9:Īfter print the statement, TH (3,1,2,3) will execute its 2 nd recursive call which is TH (2,2,1,3). Step8:Īs seen in Image, TH (3,1,2,3) completed its first call, now it will print “1 to 3” on the screen. Here, control goes back to the previous call TH (2,1,3,2) which also executed all 3 steps, again control goes back to the previous call which is TH (3,1,2,3). Now, here, all steps of call TH (1,3,1,2) have finished. Then it will print “3 to 2” on the screen and after that, it will call its 2 nd recursive call which will also terminate hare. The first call will terminate because it will fail for if condition. Here, TH (1,3,1,2) will execute all 3 steps as you can see in the above image. After printing, it will execute its 2 nd recursive call TH (1,3,1,2) with the following modified parameters: num = 1, X = 3, Y = 1, Z = 2. Step6:Īs shown in above Image, TH (2,1,3,2) has completed its first call, now it will print “1 to 2” on the screen. As this call is completed its all 3 steps, control return to the previous call which is TH (2,1,3,2). Inside this call, it will check for if (0 > 0), No, it will terminate here and control goes back to previous call TH (1,1,2,3). Then control goes back to the previous call TH (1,1,2,3), here it has finished its first call, now it will print “1 to 3” on the screen.Īfter print the statement on screen, TH (1,1,2,3) will execute its 2 nd recursive call which is TH (0,2,1,3) with the modified parameters as: num = 0, X = 2, Y = 1, Z = 3. TH (0,1,3,2) will check for if (0 > 0), No, this call will terminate here. It will execute its first call TH (0,1,3,2) with the modified parameter as num = 0, X = 1, Y = 3, Z = 2 Step4: Here TH (1,1,2,3) check for if (1 > 0), yes, then again here are three things- call, print and again call. It will execute its first call TH (1,1,2,3) with the modified parameter as num = 1, X = 1, Y = 2, Z = 3 Step3: Here TH (2,1,3,2) check for if (2 > 0), yes, then again here are three things- call, print and again call. In above Image, it will execute TH (2, 1, 3, 2) with modified parameter as num = 2, X = 1, Y = 3, Z = 2 Step2: recursive call – TH (num – 1, y, x, z) (x = y, y = x, z = z)įirst it has to complete its 1 st recursive call then print a statement then again execute its 2 nd recursive call.print statement – printf (“From %c to %c \n”, x, z).recursive call – TH (num – 1, x, z, y) (swapping of z and y).We call our function with the following parameters –įirst it will check for if (3 > 0), yes, then here are 3 things – Tracing Tree of Tower of Hanoi Program using Recursion: Step1: Let’s understand each and every step in Tracing Tree. ![]() Let us first see the code and then we will understand the code using the tracing tree. Tower of Hanoi Program using Recursion in C Language: Data Structures and Algorithms Tutorials.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |