Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
DATA
O
V
STRUCTURES
E
R
ALGORITHM
1
2
3
1
2
3
If the NoDisk is not 1, then, the function would call itself by deducting 1 from 3, i.e.,
hanoi(2, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 3-1 = 2
Again, NoDisk is not 1, the function calls itself by deducting 1 from 2, i.e.,
hanoi(1, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 2-1 = 1
1
2
3
If the NoDisk is not 1, then, the function would call itself by deducting 1 from 3, i.e.,
hanoi(2, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 3-1 = 2
Again, NoDisk is not 1, the function calls itself by deducting 1 from 2, i.e.,
hanoi(1, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 2-1 = 1
Now, NoDisk is 1, therefore, the function Moves Disk 1 from PEG1 to PEG3..
2
3 1
Therefore, we will start with hanoi(3, “PEG1”, “PEG2”, “PEG3”) // initial call from the main()
If the NoDisk is not 1, then, the function would call itself by deducting 1 from 3, i.e.,
hanoi(2, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 3-1 = 2
Again, NoDisk is not 1, the function calls itself by deducting 1 from 2, i.e.,
hanoi(1, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 2-1 = 1
Now, NoDisk is 1, therefore, the function Moves Disk 1 from PEG1 to PEG3..
The control returns back to the first call (here again the value of NoDisk is 2),
The function Moves Disk 2 from PEG1 to PEG2
3 2 1
Therefore, we will start with hanoi(3, “PEG1”, “PEG2”, “PEG3”) // initial call from the main()
If the NoDisk is not 1, then, the function would call itself by deducting 1 from 3, i.e.,
hanoi(2, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 3-1 = 2
Again, NoDisk is not 1, the function calls itself by deducting 1 from 2, i.e.,
hanoi(1, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 2-1 = 1
Now, NoDisk is 1, therefore, the function Moves Disk 1 from PEG1 to PEG3..
The control returns back to the first call (here again the value of NoDisk is 2),
The function Moves Disk 2 from PEG1 to PEG2
3 2 1
Therefore, we will start with hanoi(3, “PEG1”, “PEG2”, “PEG3”) // initial call from the main()
If the NoDisk is not 1, then, the function would call itself by deducting 1 from 3, i.e.,
hanoi(2, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 3-1 = 2
Again, NoDisk is not 1, the function calls itself by deducting 1 from 2, i.e.,
hanoi(1, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 2-1 = 1
Now, NoDisk is 1, therefore, the function Moves Disk 1 from PEG1 to PEG3..
The control returns back to the first call (here again the value of NoDisk is 2),
The function Moves Disk 2 from PEG1 to PEG2
1
3 2