JSFiddle - React, Tailwind, and code Playground
by Jeff Santos
HTML
Please Enter Value:
<br/>
<input type="textbox" id="nValue" value="" />
<input type="button" value="Perform Recursion" onClick="performRecursion();" />
<p id='output'></p>
1. Big O = O(2^n)
<br/>
2. No, 2^46 - 1 = 18,446,744,073,709,551,615. Therefore, at a rate of one move per second, it would take 584,942,417,355 years, so we need not worry.
JavaScript
var val = '';
var iteration = 0;
var Node = function(_content) {
this.next = null;
this.previous = null;
this.content = _content;
}
var Queue = function() {
this.front = null;
this.back = null;
this.push = function(_content) {
if (this.front == null) {
this.front = new Node(_content);
this.back = this.front;
return this;
}
var addedNode = new Node(_content);
addedNode.previous = this.back;
this.back.next = addedNode;
this.back = addedNode;
return this;
}
this.removeFront = function() {
if (this.front == null) {
return null;
}
var remove = this.front.content;
if (this.back == this.front) {
this.front = null;
this.back = null;
return remove;
} else {
this.front = this.front.next;
this.front.previous = null;
return remove;
}
}
this.pop2 = function() {
if (this.front == null) {
return null;
}
var remove = this.back.content;
if (this.back == this.front) {
this.front = null;
this.back = null;
return remove;
} else {
this.back = this.back.previous;
this.back.next = null;
return remove;
}
}
this.toString = function() {
var str = "";
var node = this.front;
if (this.front == null) {
str = "Queue is Empty";
}
while (node != null) {
str += node.content + " ";
node = node.next;
}
return str;
}
}
function recurs(n, t1, t2, t3) {
if (n > 1) {
recurs(n - 1, t1, t3, t2);
recurs(1, t1, t2, "");
recurs(n - 1, t3, t2, t1);
}
if (n == 1) {
moveXtoY(t1, t2);
}
}
function moveXtoY(towerX, towerY) {
if (towerX == "aTower") {
var h = aTower.pop2();
}
if (towerX == "bTower") {
var h = bTower.pop2();
}
if (towerX == "cTower") {
var h = cTower.pop2();
}
if (h == null) {
progressString += "move not valid.";
} else {
if (towerY == "aTower") {
aTower.push(h);
}
...