Recursion

Assignment 7

by Alan Harris

HTML

<p>
1. The complexity in BigO notation is: 2<sup>n</sup>. This formula is 2<sup>n</sup>- 1 <br>
2. 2<sup>64</sup>- 1 is = 18446744073709551615. I do not think we should be concerned with this legend. 
<br><br>





<label>This is the Classic Tower of Hanoi. Insert number of disks and press "Calculate" to begin.</label><br><br>

<input type="text" id="nodeinput"><br><br>
<button id="n" onclick="addtoStack();">
Calculate
</button>


<br><br>
<p>
Number of Moves:
</p>
<div id="print"></div>

CSS

button:focus {
  border: 1px solid black;
  padding: 4px 4px;
}

button {
    background-color: grey;
    border: 1px solid black;
    color: white;
    padding: 4px 8px;
    text-decoration: none;
    margin: 4px 2px;
    }
 button:hover {
   background-color:black;
 }

JavaScript

var moves = 0;

function Node(element) {
  this.content = element;
  this.next = null;
  this.last = null;
  return this;
}  

var Stack = function() {
	this.head = null;
  this.top = null;
  
 	this.push = function(element) {
  	if (this.head == null) {
  		this.head = new Node(element);
   		this.top = this.head;
    	return this;
  
  }
  var node_Input = new Node(element);
  
  node_Input.last = this.top;
  this.top.next = node_Input;
  this.top = node_Input;
  return this;

}
this.pop = function() {
	if (this.head == null) {
  	alert("Empty Stack");
    return null;
  }
	 if (this.head == this.top) {
  	var a = this.top;
    this.top = null;
    this.head = null;
   return a;
  } 
  

  var n = this.top;
  this.top = this.top.last;
  this.top.next = null;
  return n;
 }
 this.print = function() {
 	var s = "";
  var node = this.head;
  
  while (node != null) {
  	s += node.content;
    node = node.next;
  }
 return s;
  
	}
}




function addtoStack() {
	clear();
	var hanoiStack1 = new Stack();
  var hanoiStack2 = new Stack();
  var hanoiStack3 = new Stack();
	
 var d = document.getElementById("nodeinput").value;
 if (d < 1 || d > 10 || d != parseInt(d)) {
 	document.getElementById("print").innerHTML = "Enter a number Between One and ten";
//	throw "Enter number between One and Ten."
	
	
 }
 else {
 	parseInt(d);
	for(var b = d; b > 0; b--) {
  	hanoiStack1.push(b);
  }
  	
 Hanoi(d, hanoiStack1, hanoiStack2, hanoiStack3);
   
 }  
}
  
 
 function Hanoi(p, StackA, StackB, StackC) {
	 
	
 	if (p == 1) {
  	move(StackA,StackB);
    moves++;
		
		
  }
  else {
  	Hanoi(p - 1, StackA, StackC, StackB)
    move(StackA, StackB);
    moves++;
    
    Hanoi(p - 1, StackC, StackB, StackA);
		
  }
	print();
 }
 
 function move(A,B) {
 	B.push(A.pop().content);
 }
 
 function print() {
 	s = "";
	s += moves;
  document.getElementById("print").innerHTML = s;
 }

function clear() {

	moves = 0;
	del = "";
	document.getElementById("print").innerHTML = del;
}