├── Chapter4 ├── chap4-3.js ├── chap4-4.js ├── chap4-2.js ├── chap4-5.js ├── chap4-7.js ├── Chapter1 │ └── chap1-1.js ├── chap4-11.js ├── chap4-10.js ├── chap4-6.js ├── chap4-8.js ├── chap4-9.js ├── chap4-12.js └── chap4-1.js ├── Chapter2 ├── chap2.js ├── chap2_5.js ├── chap2_1.js ├── chap2_4.js ├── chap2_6.js ├── chap2_9.js ├── chap2_3.js ├── chap2_2.js ├── chap2_8.js ├── chap2_10.js ├── chap2_12.js ├── chap2_11.js ├── chap2_7.js ├── chap2_13.js └── chap2_14.js ├── 9781449364939_files.zip ├── Chapter12 ├── Chap12-2.js ├── Chap12-4.js ├── Chap12-8.js ├── Chap12-10.js ├── Chap12-3.js ├── Chap12-5.js ├── Chap12-7.js ├── Chap12-6.js ├── Chap12-12.js ├── Chap12Shellsort.js ├── Chap12-15.js ├── Chap12-11.js ├── Chap12-9.js ├── Chap12-1.js ├── Chap12-13.js └── Chap12-14.js ├── Chapter13 ├── Word Documents │ ├── Lists.docx │ ├── Queues.docx │ ├── arrays.docx │ ├── btrees.docx │ ├── graphs.docx │ ├── prop.docx │ ├── sets.docx │ ├── stacks.docx │ ├── Hashing.docx │ ├── bsearch.docx │ ├── bsorting.docx │ ├── jsdsprop.docx │ ├── preface.docx │ ├── advsorting.docx │ ├── linkedlists.docx │ ├── oreillyprop.docx │ └── progmodel.docx ├── Chap13-3.js ├── Chap13-1.js ├── Chap13-5.js ├── Chap13-7.js ├── Chap13-6.js ├── Chap13-9.js ├── Chap13-10.js ├── Chap13-8.js ├── Chap13-15.js ├── Chap13-11.js ├── Chap13-4.js ├── Chap13-13.js ├── Chap13-14.js ├── Chap13-16.js ├── Chap13-2.js ├── Chap13-12.js └── Chap13-17.js ├── Chapter11 ├── Chap11-3.js ├── Chap11-7.js ├── Chap11-4.js ├── Chap11-1.js ├── Chap11-5.js ├── Chap11-2.js └── Chap11-6.js ├── Chapter5 ├── chap5-3.js ├── chap5-1.js ├── chap5-5.js ├── chap5-7.js ├── chap5-6.js ├── chap5-9.js ├── chap5-10.js ├── chap5-8.js ├── chap5-15.js ├── chap5-11.js ├── chap5-4.js ├── chap5-13.js ├── chap5-14.js ├── chap5-2.js ├── chap5-16.js ├── chap5-12.js └── chap5-17.js ├── Chapter7 ├── Chap7-2.js ├── Chap7-5.js ├── Chap7-4.js ├── Chap7-3.js └── Dictionary.js ├── Chapter10 ├── Chap10-2.js ├── Chap10-3.js ├── Chap10-4.js ├── Chap10-5.js ├── Chap10-1.js └── bst.js ├── Chapter8 ├── Chap8-1.js ├── Chap8-3.js ├── Chap8-5.js ├── Chap8-4.js ├── Chap8-2.js └── hashing.js ├── Chapter6 ├── chap6-2.txt ├── chap6-3.txt ├── chap6-1.txt └── chap6-4.txt ├── Chapter9 ├── Chap9-3.js ├── Chap9-2.js ├── Chap9-5.js ├── Chap9-4.js ├── Chap9-1.js └── Set.js ├── Chapter3 ├── chap3_2.js ├── chap3_3.js └── chap3_1.js ├── README.md ├── Chapter14 ├── Chap14-1.js ├── Chap14-3.js ├── Chap14-2.js └── Chap14-4.js └── Chapter1 └── chapter1.js /Chapter4/chap4-3.js: -------------------------------------------------------------------------------- 1 | for (var i = 1; i < 10; ++i) { 2 | print(Math.random()); 3 | } -------------------------------------------------------------------------------- /Chapter4/chap4-4.js: -------------------------------------------------------------------------------- 1 | for (var i = 1; i < 10; ++i) { 2 | print(Math.random() * 11); 3 | } -------------------------------------------------------------------------------- /Chapter4/chap4-2.js: -------------------------------------------------------------------------------- 1 | var numElements = 100; 2 | var mynums = new CArray(numElements); 3 | print(mynums.toString()); -------------------------------------------------------------------------------- /Chapter2/chap2.js: -------------------------------------------------------------------------------- 1 | var numbers = [1,2,3,4,5]; 2 | if (Array.isArray(numbers)) { 3 | // perform array tasks on numbers 4 | } 5 | -------------------------------------------------------------------------------- /Chapter4/chap4-5.js: -------------------------------------------------------------------------------- 1 | for (var i = 0; i < numElements; ++i) { 2 | this.dataStore[i] = Math.floor(Math.random() * (numElements+1)); 3 | } -------------------------------------------------------------------------------- /9781449364939_files.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/9781449364939_files.zip -------------------------------------------------------------------------------- /Chapter12/Chap12-2.js: -------------------------------------------------------------------------------- 1 | var numElements = 100; 2 | var myNums = new CArray(numElements); 3 | myNums.setData(); 4 | print(myNums.toString()); 5 | -------------------------------------------------------------------------------- /Chapter13/Word Documents/Lists.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/Lists.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/Queues.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/Queues.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/arrays.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/arrays.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/btrees.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/btrees.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/graphs.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/graphs.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/prop.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/prop.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/sets.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/sets.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/stacks.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/stacks.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/Hashing.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/Hashing.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/bsearch.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/bsearch.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/bsorting.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/bsorting.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/jsdsprop.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/jsdsprop.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/preface.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/preface.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/advsorting.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/advsorting.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/linkedlists.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/linkedlists.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/oreillyprop.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/oreillyprop.docx -------------------------------------------------------------------------------- /Chapter13/Word Documents/progmodel.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreillymedia/data_structures_and_algorithms_using_javascript/HEAD/Chapter13/Word Documents/progmodel.docx -------------------------------------------------------------------------------- /Chapter11/Chap11-3.js: -------------------------------------------------------------------------------- 1 | load("Graph.js"); 2 | g = new Graph(5); 3 | g.addEdge(0,1); 4 | g.addEdge(0,2); 5 | g.addEdge(1,3); 6 | g.addEdge(2,4); 7 | g.showGraph(); 8 | g.bfs(0); -------------------------------------------------------------------------------- /Chapter4/chap4-7.js: -------------------------------------------------------------------------------- 1 | var numElements = 10; 2 | var mynums = new CArray(numElements); 3 | print(mynums.toString()); 4 | mynums.bubblesort(); 5 | print(); 6 | print(mynums.toString()); -------------------------------------------------------------------------------- /Chapter2/chap2_5.js: -------------------------------------------------------------------------------- 1 | function square(num) { 2 | print(num, num * num); 3 | } 4 | 5 | var nums = []; 6 | for (var i = 0; i < 10; ++i) { 7 | nums[i] = i+1; 8 | } 9 | nums.forEach(square); -------------------------------------------------------------------------------- /Chapter5/chap5-3.js: -------------------------------------------------------------------------------- 1 | function seqSearch(arr, data) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | if (arr[i] == data) { 4 | return i; 5 | } 6 | } 7 | return -1; 8 | } -------------------------------------------------------------------------------- /Chapter12/Chap12-4.js: -------------------------------------------------------------------------------- 1 | var numElements = 10; 2 | var mynums = new CArray(numElements); 3 | mynums.setData(); 4 | print(mynums.toString()); 5 | mynums.bubbleSort(); 6 | print(); 7 | print(mynums.toString()); -------------------------------------------------------------------------------- /Chapter13/Chap13-3.js: -------------------------------------------------------------------------------- 1 | function seqSearch(arr, data) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | if (arr[i] == data) { 4 | return i; 5 | } 6 | } 7 | return -1; 8 | } 9 | -------------------------------------------------------------------------------- /Chapter5/chap5-1.js: -------------------------------------------------------------------------------- 1 | function seqSearch(arr, data) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | if (arr[i] == data) { 4 | return true; 5 | } 6 | } 7 | return false; 8 | } -------------------------------------------------------------------------------- /Chapter13/Chap13-1.js: -------------------------------------------------------------------------------- 1 | function seqSearch(arr, data) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | if (arr[i] == data) { 4 | return true; 5 | } 6 | } 7 | return false; 8 | } 9 | -------------------------------------------------------------------------------- /Chapter2/chap2_1.js: -------------------------------------------------------------------------------- 1 | var sentence = "the quick brown fox jumped over the lazy dog"; 2 | var words = sentence.split(" "); 3 | for (var i = 0; i < words.length; ++i) { 4 | print("word " + i + ": " + words[i]); 5 | } -------------------------------------------------------------------------------- /Chapter2/chap2_4.js: -------------------------------------------------------------------------------- 1 | var cis = ["Mike","Clayton","Terrill","Danny","Jennifer"]; 2 | var dmp = ["Raymond","Cynthia","Bryan"]; 3 | var it = cis.concat(dmp); 4 | print(it); 5 | it = dmp.concat(cis); 6 | print(it); -------------------------------------------------------------------------------- /Chapter5/chap5-5.js: -------------------------------------------------------------------------------- 1 | function findMin(arr) { 2 | var min = arr[0]; 3 | for (var i = 1; i < arr.length; ++i) { 4 | if (arr[i] < min) { 5 | min = arr[i]; 6 | } 7 | } 8 | return min; 9 | } -------------------------------------------------------------------------------- /Chapter5/chap5-7.js: -------------------------------------------------------------------------------- 1 | function findMax(arr) { 2 | var max = arr[0]; 3 | for (var i = 1; i < arr.length; ++i) { 4 | if (arr[i] > max) { 5 | max = arr[i]; 6 | } 7 | } 8 | return max; 9 | } -------------------------------------------------------------------------------- /Chapter4/Chapter1/chap1-1.js: -------------------------------------------------------------------------------- 1 | var x = 3; 2 | var y = 1.1; 3 | print(x + y); 4 | var z = x * y; 5 | print(z.toFixed(2)); 6 | print((x+y)*(x-y)); 7 | var z = 9; 8 | print(Math.sqrt(z)); 9 | print(Math.abs(y/x)); 10 | -------------------------------------------------------------------------------- /Chapter13/Chap13-5.js: -------------------------------------------------------------------------------- 1 | function findMin(arr) { 2 | var min = arr[0]; 3 | for (var i = 1; i < arr.length; ++i) { 4 | if (arr[i] < min) { 5 | min = arr[i]; 6 | } 7 | } 8 | return min; 9 | } 10 | -------------------------------------------------------------------------------- /Chapter13/Chap13-7.js: -------------------------------------------------------------------------------- 1 | function findMax(arr) { 2 | var max = arr[0]; 3 | for (var i = 1; i < arr.length; ++i) { 4 | if (arr[i] > max) { 5 | max = arr[i]; 6 | } 7 | } 8 | return max; 9 | } 10 | -------------------------------------------------------------------------------- /Chapter5/chap5-6.js: -------------------------------------------------------------------------------- 1 | var nums = []; 2 | for (var i = 0; i < 100; ++i) { 3 | nums[i] = Math.floor(Math.random() * 101); 4 | } 5 | var minValue = findMin(nums); 6 | dispArr(nums); 7 | print(); 8 | print("The minimum value is: " + minValue); -------------------------------------------------------------------------------- /Chapter13/Chap13-6.js: -------------------------------------------------------------------------------- 1 | var nums = []; 2 | for (var i = 0; i < 100; ++i) { 3 | nums[i] = Math.floor(Math.random() * 101); 4 | } 5 | var minValue = findMin(nums); 6 | dispArr(nums); 7 | print(); 8 | print("The minimum value is: " + minValue); 9 | -------------------------------------------------------------------------------- /Chapter4/chap4-11.js: -------------------------------------------------------------------------------- 1 | var start = new Date().getTime(); 2 | for (var i = 1; i < 100; ++i) { 3 | print(i); 4 | } 5 | var stop = new Date().getTime(); 6 | var elapsed = stop - start; 7 | print("The elapsed time was: " + elapsed + 8 | " milliseconds."); -------------------------------------------------------------------------------- /Chapter12/Chap12-8.js: -------------------------------------------------------------------------------- 1 | var start = new Date().getTime(); 2 | for (var i = 1; i < 100; ++i) { 3 | print(i); 4 | } 5 | var stop = new Date().getTime(); 6 | var elapsed = stop - start; 7 | print("The elapsed time was: " + elapsed + 8 | " milliseconds."); 9 | -------------------------------------------------------------------------------- /Chapter2/chap2_6.js: -------------------------------------------------------------------------------- 1 | function isEven(num) { 2 | return num % 2 == 0; 3 | } 4 | 5 | var nums = [2,4,6,8,10]; 6 | var even = nums.every(isEven); 7 | if (even) { 8 | print("all numbers are even"); 9 | } 10 | else { 11 | print("some numbers are odd"); 12 | } -------------------------------------------------------------------------------- /Chapter7/Chap7-2.js: -------------------------------------------------------------------------------- 1 | load("Dictionary.js"); 2 | var pbook = new Dictionary(); 3 | pbook.add("Mike","123"); 4 | pbook.add("David", "345"); 5 | pbook.add("Cynthia", "456"); 6 | print("David's extension: " + pbook.find("David")); 7 | pbook.remove("David"); 8 | pbook.showAll(); -------------------------------------------------------------------------------- /Chapter10/Chap10-2.js: -------------------------------------------------------------------------------- 1 | load("bst.js"); 2 | var nums = new BST(); 3 | nums.insert(23); 4 | nums.insert(45); 5 | nums.insert(16); 6 | nums.insert(37); 7 | nums.insert(3); 8 | nums.insert(99); 9 | nums.insert(22); 10 | print("Inorder traversal: "); 11 | inOrder(nums.root); 12 | -------------------------------------------------------------------------------- /Chapter5/chap5-9.js: -------------------------------------------------------------------------------- 1 | function seqSearch(arr, data) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | if (arr[i] == data) { 4 | if (i > 0) { 5 | swap(i,i-1); 6 | } 7 | return true; 8 | } 9 | } 10 | return false; 11 | } -------------------------------------------------------------------------------- /Chapter12/Chap12-10.js: -------------------------------------------------------------------------------- 1 | load("CArray.js") 2 | var nums = new CArray(10); 3 | nums.setData(); 4 | print("Before Shellsort: \n"); 5 | print(nums.toString()); 6 | print("\nDuring Shellsort: \n"); 7 | nums.shellsort(); 8 | print("\nAfter Shellsort: \n"); 9 | print(nums.toString()); 10 | -------------------------------------------------------------------------------- /Chapter2/chap2_9.js: -------------------------------------------------------------------------------- 1 | function passing(num) { 2 | return num >= 60; 3 | } 4 | 5 | var grades = []; 6 | for (var i = 0; i < 20; ++i) { 7 | grades[i] = Math.floor(Math.random() * 101); 8 | } 9 | var passGrades = grades.filter(passing); 10 | print(grades); 11 | print(passGrades); -------------------------------------------------------------------------------- /Chapter13/Chap13-9.js: -------------------------------------------------------------------------------- 1 | function seqSearch(arr, data) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | if (arr[i] == data) { 4 | if (i > 0) { 5 | swap(arr,i,i-1); 6 | } 7 | return true; 8 | } 9 | } 10 | return false; 11 | } 12 | -------------------------------------------------------------------------------- /Chapter2/chap2_3.js: -------------------------------------------------------------------------------- 1 | var names = ["David","Mike","Cynthia","Raymond","Clayton","Mike","Jennifer"]; 2 | var name = "Mike"; 3 | var firstPos = names.indexOf(name); 4 | print("First found " + name + " at position " + firstPos); 5 | var lastPos = names.lastIndexOf(name); 6 | print("Last found " + name + " at position " + lastPos); -------------------------------------------------------------------------------- /Chapter8/Chap8-1.js: -------------------------------------------------------------------------------- 1 | load("HashTable.js"); 2 | var someNames = ["David", "Jennifer", "Donnie", "Raymond", 3 | "Cynthia", "Mike", "Clayton", "Danny", "Jonathan"]; 4 | var hTable = new HashTable(); 5 | for (var i = 0; i < someNames.length; ++i) { 6 | hTable.put(someNames[i]); 7 | } 8 | hTable.showDistro(); -------------------------------------------------------------------------------- /Chapter8/Chap8-3.js: -------------------------------------------------------------------------------- 1 | load("HashTable.js"); 2 | var someNames = ["David", "Jennifer", "Donnie", "Raymond", 3 | "Cynthia", "Mike", "Clayton", "Danny", "Jonathan"]; 4 | var hTable = new HashTable(); 5 | for (var i = 0; i < someNames.length; ++i) { 6 | hTable.put(someNames[i]); 7 | } 8 | htable.showDistro(); -------------------------------------------------------------------------------- /Chapter7/Chap7-5.js: -------------------------------------------------------------------------------- 1 | load("Dictionary.js"); 2 | var pbook = new Dictionary(); 3 | pbook.add("Raymond","123"); 4 | pbook.add("David", "345"); 5 | pbook.add("Cynthia", "456"); 6 | pbook.add("Mike", "723"); 7 | pbook.add("Jennifer", "987"); 8 | pbook.add("Danny", "012"); 9 | pbook.add("Jonathan", "666"); 10 | pbook.showAll(); -------------------------------------------------------------------------------- /Chapter5/chap5-10.js: -------------------------------------------------------------------------------- 1 | function seqSearch(arr, data) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | if (arr[i] == data && i > (arr.length * 0.2)) { 4 | swap(arr,i,0); 5 | return true; 6 | } 7 | else if (arr[i] == data) { 8 | return true; 9 | } 10 | } 11 | return false; 12 | } -------------------------------------------------------------------------------- /Chapter6/chap6-2.txt: -------------------------------------------------------------------------------- 1 | load("llist.js"); 2 | var cities = new LList(); 3 | cities.insert("Conway", "head"); 4 | cities.insert("Russellville", "Conway"); 5 | cities.insert("Carlisle", "Russellville"); 6 | cities.insert("Alma", "Carlisle"); 7 | cities.display(); 8 | console.log(); 9 | cities.remove("Carlisle"); 10 | cities.display(); 11 | -------------------------------------------------------------------------------- /Chapter9/Chap9-3.js: -------------------------------------------------------------------------------- 1 | load("set.js"); 2 | var cis = new Set(); 3 | cis.add("Mike"); 4 | cis.add("Clayton"); 5 | cis.add("Jennifer"); 6 | cis.add("Raymond"); 7 | var dmp = new Set(); 8 | dmp.add("Raymond"); 9 | dmp.add("Cynthia"); 10 | dmp.add("Bryan"); 11 | var inter = cis.intersect(dmp); 12 | print(inter.show()); // displays Raymond 13 | -------------------------------------------------------------------------------- /Chapter13/Chap13-10.js: -------------------------------------------------------------------------------- 1 | function seqSearch(arr, data) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | if (arr[i] == data && i > (arr.length * 0.2)) { 4 | swap(arr,i,0); 5 | return true; 6 | } 7 | else if (arr[i] == data) { 8 | return true; 9 | } 10 | } 11 | return false; 12 | } 13 | -------------------------------------------------------------------------------- /Chapter2/chap2_2.js: -------------------------------------------------------------------------------- 1 | var names = ["David","Cynthia","Raymond","Clayton","Jennifer"]; 2 | putstr("Enter a name to search for: "); 3 | var name = readline(); 4 | var position = names.indexOf(name); 5 | if (position >= 0) { 6 | print("Found " + name + " at position " + position); 7 | } 8 | else { 9 | print(name + " not found in array."); 10 | } -------------------------------------------------------------------------------- /Chapter5/chap5-8.js: -------------------------------------------------------------------------------- 1 | var nums = []; 2 | for (var i = 0; i < 100; ++i) { 3 | nums[i] = Math.floor(Math.random() * 101); 4 | } 5 | var minValue = findMin(nums); 6 | dispArr(nums); 7 | print(); 8 | print(); 9 | print("The minimum value is: " + minValue); 10 | var maxValue = findMax(nums); 11 | print(); 12 | print("The maximum value is: " + maxValue); -------------------------------------------------------------------------------- /Chapter8/Chap8-5.js: -------------------------------------------------------------------------------- 1 | load("HashTable.js"); 2 | var hTable = new HashTable(); 3 | hTable.buildChains(); 4 | var someNames = ["David", "Jennifer", "Donnie", "Raymond", 5 | "Cynthia", "Mike", "Clayton", "Danny", "Jonathan"]; 6 | for (var i = 0; i < someNames.length; ++i) { 7 | hTable.put(someNames[i]); 8 | } 9 | hTable.showDistro(); 10 | -------------------------------------------------------------------------------- /Chapter11/Chap11-7.js: -------------------------------------------------------------------------------- 1 | load("Graph.js"); 2 | g = new Graph(6); 3 | g.addEdge(1,2); 4 | g.addEdge(2,5); 5 | g.addEdge(1,3); 6 | g.addEdge(1,4); 7 | g.addEdge(0,1); 8 | g.vertexList = ["CS1", "CS2", "Data Structures", 9 | "Assembly Language", "Operating Systems", 10 | "Algorithms"]; 11 | g.showGraph(); 12 | g.topSort(); 13 | -------------------------------------------------------------------------------- /Chapter13/Chap13-8.js: -------------------------------------------------------------------------------- 1 | var nums = []; 2 | for (var i = 0; i < 100; ++i) { 3 | nums[i] = Math.floor(Math.random() * 101); 4 | } 5 | var minValue = findMin(nums); 6 | dispArr(nums); 7 | print(); 8 | print(); 9 | print("The minimum value is: " + minValue); 10 | var maxValue = findMax(nums); 11 | print(); 12 | print("The maximum value is: " + maxValue); 13 | -------------------------------------------------------------------------------- /Chapter13/Chap13-15.js: -------------------------------------------------------------------------------- 1 | var nums = []; 2 | for (var i = 0; i < 100; ++i) { 3 | nums[i] = Math.floor(Math.random() * 101); 4 | } 5 | insertionsort(nums); 6 | dispArr(nums); 7 | print(); 8 | putstr("Enter a value to count: "); 9 | var val = parseInt(readline()); 10 | var retVal = count(nums, val); 11 | print("Found " + retVal + " occurrences of " + val + "."); 12 | -------------------------------------------------------------------------------- /Chapter5/chap5-15.js: -------------------------------------------------------------------------------- 1 | var nums = []; 2 | for (var i = 0; i < 100; ++i) { 3 | nums[i] = Math.floor(Math.random() * 101); 4 | } 5 | insertionsort(nums); 6 | dispArr(nums); 7 | print(); 8 | putstr("Enter a value to count: "); 9 | var val = parseInt(readline()); 10 | var retVal = count(nums, val); 11 | print("Found " + retVal + " occurrences of " + val + "."); 12 | -------------------------------------------------------------------------------- /Chapter7/Chap7-4.js: -------------------------------------------------------------------------------- 1 | load("Dictionary.js"); 2 | var pbook = new Dictionary(); 3 | pbook.add("Raymond","123"); 4 | pbook.add("David", "345"); 5 | pbook.add("Cynthia", "456"); 6 | print("Number of entries: " + pbook.count()); 7 | print("David's extension: " + pbook.find("David")); 8 | pbook.showAll(); 9 | pbook.clear(); 10 | print("Number of entries: " + pbook.count()); -------------------------------------------------------------------------------- /Chapter11/Chap11-4.js: -------------------------------------------------------------------------------- 1 | load("Graph.js"); 2 | g = new Graph(5); 3 | g.addEdge(0,1); 4 | g.addEdge(0,2); 5 | g.addEdge(1,3); 6 | g.addEdge(2,4); 7 | var vertex = 4; 8 | var paths = g.pathTo(vertex); 9 | while (paths.length > 0) { 10 | if (paths.length > 1) { 11 | putstr(paths.pop() + '-'); 12 | } 13 | else { 14 | putstr(paths.pop()); 15 | } 16 | } -------------------------------------------------------------------------------- /Chapter9/Chap9-2.js: -------------------------------------------------------------------------------- 1 | load("set.js"); 2 | var cis = new Set(); 3 | cis.add("Mike"); 4 | cis.add("Clayton"); 5 | cis.add("Jennifer"); 6 | cis.add("Raymond"); 7 | var dmp = new Set(); 8 | dmp.add("Raymond"); 9 | dmp.add("Cynthia"); 10 | dmp.add("Jonathan"); 11 | var it = new Set(); 12 | it = cis.union(dmp); 13 | print(it.show()); 14 | //displays Mike,Clayton,Jennifer,Raymond,Cynthia,Jonathan -------------------------------------------------------------------------------- /Chapter9/Chap9-5.js: -------------------------------------------------------------------------------- 1 | load("set.js"); 2 | var cis = new Set(); 3 | var it = new Set(); 4 | cis.add("Clayton"); 5 | cis.add("Jennifer"); 6 | cis.add("Danny"); 7 | it.add("Bryan"); 8 | it.add("Clayton"); 9 | it.add("Jennifer"); 10 | var diff = new Set(); 11 | diff = cis.difference(it); 12 | print("[" + cis.show() + "] difference [" + it.show() 13 | + "] -> [" + diff.show() + "]"); 14 | -------------------------------------------------------------------------------- /Chapter12/Chap12-3.js: -------------------------------------------------------------------------------- 1 | function bubbleSort() { 2 | var numElements = this.dataStore.length; 3 | var temp; 4 | for (var outer = numElements; outer >= 2; --outer) { 5 | for (var inner = 0; inner <= outer-1; ++inner) { 6 | if (this.dataStore[inner] > this.dataStore[inner+1]) { 7 | swap(this.dataStore, inner, inner+1); 8 | } 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter3/chap3_2.js: -------------------------------------------------------------------------------- 1 | var movies = createArr("films.txt"); 2 | var movieList = new List(); 3 | var customers = new List(); 4 | for (var i = 0; i < movies.length; ++i) { 5 | movieList.append(movies[i]); 6 | } 7 | print("Available movies: \n"); 8 | displayList(movieList); 9 | checkOut("Jane Doe", "The Godfather", movieList, customers); 10 | print("\nCustomer Rentals: \n"); 11 | displayList(customers); -------------------------------------------------------------------------------- /Chapter10/Chap10-3.js: -------------------------------------------------------------------------------- 1 | load("bst.js"); 2 | var nums = new BST(); 3 | nums.insert(23); 4 | nums.insert(45); 5 | nums.insert(16); 6 | nums.insert(37); 7 | nums.insert(3); 8 | nums.insert(99); 9 | nums.insert(22); 10 | var min = nums.getmin(); 11 | print("The minimum value of the BST is: " + min); 12 | print("\n"); 13 | var max = nums.getmax(); 14 | print("The maximum value of the BST is: " + max); 15 | -------------------------------------------------------------------------------- /Chapter2/chap2_8.js: -------------------------------------------------------------------------------- 1 | function isEven(num) { 2 | return num % 2 == 0; 3 | } 4 | 5 | function isOdd(num) { 6 | return num % 2 != 0; 7 | } 8 | 9 | var nums = []; 10 | for (var i = 0; i < 20; ++i) { 11 | nums[i] = i+1; 12 | } 13 | var evens = nums.filter(isEven); 14 | print("Even numbers: "); 15 | print(evens); 16 | var odds = nums.filter(isOdd); 17 | print("Odd numbers: "); 18 | print(odds); -------------------------------------------------------------------------------- /Chapter5/chap5-11.js: -------------------------------------------------------------------------------- 1 | var nums = []; 2 | for (var i = 0; i < 10; ++i) { 3 | nums[i] = Math.floor(Math.random() * 11); 4 | } 5 | dispArr(nums); 6 | print(); 7 | putstr("Enter a value to search for: "); 8 | var val = parseInt(readline()); 9 | if (seqSearch(nums, val)) { 10 | print("Found element: "); 11 | print(); 12 | dispArr(nums); 13 | } 14 | else { 15 | print(val + " is not in array."); 16 | } -------------------------------------------------------------------------------- /Chapter13/Chap13-11.js: -------------------------------------------------------------------------------- 1 | var nums = []; 2 | for (var i = 0; i < 10; ++i) { 3 | nums[i] = Math.floor(Math.random() * 11); 4 | } 5 | dispArr(nums); 6 | print(); 7 | putstr("Enter a value to search for: "); 8 | var val = parseInt(readline()); 9 | if (seqSearch(nums, val)) { 10 | print("Found element: "); 11 | print(); 12 | dispArr(nums); 13 | } 14 | else { 15 | print(val + " is not in array."); 16 | } 17 | -------------------------------------------------------------------------------- /Chapter4/chap4-10.js: -------------------------------------------------------------------------------- 1 | function insertionsort() { 2 | var temp, inner; 3 | for (var outer = 1; outer <= this.dataStore.length-1; ++outer) { 4 | temp = this.dataStore[outer]; 5 | inner = outer; 6 | while (inner > 0 && (this.dataStore[inner-1] >= temp)) { 7 | this.dataStore[inner] = this.dataStore[inner-1]; 8 | --inner; 9 | } 10 | this.dataStore[inner] = temp; 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter12/Chap12-5.js: -------------------------------------------------------------------------------- 1 | function bubbleSort() { 2 | var numElements = this.dataStore.length; 3 | var temp; 4 | for (var outer = numElements; outer >= 2; --outer) { 5 | for (var inner = 0; inner <= outer-1; ++inner) { 6 | if (this.dataStore[inner] > this.dataStore[inner+1]) { 7 | swap(this.dataStore, inner, inner+1); 8 | } 9 | } 10 | print(this.toString()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Chapter12/Chap12-7.js: -------------------------------------------------------------------------------- 1 | function insertionSort() { 2 | var temp, inner; 3 | for (var outer = 1; outer <= this.dataStore.length-1; ++outer) { 4 | temp = this.dataStore[outer]; 5 | inner = outer; 6 | while (inner > 0 && (this.dataStore[inner-1] >= temp)) { 7 | this.dataStore[inner] = this.dataStore[inner-1]; 8 | --inner; 9 | } 10 | this.dataStore[inner] = temp; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Chapter5/chap5-4.js: -------------------------------------------------------------------------------- 1 | var nums = []; 2 | for (var i = 0; i < 100; ++i) { 3 | nums[i] = Math.floor(Math.random() * 101); 4 | } 5 | putstr("Enter a number to search for: "); 6 | var num = readline(); 7 | print(); 8 | var position = seqSearch(nums, num); 9 | if (position > -1) { 10 | print(num + " is in the array at position " + position); 11 | } 12 | else { 13 | print(num + " is not in the array."); 14 | } 15 | print(); 16 | dispArr(nums); -------------------------------------------------------------------------------- /Chapter13/Chap13-4.js: -------------------------------------------------------------------------------- 1 | var nums = []; 2 | for (var i = 0; i < 100; ++i) { 3 | nums[i] = Math.floor(Math.random() * 101); 4 | } 5 | putstr("Enter a number to search for: "); 6 | var num = readline(); 7 | print(); 8 | var position = seqSearch(nums, num); 9 | if (position > -1) { 10 | print(num + " is in the array at position " + position); 11 | } 12 | else { 13 | print(num + " is not in the array."); 14 | } 15 | print(); 16 | dispArr(nums); 17 | -------------------------------------------------------------------------------- /Chapter12/Chap12-6.js: -------------------------------------------------------------------------------- 1 | function selectionSort() { 2 | var min, temp; 3 | for (var outer = 0; outer <= this.dataStore.length-2; ++outer) { 4 | min = outer; 5 | for (var inner = outer + 1; 6 | inner <= this.dataStore.length-1; ++inner) { 7 | if (this.dataStore[inner] < this.dataStore[min]) { 8 | min = inner; 9 | } 10 | } 11 | swap(this.dataStore, outer, min); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Chapter2/chap2_10.js: -------------------------------------------------------------------------------- 1 | var grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]]; 2 | var total = 0; 3 | var average = 0.0; 4 | for (var row = 0; row < grades.length; ++row) { 5 | for (var col = 0; col < grades[row].length; ++col) { 6 | total += grades[row][col]; 7 | } 8 | average = total / grades[row].length; 9 | print("Student " + parseInt(row+1) + " average: " + 10 | average.toFixed(2)); 11 | total = 0; 12 | average = 0.0; 13 | } -------------------------------------------------------------------------------- /Chapter2/chap2_12.js: -------------------------------------------------------------------------------- 1 | var grades = [[89, 77],[76, 82, 81],[91, 94, 89, 99]]; 2 | var total = 0; 3 | var average = 0.0; 4 | for (var row = 0; row < grades.length; ++row) { 5 | for (var col = 0; col < grades[row].length; ++col) { 6 | total += grades[row][col]; 7 | } 8 | average = total / grades[row].length; 9 | print("Student " + parseInt(row+1) + " average: " + 10 | average.toFixed(2)); 11 | total = 0; 12 | average = 0.0; 13 | } -------------------------------------------------------------------------------- /Chapter2/chap2_11.js: -------------------------------------------------------------------------------- 1 | var grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]]; 2 | var total = 0; 3 | var average = 0.0; 4 | for (var col = 0; col < grades.length; ++col) { 5 | for (var row = 0; row < grades[col].length; ++row) { 6 | total += grades[row][col]; 7 | } 8 | 9 | 10 | average = total / grades[col].length; 11 | print("Test " + parseInt(col+1) + " average: " + 12 | average.toFixed(2)); 13 | total = 0; 14 | average = 0.0; 15 | } -------------------------------------------------------------------------------- /Chapter2/chap2_7.js: -------------------------------------------------------------------------------- 1 | function isEven(num) { 2 | return num % 2 == 0; 3 | } 4 | 5 | var nums = [1,2,3,4,5,6,7,8,9,10]; 6 | var someEven = nums.some(isEven); 7 | if (someEven) { 8 | print("some numbers are even"); 9 | } 10 | else { 11 | print("no numbers are even"); 12 | } 13 | nums = [1,3,5,7,9]; 14 | var someEven = nums.some(isEven); 15 | if (someEven) { 16 | print("some numbers are even"); 17 | } 18 | else { 19 | print("no numbers are even"); 20 | } -------------------------------------------------------------------------------- /Chapter9/Chap9-4.js: -------------------------------------------------------------------------------- 1 | load("set.js"); 2 | var it = new Set(); 3 | it.add("Cynthia"); 4 | it.add("Clayton"); 5 | it.add("Jennifer"); 6 | it.add("Danny"); 7 | it.add("Jonathan"); 8 | it.add("Terrill"); 9 | it.add("Raymond"); 10 | it.add("Mike"); 11 | var dmp = new Set(); 12 | dmp.add("Cynthia"); 13 | dmp.add("Raymond"); 14 | dmp.add("Jonathan"); 15 | if (dmp.subset(it)) { 16 | print("DMP is a subset of IT."); 17 | } 18 | else { 19 | print("DMP is not a subset of IT."); 20 | } -------------------------------------------------------------------------------- /Chapter4/chap4-6.js: -------------------------------------------------------------------------------- 1 | function bubblesort() { 2 | var numElements = this.dataStore.length; 3 | var temp; 4 | for (var outer = numElements; outer >= 2; --outer) { 5 | for (var inner = 0; inner <= outer-1; ++inner) { 6 | if (this.dataStore[inner] > this.dataStore[inner+1]) { 7 | temp = this.dataStore[inner]; 8 | this.dataStore[inner] = this.dataStore[inner+1]; 9 | this.dataStore[inner+1] = temp; 10 | } 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter12/Chap12-12.js: -------------------------------------------------------------------------------- 1 | load("CArray.js"); 2 | var nums = new CArray(10000); 3 | nums.setData(); 4 | var start = new Date().getTime(); 5 | nums.shellsort(); 6 | var stop = new Date().getTime(); 7 | var elapsed = stop - start; 8 | print("Shellsort with hard-coded gap sequence: " + elapsed + " ms."); 9 | nums.clear(); 10 | nums.setData(); 11 | start = new Date().getTime(); 12 | nums.shellsort1(); 13 | stop = new Date().getTime(); 14 | print("Shellsort with dynamic gap sequence: " + elapsed + " ms."); 15 | -------------------------------------------------------------------------------- /Chapter12/Chap12Shellsort.js: -------------------------------------------------------------------------------- 1 | function shellsort() { 2 | for (var g = 0; g < this.gaps.length; ++g) { 3 | for (var i = this.gaps[g]; i < this.dataStore.length; ++i) { 4 | var temp = this.dataStore[i]; 5 | for (var j = i; j >= this.gaps[g] && 6 | this.dataStore[j-this.gaps[g]] > temp; 7 | j -= this.gaps[g]) { 8 | this.dataStore[j] = this.dataStore[j - this.gaps[g]]; 9 | } 10 | this.dataStore[j] = temp; 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter4/chap4-8.js: -------------------------------------------------------------------------------- 1 | function bubblesort() { 2 | var numElements = this.dataStore.length; 3 | var temp; 4 | for (var outer = numElements; outer >= 2; --outer) { 5 | for (var inner = 0; inner <= outer-1; ++inner) { 6 | if (this.dataStore[inner] > this.dataStore[inner+1]) { 7 | temp = this.dataStore[inner]; 8 | this.dataStore[inner] = this.dataStore[inner+1]; 9 | this.dataStore[inner+1] = temp; 10 | } 11 | } 12 | print(this.toString()); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter10/Chap10-4.js: -------------------------------------------------------------------------------- 1 | load("bst.js"); 2 | var nums = new BST(); 3 | nums.insert(23); 4 | nums.insert(45); 5 | nums.insert(16); 6 | nums.insert(37); 7 | nums.insert(3); 8 | nums.insert(99); 9 | nums.insert(22); 10 | inOrder(nums.root); 11 | print("\n"); 12 | putstr("Enter a value to search for: "); 13 | var value = parseInt(readline()); 14 | var found = nums.find(value); 15 | if (found != null) { 16 | print("Found " + value + " in the BST."); 17 | } 18 | else { 19 | print(value + " was not found in the BST."); 20 | } 21 | -------------------------------------------------------------------------------- /Chapter13/Chap13-13.js: -------------------------------------------------------------------------------- 1 | function binSearch(arr, data) { 2 | var upperBound = arr.length-1; 3 | var lowerBound = 0; 4 | while (lowerBound <= upperBound) { 5 | var mid = Math.floor((upperBound + lowerBound) / 2); 6 | print("Current midpoint: " + mid); 7 | if (arr[mid] < data) { 8 | lowerBound = mid + 1; 9 | } 10 | else if (arr[mid] > data) { 11 | upperBound = mid - 1; 12 | } 13 | else { 14 | return mid; 15 | } 16 | } 17 | return -1; 18 | } 19 | -------------------------------------------------------------------------------- /Chapter5/chap5-13.js: -------------------------------------------------------------------------------- 1 | function binSearch(arr, data) { 2 | var upperBound = arr.length-1; 3 | var lowerBound = 0; 4 | while (lowerBound <= upperBound) { 5 | var mid = Math.floor((upperBound + lowerBound) / 2); 6 | print("Current midpoint: " + mid); 7 | if (arr[mid] < data) { 8 | lowerBound = mid + 1; 9 | } 10 | else if (arr[mid] > data) { 11 | upperBound = mid - 1; 12 | } 13 | else { 14 | return mid; 15 | } 16 | } 17 | return -1; 18 | } 19 | -------------------------------------------------------------------------------- /Chapter4/chap4-9.js: -------------------------------------------------------------------------------- 1 | function selectionsort() { 2 | var min, temp; 3 | for (var outer = 0; outer <= this.dataStore.length-2; ++outer) { 4 | min = outer; 5 | for (var inner = outer + 1; 6 | inner <= this.dataStore.length-1; ++inner) { 7 | if (this.dataStore[inner] < this.dataStore[min]) { 8 | min = inner; 9 | } 10 | } 11 | temp = this.dataStore[outer]; 12 | this.dataStore[outer] = this.dataStore[min]; 13 | this.dataStore[min] = temp; 14 | } 15 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | data_structures_and_algorithms_using_javascript 2 | =============================================== 3 | 4 | This is the example code than accompanies Data Structures and Algorithms with JavaScript by Michael McMillan (9781449364939). 5 | 6 | Click the Download Zip button to the right to download example code. 7 | 8 | Visit the catalog page [here](http://shop.oreilly.com/product/0636920029557.do). 9 | 10 | See an error? Report it [here](http://oreilly.com/catalog/errata.csp?isbn=0636920029557), or simply fork and send us a pull request. 11 | -------------------------------------------------------------------------------- /Chapter3/chap3_3.js: -------------------------------------------------------------------------------- 1 | var movies = createArr("films.txt"); 2 | var movieList = new List(); 3 | var customers = new List(); 4 | for (var i = 0; i < movies.length; ++i) { 5 | movieList.append(movies[i]); 6 | } 7 | print("Available movies: \n"); 8 | displayList(movieList); 9 | putstr("\nEnter your name: "); 10 | var name = readline(); 11 | putstr("What movie would you like? "); 12 | var movie = readline(); 13 | checkOut(name, movie, movieList, customers); 14 | print("\nCustomer Rentals: \n"); 15 | displayList(customers); 16 | print("\nMovies Now Available\n"); 17 | displayList(movieList); -------------------------------------------------------------------------------- /Chapter5/chap5-14.js: -------------------------------------------------------------------------------- 1 | function count(arr, data) { 2 | var count = 0; 3 | var position = binSearch(arr, data); 4 | if (position > -1) { 5 | ++count; 6 | for (var i = position-1; i > 0; --i) { 7 | if (arr[i] == data) { 8 | ++count; 9 | } 10 | else { 11 | break; 12 | } 13 | } 14 | for (var i = position+1; i < arr.length; ++i) { 15 | if (arr[i] == data) { 16 | ++count; 17 | } 18 | else { 19 | break; 20 | } 21 | } 22 | } 23 | return count; 24 | } -------------------------------------------------------------------------------- /Chapter13/Chap13-14.js: -------------------------------------------------------------------------------- 1 | function count(arr, data) { 2 | var count = 0; 3 | var position = binSearch(arr, data); 4 | if (position > -1) { 5 | ++count; 6 | for (var i = position-1; i > 0; --i) { 7 | if (arr[i] == data) { 8 | ++count; 9 | } 10 | else { 11 | break; 12 | } 13 | } 14 | for (var i = position+1; i < arr.length; ++i) { 15 | if (arr[i] == data) { 16 | ++count; 17 | } 18 | else { 19 | break; 20 | } 21 | } 22 | } 23 | return count; 24 | } 25 | -------------------------------------------------------------------------------- /Chapter5/chap5-2.js: -------------------------------------------------------------------------------- 1 | function dispArr(arr) { 2 | putstr(arr[0] + " "); 3 | for (var i = 1; i < arr.length; ++i) { 4 | putstr(arr[i] + " "); 5 | if (i % 10 == 0) { 6 | putstr("\n"); 7 | } 8 | } 9 | } 10 | 11 | var nums = []; 12 | for (var i = 0; i < 100; ++i) { 13 | nums[i] = Math.floor(Math.random() * 101); 14 | } 15 | putstr("Enter a number to search for: "); 16 | var num = readline(); 17 | print(); 18 | if (seqSearch(nums, num)) { 19 | print(num + " is in the array."); 20 | } 21 | else { 22 | print(num + " is not in the array."); 23 | } 24 | print(); 25 | dispArr(nums); -------------------------------------------------------------------------------- /Chapter12/Chap12-15.js: -------------------------------------------------------------------------------- 1 | function qSort(arr) 2 | { 3 | if (arr.length == 0) { 4 | return []; 5 | } 6 | var left = []; 7 | var right = []; 8 | var pivot = arr[0]; 9 | for (var i = 1; i < arr.length; i++) { 10 | 11 | if (arr[i] < pivot) { 12 | 13 | left.push(arr[i]); 14 | } else { 15 | 16 | right.push(arr[i]); 17 | } 18 | } 19 | return qSort(left).concat(pivot, qSort(right)); 20 | } 21 | var a = []; 22 | for (var i = 0; i < 10; ++i) { 23 | a[i] = Math.floor((Math.random()*100)+1); 24 | } 25 | print(a); 26 | print(); 27 | print(qSort(a)); 28 | -------------------------------------------------------------------------------- /Chapter13/Chap13-16.js: -------------------------------------------------------------------------------- 1 | function seqSearch(arr, data) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | if (arr[i] == data) { 4 | return i; 5 | } 6 | } 7 | return -1; 8 | } 9 | 10 | var words = read("words.txt").split(" "); 11 | var word = "rhetoric"; 12 | var start = new Date().getTime(); 13 | var position = seqSearch(words, word); 14 | var stop = new Date().getTime(); 15 | var elapsed = stop - start; 16 | if (position >= 0) { 17 | print("Found " + word + " at position " + position + "."); 18 | print("Sequential search took " + elapsed + " milliseconds."); 19 | } 20 | else { 21 | print(word + " is not in the file."); 22 | } 23 | -------------------------------------------------------------------------------- /Chapter2/chap2_13.js: -------------------------------------------------------------------------------- 1 | function Point(x,y) { 2 | this.x = x; 3 | this.y = y; 4 | } 5 | 6 | function displayPts(arr) { 7 | for (var i = 0; i < arr.length; ++i) { 8 | print(arr[i].x + ", " + arr[i].y); 9 | } 10 | } 11 | 12 | var p1 = new Point(1,2); 13 | var p2 = new Point(3,5); 14 | var p3 = new Point(2,8); 15 | var p4 = new Point(4,4); 16 | var points = [p1,p2,p3,p4]; 17 | for (var i = 0; i < points.length; ++i) { 18 | print("Point " + parseInt(i+1) + ": " + points[i].x + ", " + 19 | points[i].y); 20 | } 21 | var p5 = new Point(12,-3); 22 | points.push(p5); 23 | displayPts(points); 24 | points.shift(); 25 | displayPts(points); 26 | -------------------------------------------------------------------------------- /Chapter5/chap5-16.js: -------------------------------------------------------------------------------- 1 | function seqSearch(arr, data) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | if (arr[i] == data) { 4 | return i; 5 | } 6 | } 7 | return -1; 8 | } 9 | 10 | var words = read("words.txt").split(" "); 11 | var word = "rhetoric"; 12 | var start = new Date().getTime(); 13 | var position = seqSearch(words, word); 14 | var stop = new Date().getTime(); 15 | var elapsed = stop - start; 16 | if (position >= 0) { 17 | print("Found " + word + " at position " + position + "."); 18 | print("Sequential search took " + elapsed + " milliseconds."); 19 | } 20 | else { 21 | print(word + " is not in the file."); 22 | } 23 | -------------------------------------------------------------------------------- /Chapter13/Chap13-2.js: -------------------------------------------------------------------------------- 1 | function dispArr(arr) { 2 | for (var i = 0; i < arr.length; ++i) { 3 | putstr(arr[i] + " "); 4 | if (i % 10 == 9) { 5 | putstr("\n"); 6 | } 7 | } 8 | if (i % 10 != 0) { 9 | putstr("\n"); 10 | } 11 | } 12 | 13 | var nums = []; 14 | for (var i = 0; i < 100; ++i) { 15 | nums[i] = Math.floor(Math.random() * 101); 16 | } 17 | dispArr(nums); 18 | putstr("Enter a number to search for: "); 19 | var num = parseInt(readline()); 20 | print(); 21 | if (seqSearch(nums, num)) { 22 | print(num + " is in the array."); 23 | } 24 | else { 25 | print(num + " is not in the array."); 26 | } 27 | print(); 28 | dispArr(nums); 29 | -------------------------------------------------------------------------------- /Chapter12/Chap12-11.js: -------------------------------------------------------------------------------- 1 | function shellsort1() { 2 | var N = this.dataStore.length; 3 | var h = 1; 4 | while (h < N/3) { 5 | h = 3 * h + 1; 6 | } 7 | while (h >= 1) { 8 | for (var i = h; i < N; i++) { 9 | for (var j = i; j >= h && this.dataStore[j] < this.dataStore[j-h]; 10 | j -= h) { 11 | swap(this.dataStore, j, j-h); 12 | } 13 | } 14 | h = (h-1)/3; 15 | } 16 | } 17 | 18 | load("CArray.js") 19 | var nums = new CArray(100); 20 | nums.setData(); 21 | print("Before Shellsort1: \n"); 22 | print(nums.toString()); 23 | nums.shellsort1(); 24 | print("\nAfter Shellsort1: \n"); 25 | print(nums.toString()); 26 | -------------------------------------------------------------------------------- /Chapter2/chap2_14.js: -------------------------------------------------------------------------------- 1 | function weekTemps() { 2 | this.dataStore = []; 3 | this.add = add; 4 | this.average = average; 5 | } 6 | 7 | function add(temp) { 8 | this.dataStore.push(temp); 9 | } 10 | 11 | function average() { 12 | var total = 0; 13 | for (var i = 0; i < this.dataStore.length; ++i) { 14 | total += this.dataStore[i]; 15 | } 16 | return total / this.dataStore.length; 17 | } 18 | 19 | var thisWeek = new weekTemps(); 20 | thisWeek.add(52); 21 | thisWeek.add(55); 22 | thisWeek.add(61); 23 | thisWeek.add(65); 24 | thisWeek.add(55); 25 | thisWeek.add(50); 26 | thisWeek.add(52); 27 | thisWeek.add(49); 28 | print(thisWeek.average()); // displays 54.875 29 | -------------------------------------------------------------------------------- /Chapter9/Chap9-1.js: -------------------------------------------------------------------------------- 1 | load("set.js"); 2 | var names = new Set(); 3 | names.add("David"); 4 | names.add("Jennifer"); 5 | names.add("Cynthia"); 6 | names.add("Mike"); 7 | names.add("Raymond"); 8 | if (names.add("Mike")) { 9 | print("Mike added") 10 | } 11 | else { 12 | print("Can't add Mike, must already be in set"); 13 | } 14 | print(names.show()); 15 | var removed = "Mike"; 16 | if (names.remove(removed)) { 17 | print(removed + " removed."); 18 | } 19 | else { 20 | print(removed + " not removed."); 21 | } 22 | names.add("Clayton"); 23 | print(names.show()); 24 | removed = "Alisa"; 25 | if (names.remove("Mike")) { 26 | print(removed + " removed."); 27 | } 28 | else { 29 | print(removed + " not removed."); 30 | } -------------------------------------------------------------------------------- /Chapter11/Chap11-1.js: -------------------------------------------------------------------------------- 1 | function Graph(v) { 2 | this.vertices = v; 3 | this.edges = 0; 4 | this.adj = []; 5 | for (var i = 0; i < this.vertices; ++i) { 6 | this.adj[i] = []; 7 | this.adj[i].push(""); 8 | } 9 | this.addEdge = addEdge; 10 | this.showGraph = showGraph; 11 | } 12 | 13 | 14 | function addEdge(v,w) { 15 | this.adj[v].push(w); 16 | this.adj[w].push(v); 17 | this.edges++; 18 | } 19 | 20 | function showGraph() { 21 | for (var i = 0; i < this.vertices; ++i) { 22 | putstr(i + " -> "); 23 | for (var j = 0; j < this.vertices; ++j) { 24 | if (this.adj[i][j] != undefined) 25 | putstr(this.adj[i][j] + ' '); 26 | } 27 | print(); 28 | } 29 | } -------------------------------------------------------------------------------- /Chapter11/Chap11-5.js: -------------------------------------------------------------------------------- 1 | function topSort() { 2 | var stack = []; 3 | var visited = []; 4 | for (var i = 0; i < this.vertices; i++) { 5 | visited[i] = false; 6 | } 7 | for (var i = 0; i < this.vertices; i++) { 8 | if (visited[i] == false) { 9 | this.topSortHelper(i, visited, stack); 10 | } 11 | } 12 | for (var i = 0; i < stack.length; i++) { 13 | if (stack[i] != undefined && stack[i] != false) { 14 | print(this.vertexList[stack[i]]); 15 | } 16 | } 17 | } 18 | 19 | function topSortHelper(v, visited, stack) { 20 | visited[v] = true; 21 | for each (var w in this.adj[v]) { 22 | if (!visited[w]) { 23 | this.topSortHelper(visited[w], visited, stack); 24 | } 25 | } 26 | stack.push(v); 27 | } -------------------------------------------------------------------------------- /Chapter10/Chap10-5.js: -------------------------------------------------------------------------------- 1 | load("bst.js"); 2 | var grades = genArray(100); 3 | prArray(grades); 4 | var gradedistro = new BST(); 5 | for (var i = 0; i < grades.length; ++i) { 6 | var g = grades[i]; 7 | var grade = gradedistro.find(g); 8 | if (grade == null) { 9 | gradedistro.insert(g); 10 | } 11 | else { 12 | gradedistro.update(g); 13 | } 14 | } 15 | var cont = "y"; 16 | 17 | 18 | while (cont == "y") { 19 | putstr("\n\nEnter a grade: "); 20 | var g = parseInt(readline()); 21 | var aGrade = gradedistro.find(g); 22 | if (aGrade == null) { 23 | print("No occurrences of " + g); 24 | } 25 | else { 26 | print("Occurrences of " + g + ": " + aGrade.count); 27 | } 28 | putstr("Look at another grade (y/n)? "); 29 | cont = readline(); 30 | } 31 | -------------------------------------------------------------------------------- /Chapter12/Chap12-9.js: -------------------------------------------------------------------------------- 1 | var numElements = 100; 2 | var nums = new CArray(numElements); 3 | nums.setData(); 4 | var start = new Date().getTime(); 5 | nums.bubbleSort(); 6 | var stop = new Date().getTime(); 7 | var elapsed = stop - start; 8 | print("Elapsed time for the bubble sort on " + 9 | numElements + " elements is: " + elapsed + " milliseconds."); 10 | start = new Date().getTime(); 11 | nums.selectionSort(); 12 | stop = new Date().getTime(); 13 | elapsed = stop - start; 14 | print("Elapsed time for the selection sort on " + 15 | numElements + " elements is: " + elapsed + " milliseconds."); 16 | start = new Date().getTime(); 17 | nums.insertionSort(); 18 | stop = new Date().getTime(); 19 | elapsed = stop - start; 20 | print("Elapsed time for the insertion sort on " + 21 | numElements + " elements is: " + elapsed + " milliseconds."); 22 | -------------------------------------------------------------------------------- /Chapter14/Chap14-1.js: -------------------------------------------------------------------------------- 1 | function recurFib(n) { 2 | if (n < 2) { 3 | return n; 4 | } 5 | else { 6 | return recurFib(n-1) + recurFib(n-2); 7 | } 8 | } 9 | 10 | function dynFib(n) { 11 | var val = []; 12 | for (var i = 0; i <= n; ++i) { 13 | val[i] = 0; 14 | } 15 | if (n == 1 || n == 2) { 16 | return 1; 17 | } 18 | else { 19 | val[1] = 1; 20 | val[2] = 2; 21 | for (var i = 3; i <= n; ++i) { 22 | val[i] = val[i-1] + val[i-2]; 23 | } 24 | return val[n-1]; 25 | } 26 | } 27 | 28 | var start = new Date().getTime(); 29 | print(recurFib(10)); 30 | var stop = new Date().getTime(); 31 | print("recursive time - " + (stop-start) + "milliseconds"); 32 | print(); 33 | start = new Date().getTime(); 34 | print(dynFib(10)); 35 | stop = new Date().getTime(); 36 | print("dynamic programming time - " + (stop-start) + " milliseconds"); 37 | -------------------------------------------------------------------------------- /Chapter14/Chap14-3.js: -------------------------------------------------------------------------------- 1 | function max(a, b) { 2 | return (a > b) ? a : b; 3 | } 4 | 5 | function dKnapsack(capacity, size, value, n) { 6 | var K = []; 7 | for (var i = 0; i <= capacity+1; i++) { 8 | K[i] = []; 9 | } 10 | for (var i = 0; i <= n; i++) { 11 | for (var w = 0; w <= capacity; w++) { 12 | if (i == 0 || w == 0) { 13 | K[i][w] = 0; 14 | } 15 | else if (size[i-1] <= w) { 16 | K[i][w] = max(value[i-1] + K[i-1][w-size[i-1]], 17 | K[i-1][w]); 18 | } 19 | else { 20 | K[i][w] = K[i-1][w]; 21 | } 22 | putstr(K[i][w] + " "); 23 | } 24 | print(); 25 | } 26 | 27 | return K[n][capacity]; 28 | } 29 | 30 | var value = [4,5,10,11,13]; 31 | var size = [3,4,7,8,9]; 32 | var capacity = 16; 33 | var n = 5; 34 | print(dKnapsack(capacity, size, value, n)); 35 | -------------------------------------------------------------------------------- /Chapter5/chap5-12.js: -------------------------------------------------------------------------------- 1 | function binSearch(arr, data) { 2 | var upperBound = arr.length-1; 3 | var lowerBound = 0; 4 | while (lowerBound <= upperBound) { 5 | var mid = Math.floor((upperBound + lowerBound) / 2); 6 | if (arr[mid] < data) { 7 | lowerBound = mid + 1; 8 | } 9 | else if (arr[mid] > data) { 10 | upperBound = mid - 1; 11 | } 12 | else { 13 | return mid; 14 | } 15 | } 16 | return -1; 17 | } 18 | 19 | var nums = []; 20 | for (var i = 0; i < 100; ++i) { 21 | nums[i] = Math.floor(Math.random() * 101); 22 | } 23 | insertionsort(nums); 24 | dispArr(nums); 25 | print(); 26 | putstr("Enter a value to search for: "); 27 | var val = parseInt(readline()); 28 | var retVal = binSearch(nums, val); 29 | if (retVal >= 0) { 30 | print("Found " + val + " at position " + retVal); 31 | } 32 | else { 33 | print(val + " is not in array."); 34 | } -------------------------------------------------------------------------------- /Chapter13/Chap13-12.js: -------------------------------------------------------------------------------- 1 | function binSearch(arr, data) { 2 | var upperBound = arr.length-1; 3 | var lowerBound = 0; 4 | while (lowerBound <= upperBound) { 5 | var mid = Math.floor((upperBound + lowerBound) / 2); 6 | if (arr[mid] < data) { 7 | lowerBound = mid + 1; 8 | } 9 | else if (arr[mid] > data) { 10 | upperBound = mid - 1; 11 | } 12 | else { 13 | return mid; 14 | } 15 | } 16 | return -1; 17 | } 18 | 19 | var nums = []; 20 | for (var i = 0; i < 100; ++i) { 21 | nums[i] = Math.floor(Math.random() * 101); 22 | } 23 | insertionsort(nums); 24 | dispArr(nums); 25 | print(); 26 | putstr("Enter a value to search for: "); 27 | var val = parseInt(readline()); 28 | var retVal = binSearch(nums, val); 29 | if (retVal >= 0) { 30 | print("Found " + val + " at position " + retVal); 31 | } 32 | else { 33 | print(val + " is not in array."); 34 | } 35 | -------------------------------------------------------------------------------- /Chapter8/Chap8-4.js: -------------------------------------------------------------------------------- 1 | function getRandomInt (min, max) { 2 | return Math.floor(Math.random() * (max - min + 1)) + min; 3 | } 4 | 5 | function genStuData(arr) { 6 | for (var i = 0; i < arr.length; ++i) { 7 | var num = ""; 8 | for (var j = 1; j <= 9; ++j) { 9 | num += Math.floor(Math.random() * 10); 10 | } 11 | num += getRandomInt(50,100); 12 | arr[i] = num; 13 | } 14 | } 15 | 16 | load("HashTable.js"); 17 | var numStudents = 10; 18 | var arrSize = 97; 19 | var idLen = 9; 20 | var students = new Array(numStudents); 21 | genStuData(students); 22 | print ("Student data: \n"); 23 | for (var i = 0; i < students.length; ++i) { 24 | print(students[i].substring(0,8) + " " + 25 | students[i].substring(9)); 26 | } 27 | print("\n\nData distribution: \n"); 28 | var hTable = new HashTable(); 29 | for (var i = 0; i < students.length; ++i) { 30 | hTable.put(students[i]); 31 | } 32 | hTable.showDistro(); 33 | -------------------------------------------------------------------------------- /Chapter7/Chap7-3.js: -------------------------------------------------------------------------------- 1 | function Dictionary() { 2 | this.add = add; 3 | this.datastore = new Array(); 4 | this.find = find; 5 | this.remove = remove; 6 | this.showAll = showAll; 7 | this.count = count; 8 | this.clear = clear; 9 | } 10 | 11 | function add(key, value) { 12 | this.datastore[key] = value; 13 | } 14 | 15 | function find(key) { 16 | return this.datastore[key]; 17 | } 18 | 19 | function remove(key) { 20 | delete this.datastore[key]; 21 | } 22 | 23 | function showAll() { 24 | for each (var key in Object.keys(this.datastore).sort()) { 25 | print(key + " -> " + this.datastore[key]); 26 | } 27 | } 28 | 29 | function count() { 30 | var n = 0; 31 | for each (var key in Object.keys(this.datastore)) { 32 | ++n; 33 | } 34 | return n; 35 | } 36 | 37 | function clear() { 38 | for each (var key in Object.keys(this.datastore)) { 39 | delete this.datastore[key]; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter7/Dictionary.js: -------------------------------------------------------------------------------- 1 | function Dictionary() { 2 | this.add = add; 3 | this.datastore = new Array(); 4 | this.find = find; 5 | this.remove = remove; 6 | this.showAll = showAll; 7 | this.count = count; 8 | this.clear = clear; 9 | } 10 | 11 | function add(key, value) { 12 | this.datastore[key] = value; 13 | } 14 | 15 | function find(key) { 16 | return this.datastore[key]; 17 | } 18 | 19 | function remove(key) { 20 | delete this.datastore[key]; 21 | } 22 | 23 | function showAll() { 24 | for each (var key in Object.keys(this.datastore).sort()) { 25 | print(key + " -> " + this.datastore[key]); 26 | } 27 | } 28 | 29 | function count() { 30 | var n = 0; 31 | for each (var key in Object.keys(this.datastore)) { 32 | ++n; 33 | } 34 | return n; 35 | } 36 | 37 | function clear() { 38 | for each (var key in Object.keys(this.datastore)) { 39 | delete this.datastore[key]; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter1/chapter1.js: -------------------------------------------------------------------------------- 1 | /*var david = { 2 | first_name: "David", 3 | last_name: "Durr" 4 | }; 5 | 6 | var student1 = { 7 | id: "1234", 8 | courses: ["Programming I", "English II", "Algebra"], 9 | advised: false 10 | }; 11 | 12 | console.log(david.last_name); // displays "Durr" 13 | console.log(student1["id"]); // displays "1234" 14 | console.log(student1.courses[1]); displays "English II" 15 | student1.advised = true; 16 | david.age = 50;*/ 17 | function Person(first, middle, last) { 18 | this.first = first; 19 | this.middle = middle; 20 | this.last = last; 21 | this.initials = initials; 22 | } 23 | 24 | function initials() { 25 | return this.first[0] + this.middle[0] + this.last[0]; 26 | } 27 | 28 | var aPerson = new Person("John","Quincy","Public"); 29 | console.log("First name: " + aPerson.first); 30 | console.log("Middle name: " + aPerson.middle); 31 | console.log("Last name: " + aPerson.last); 32 | console.log("Initials: " + aPerson.initials()); -------------------------------------------------------------------------------- /Chapter4/chap4-12.js: -------------------------------------------------------------------------------- 1 | function setData(arr, numElements) { 2 | for (var i = 0; i < numElements; ++i) { 3 | arr.insert(Math.floor(Math.random() * (numElements+1))); 4 | } 5 | } 6 | 7 | var numElements = 100; 8 | var nums = new CArray(numElements); 9 | var start = new Date().getTime(); 10 | nums.bubblesort(); 11 | var stop = new Date().getTime(); 12 | var elapsed = stop - start; 13 | print("Elapsed time for the bubble sort on " + 14 | numElements + " elements is: " + elapsed + " milliseconds."); 15 | setData(); 16 | start = new Date().getTime(); 17 | nums.selectionsort(); 18 | stop = new Date().getTime(); 19 | elapsed = stop - start; 20 | print("Elapsed time for the selection sort on " + 21 | numElements + " elements is: " + elapsed + " milliseconds."); 22 | setData(); 23 | start = new Date().getTime(); 24 | nums.insertionsort(); 25 | stop = new Date().getTime(); 26 | elapsed = stop - start; 27 | print("Elapsed time for the insertion sort on " + 28 | numElements + " elements is: " + elapsed + " milliseconds."); -------------------------------------------------------------------------------- /Chapter4/chap4-1.js: -------------------------------------------------------------------------------- 1 | function CArray(numElements) { 2 | this.dataStore = []; 3 | this.pos = 0; 4 | this.numElements = numElements; 5 | this.insert = insert; 6 | this.toString = toString; 7 | this.clear = clear; 8 | this.setData = setData; 9 | 10 | for (var i = 0; i < numElements; ++i) { 11 | this.dataStore[i] = i; 12 | } 13 | } 14 | 15 | function setData() { 16 | for (var i = 0; i < this.numElements; ++i) { 17 | this.dataStore[i] = Math.floor(Math.random() * 18 | (this.numElements+1)); 19 | } 20 | } 21 | 22 | function clear() { 23 | for (var i = 0; i < this.dataStore.length; ++i) { 24 | this.dataStore[i] = 0; 25 | } 26 | } 27 | 28 | function insert(element) { 29 | this.dataStore[this.pos++] = element; 30 | } 31 | 32 | function toString() { 33 | var retstr = ""; 34 | for (var i = 0; i < this.dataStore.length; ++i) { 35 | retstr += this.dataStore[i] + " "; 36 | if (i > 0 && i % 10 == 0) { 37 | retstr += "\n"; 38 | } 39 | } 40 | return retstr; 41 | } -------------------------------------------------------------------------------- /Chapter14/Chap14-2.js: -------------------------------------------------------------------------------- 1 | function lcs(word1, word2) { 2 | var max = 0; 3 | var index = 0; 4 | var lcsarr = new Array(word1.length+1); 5 | for (var i = 0; i <= word1.length+1; ++i) { 6 | lcsarr[i] = new Array(word2.length+1); 7 | for (var j = 0; j <= word2.length+1; ++j) { 8 | lcsarr[i][j] = 0; 9 | } 10 | } 11 | for (var i = 0; i <= word1.length; ++i) { 12 | for (var j = 0; j <= word2.length; ++j) { 13 | if (i == 0 || j == 0) { 14 | lcsarr[i][j] = 0; 15 | } 16 | else { 17 | if (word1[i-1] == word2[j-1]) { 18 | lcsarr[i][j] = lcsarr[i-1][j-1] + 1; 19 | } 20 | else { 21 | lcsarr[i][j] = 0; 22 | } 23 | } 24 | if (max < lcsarr[i][j]) { 25 | max = lcsarr[i][j]; 26 | index = i; 27 | } 28 | } 29 | } 30 | var str = ""; 31 | if (max == 0) { 32 | return ""; 33 | } 34 | else { 35 | for (var i = index-max; i <= max; ++i) { 36 | str += word2[i]; 37 | } 38 | return str; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Chapter8/Chap8-2.js: -------------------------------------------------------------------------------- 1 | function HashTable() { 2 | this.table = new Array(137); 3 | this.simpleHash = simpleHash; 4 | this.betterHash = betterHash; 5 | this.showDistro = showDistro; 6 | this.put = put; 7 | //this.get = get; 8 | } 9 | 10 | function put(data) { 11 | var pos = this.betterHash(data); 12 | this.table[pos] = data; 13 | } 14 | 15 | function simpleHash(data) { 16 | var total = 0; 17 | for (var i = 0; i < data.length; ++i) { 18 | total += data.charCodeAt(i); 19 | } 20 | print("Hash value: " + data + " -> " + total); 21 | return total % this.table.length; 22 | } 23 | 24 | function showDistro() { 25 | var n = 0; 26 | for (var i = 0; i < this.table.length; ++i) { 27 | if (this.table[i] != undefined) { 28 | print(i + ": " + this.table[i]); 29 | } 30 | } 31 | } 32 | 33 | function betterHash(string) { 34 | const H = 37; 35 | var total = 0; 36 | for (var i = 0; i < string.length; ++i) { 37 | total += H * total + string.charCodeAt(i); 38 | } 39 | total = total % this.table.length; 40 | if (total < 0) { 41 | total += this.table.length-1; 42 | } 43 | return parseInt(total); 44 | } -------------------------------------------------------------------------------- /Chapter14/Chap14-4.js: -------------------------------------------------------------------------------- 1 | function makeChange(origAmt, coins) { 2 | var remainAmt = 0; 3 | if (origAmt % .25 < origAmt) { 4 | coins[3] = parseInt(origAmt / .25); 5 | remainAmt = origAmt % .25; 6 | origAmt = remainAmt; 7 | } 8 | if (origAmt % .1 < origAmt) { 9 | coins[2] = parseInt(origAmt / .1); 10 | remainAmt = origAmt % .1; 11 | origAmt = remainAmt; 12 | } 13 | if (origAmt % .05 < origAmt) { 14 | coins[1] = parseInt(origAmt / .05); 15 | remainAmt = origAmt % .05; 16 | origAmt = remainAmt; 17 | } 18 | coins[0] = parseInt(origAmt / .01); 19 | } 20 | 21 | function showChange(coins) { 22 | if (coins[3] > 0) { 23 | print("Number of quarters - " + coins[3] + " - " + coins[3] * .25); 24 | } 25 | if (coins[2] > 0) { 26 | print("Number of dimes - " + coins[2] + " - " + coins[2] * .10); 27 | } 28 | if (coins[1] > 0) { 29 | print("Number of nickels - " + coins[1] + " - " + coins[1] * .05); 30 | } 31 | if (coins[0] > 0) { 32 | print("Number of pennies - " + coins[0] + " - " + coins[0] * .01); 33 | } 34 | } 35 | 36 | var origAmt = .63; 37 | var coins = []; 38 | makeChange(origAmt, coins); 39 | showChange(coins); 40 | -------------------------------------------------------------------------------- /Chapter12/Chap12-1.js: -------------------------------------------------------------------------------- 1 | function CArray(numElements) { 2 | this.dataStore = []; 3 | this.pos = 0; 4 | this.numElements = numElements; 5 | this.insert = insert; 6 | this.toString = toString; 7 | this.clear = clear; 8 | this.setData = setData; 9 | this.swap = swap; 10 | 11 | for (var i = 0; i < numElements; ++i) { 12 | this.dataStore[i] = i; 13 | } 14 | } 15 | 16 | function setData() { 17 | for (var i = 0; i < this.numElements; ++i) { 18 | this.dataStore[i] = Math.floor(Math.random() * 19 | (this.numElements+1)); 20 | } 21 | } 22 | 23 | function clear() { 24 | for (var i = 0; i < this.dataStore.length; ++i) { 25 | this.dataStore[i] = 0; 26 | } 27 | } 28 | 29 | function insert(element) { 30 | this.dataStore[this.pos++] = element; 31 | } 32 | 33 | function toString() { 34 | var retstr = ""; 35 | for (var i = 0; i < this.dataStore.length; ++i) { 36 | retstr += this.dataStore[i] + " "; 37 | if (i > 0 && i % 10 == 0) { 38 | retstr += "\n"; 39 | } 40 | } 41 | return retstr; 42 | } 43 | 44 | function swap(arr, index1, index2) { 45 | var temp = arr[index1]; 46 | arr[index1] = arr[index2]; 47 | arr[index2] = temp; 48 | } 49 | -------------------------------------------------------------------------------- /Chapter13/Chap13-17.js: -------------------------------------------------------------------------------- 1 | function binSearch(arr, data) { 2 | var upperBound = arr.length-1; 3 | var lowerBound = 0; 4 | while (lowerBound <= upperBound) { 5 | var mid = Math.floor((upperBound + lowerBound) / 2); 6 | if (arr[mid] < data) { 7 | lowerBound = mid + 1; 8 | } 9 | else if (arr[mid] > data) { 10 | upperBound = mid - 1; 11 | } 12 | else { 13 | return mid; 14 | } 15 | } 16 | return -1; 17 | } 18 | 19 | function insertionsort(arr) { 20 | var temp, inner; 21 | for (var outer = 1; outer <= arr.length-1; ++outer) { 22 | temp = arr[outer]; 23 | inner = outer; 24 | while (inner > 0 && (arr[inner-1] >= temp)) { 25 | arr[inner] = arr[inner-1]; 26 | --inner; 27 | } 28 | arr[inner] = temp; 29 | } 30 | } 31 | 32 | var words = read("words.txt").split(" "); 33 | insertionsort(words); 34 | var word = "rhetoric"; 35 | var start = new Date().getTime(); 36 | var position = binSearch(words, word); 37 | var stop = new Date().getTime(); 38 | var elapsed = stop - start; 39 | if (position >= 0) { 40 | print("Found " + word + " at position " + position + "."); 41 | print("Binary search took " + elapsed + " milliseconds."); 42 | } 43 | else { 44 | print(word + " is not in the file."); 45 | } 46 | -------------------------------------------------------------------------------- /Chapter5/chap5-17.js: -------------------------------------------------------------------------------- 1 | function binSearch(arr, data) { 2 | var upperBound = arr.length-1; 3 | var lowerBound = 0; 4 | while (lowerBound <= upperBound) { 5 | var mid = Math.floor((upperBound + lowerBound) / 2); 6 | if (arr[mid] < data) { 7 | lowerBound = mid + 1; 8 | } 9 | else if (arr[mid] > data) { 10 | upperBound = mid - 1; 11 | } 12 | else { 13 | return mid; 14 | } 15 | } 16 | return -1; 17 | } 18 | 19 | function insertionsort(arr) { 20 | var temp, inner; 21 | for (var outer = 1; outer <= arr.length-1; ++outer) { 22 | temp = arr[outer]; 23 | inner = outer; 24 | while (inner > 0 && (arr[inner-1] >= temp)) { 25 | arr[inner] = arr[inner-1]; 26 | --inner; 27 | } 28 | arr[inner] = temp; 29 | } 30 | } 31 | 32 | var words = read("words.txt").split(" "); 33 | insertionsort(words); 34 | var word = "rhetoric"; 35 | var start = new Date().getTime(); 36 | var position = binSearch(words, word); 37 | var stop = new Date().getTime(); 38 | var elapsed = stop - start; 39 | if (position >= 0) { 40 | print("Found " + word + " at position " + position + "."); 41 | print("Binary search took " + elapsed + " milliseconds."); 42 | } 43 | else { 44 | print(word + " is not in the file."); 45 | } 46 | -------------------------------------------------------------------------------- /Chapter3/chap3_1.js: -------------------------------------------------------------------------------- 1 | function List() { 2 | this.listSize = 0; 3 | this.pos = 0; 4 | this.dataStore = []; 5 | this.clear = clear; 6 | this.find = find; 7 | this.toString = toString; 8 | this.insert = insert; 9 | this.append = append; 10 | this.remove = remove; 11 | this.front = front; 12 | this.end = end; 13 | this.prev = prev; 14 | this.next = next; 15 | this.length = length; 16 | this.currPos = currPos; 17 | this.moveTo = moveTo; 18 | this.getElement = getElement; 19 | this.length = length; 20 | } 21 | 22 | function append(element) { 23 | this.dataStore[this.listSize++] = element; 24 | } 25 | 26 | function find(element) { 27 | for (var i = 0; i < this.dataStore.length; ++i) { 28 | if (this.dataStore[i] == element) { 29 | return i; 30 | } 31 | } 32 | return -1; 33 | } 34 | 35 | function remove(element) { 36 | var foundAt = this.find(element); 37 | if (foundAt > -1) { 38 | this.dataStore.splice(foundAt,1); 39 | --this.listSize; 40 | return true; 41 | } 42 | return false; 43 | } 44 | 45 | function toString() { 46 | return this.dataStore; 47 | } 48 | 49 | var names = new List(); 50 | names.append("Cynthia"); 51 | names.append("Raymond"); 52 | names.append("Barbara"); 53 | print(names.toString()); 54 | names.remove("Raymond"); 55 | print(names.toString()); 56 | -------------------------------------------------------------------------------- /Chapter11/Chap11-2.js: -------------------------------------------------------------------------------- 1 | function Graph(v) { 2 | this.vertices = v; 3 | this.edges = 0; 4 | this.adj = []; 5 | for (var i = 0; i < this.vertices; ++i) { 6 | this.adj[i] = []; 7 | this.adj[i].push(""); 8 | } 9 | this.addEdge = addEdge; 10 | this.showGraph = showGraph; 11 | this.dfs = dfs; 12 | this.marked = []; 13 | for (var i = 0; i < this.vertices; ++i) { 14 | this.marked[i] = false; 15 | } 16 | } 17 | 18 | 19 | function addEdge(v,w) { 20 | this.adj[v].push(w); 21 | this.adj[w].push(v); 22 | this.edges++; 23 | } 24 | 25 | function showGraph() { 26 | for (var i = 0; i < this.vertices; ++i) { 27 | putstr(i + " -> "); 28 | for (var j = 0; j 29 | 30 | < this.vertices; ++j) { 31 | if (this.adj[i][j] != undefined) 32 | putstr(this.adj[i][j] + ' '); 33 | } 34 | print(); 35 | } 36 | } 37 | 38 | function dfs(v) { 39 | this.marked[v] = true; 40 | if (this.adj[v] != undefined) { 41 | print("Visited vertex: " + v); 42 | } 43 | for each (var w in this.adj[v]) { 44 | if (!this.marked[w]) { 45 | this.dfs(w); 46 | } 47 | } 48 | } 49 | 50 | // program to test dfs() function 51 | 52 | load("Graph.js"); 53 | g = new Graph(5); 54 | g.addEdge(0,1); 55 | g.addEdge(0,2); 56 | g.addEdge(1,3); 57 | g.addEdge(2,4); 58 | g.showGraph(); 59 | g.dfs(0); 60 | -------------------------------------------------------------------------------- /Chapter6/chap6-3.txt: -------------------------------------------------------------------------------- 1 | function Node(element) { 2 | this.element = element; 3 | this.next = null; 4 | } 5 | 6 | function LList() { 7 | this.head = new Node("head"); 8 | this.find = find; 9 | this.insert = insert; 10 | this.display = display; 11 | this.findPrevious = findPrevious; 12 | this.remove = remove; 13 | } 14 | 15 | function remove(item) { 16 | var prevNode = this.findPrevious(item); 17 | if (!(prevNode.next == null)) { 18 | prevNode.next = prevNode.next.next; 19 | } 20 | } 21 | 22 | function findPrevious(item) { 23 | var currNode = this.head; 24 | while (!(currNode.next == null) && 25 | (currNode.next.element != item)) { 26 | currNode = currNode.next; 27 | } 28 | return currNode; 29 | } 30 | 31 | function display() { 32 | var currNode = this.head; 33 | while (!(currNode.next == null)) { 34 | console.log(currNode.next.element); 35 | currNode = currNode.next; 36 | } 37 | } 38 | 39 | function find(item) { 40 | var currNode = this.head; 41 | while (currNode.element != item) { 42 | currNode = currNode.next; 43 | } 44 | return currNode; 45 | } 46 | 47 | function insert(newElement, item) { 48 | var newNode = new Node(newElement); 49 | var current = this.find(item); 50 | newNode.next = current.next; 51 | current.next = newNode; 52 | } 53 | 54 | 55 | var cities = new LList(); 56 | cities.insert("Conway", "head"); 57 | cities.insert("Russellville", "Conway"); 58 | cities.insert("Carlisle", "Russellville"); 59 | cities.insert("Alma", "Carlisle"); 60 | cities.display(); 61 | console.log(); 62 | cities.remove("Carlisle"); 63 | cities.display(); 64 | -------------------------------------------------------------------------------- /Chapter6/chap6-1.txt: -------------------------------------------------------------------------------- 1 | function Node(element) { 2 | this.element = element; 3 | this.next = null; 4 | } 5 | 6 | function LList() { 7 | this.head = new Node("head"); 8 | this.find = find; 9 | this.insert = insert; 10 | this.display = display; 11 | this.findPrevious = findPrevious; 12 | this.remove = remove; 13 | } 14 | 15 | function remove(item) { 16 | var prevNode = this.findPrevious(item); 17 | if (!(prevNode.next == null)) { 18 | prevNode.next = prevNode.next.next; 19 | } 20 | } 21 | 22 | function findPrevious(item) { 23 | var currNode = this.head; 24 | while (!(currNode.next == null) && 25 | (currNode.next.element != item)) { 26 | currNode = currNode.next; 27 | } 28 | return currNode; 29 | } 30 | 31 | function display() { 32 | var currNode = this.head; 33 | while (!(currNode.next == null)) { 34 | console.log(currNode.next.element); 35 | currNode = currNode.next; 36 | } 37 | } 38 | 39 | function find(item) { 40 | var currNode = this.head; 41 | while (currNode.element != item) { 42 | currNode = currNode.next; 43 | } 44 | return currNode; 45 | } 46 | 47 | function insert(newElement, item) { 48 | var newNode = new Node(newElement); 49 | var current = this.find(item); 50 | newNode.next = current.next; 51 | current.next = newNode; 52 | } 53 | 54 | 55 | var cities = new LList(); 56 | cities.insert("Conway", "head"); 57 | cities.insert("Russellville", "Conway"); 58 | cities.insert("Carlisle", "Russellville"); 59 | cities.insert("Alma", "Carlisle"); 60 | cities.display(); 61 | console.log(); 62 | cities.remove("Carlisle"); 63 | cities.display(); 64 | 65 | -------------------------------------------------------------------------------- /Chapter12/Chap12-13.js: -------------------------------------------------------------------------------- 1 | function mergeSort(arr) { 2 | if (arr.length < 2) { 3 | return; 4 | } 5 | var step = 1; 6 | var left, right; 7 | while (step < arr.length) { 8 | left = 0; 9 | right = step; 10 | while (right + step <= arr.length) { 11 | mergeArrays(arr, left, left+step, right, right+step); 12 | left = right + step; 13 | right = left + step; 14 | } 15 | if (right < arr.length) { 16 | mergeArrays(arr, left, left+step, right, arr.length); 17 | } 18 | step *= 2; 19 | } 20 | } 21 | 22 | function mergeArrays(arr, startLeft, stopLeft, startRight, stopRight) { 23 | var rightArr = new Array(stopRight - startRight + 1); 24 | var leftArr = new Array(stopLeft - startLeft + 1); 25 | k = startRight; 26 | for (var i = 0; i < (rightArr.length-1); ++i) { 27 | rightArr[i] = arr[k]; 28 | ++k; 29 | } 30 | 31 | k = startLeft; 32 | for (var i = 0; i < (leftArr.length-1); ++i) { 33 | leftArr[i] = arr[k]; 34 | ++k; 35 | } 36 | 37 | rightArr[rightArr.length-1] = Infinity; // a sentinel value 38 | leftArr[leftArr.length-1] = Infinity; // a sentinel value 39 | var m = 0; 40 | var n = 0; 41 | for (var k = startLeft; k < stopRight; ++k) { 42 | if (leftArr[m] <= rightArr[n]) { 43 | arr[k] = leftArr[m]; 44 | m++; 45 | } 46 | else { 47 | arr[k] = rightArr[n]; 48 | n++; 49 | } 50 | } 51 | print("left array - ", leftArr); 52 | print("right array - ", rightArr); 53 | } 54 | 55 | var nums = [6,10,1,9,4,8,2,7,3,5]; 56 | print(nums); 57 | print(); 58 | mergeSort(nums); 59 | print(); 60 | print(nums); 61 | -------------------------------------------------------------------------------- /Chapter12/Chap12-14.js: -------------------------------------------------------------------------------- 1 | function CArray(numElements) { 2 | this.dataStore = []; 3 | this.pos = 0; 4 | this.gaps = [5,3,1]; 5 | this.numElements = numElements; 6 | this.insert = insert; 7 | this.toString = toString; 8 | this.clear = clear; 9 | this.setData = setData; 10 | this.setGaps = setGaps; 11 | this.shellsort = shellsort; 12 | this.mergeSort = mergeSort; 13 | this.mergeArrays = mergeArrays; 14 | for (var i = 0; i < numElements; ++i) { 15 | this.dataStore[i] = 0; 16 | } 17 | 18 | } 19 | 20 | // other function definitions go here 21 | 22 | function mergeArrays(arr,startLeft, stopLeft, startRight, stopRight) { 23 | var rightArr = new Array(stopRight - startRight + 1); 24 | var leftArr = new Array(stopLeft - startLeft + 1); 25 | k = startRight; 26 | for (var i = 0; i < (rightArr.length-1); ++i) { 27 | rightArr[i] = arr[k]; 28 | ++k; 29 | } 30 | 31 | k = startLeft; 32 | for (var i = 0; i < (leftArr.length-1); ++i) { 33 | leftArr[i] = arr[k]; 34 | ++k; 35 | } 36 | 37 | rightArr[rightArr.length-1] = Infinity; // a sentinel value 38 | leftArr[leftArr.length-1] = Infinity; // a sentinel value 39 | var m = 0; 40 | var n = 0; 41 | for (var k = startLeft; k < stopRight; ++k) { 42 | if (leftArr[m] <= rightArr[n]) { 43 | arr[k] = leftArr[m]; 44 | m++; 45 | } 46 | else { 47 | arr[k] = rightArr[n]; 48 | n++; 49 | } 50 | } 51 | print("left array - ", leftArr); 52 | print("right array - ", rightArr); 53 | } 54 | 55 | function mergeSort() { 56 | if (this.dataStore.length < 2) { 57 | return; 58 | } 59 | var step = 1; 60 | var left, right; 61 | while (step < this.dataStore.length) { 62 | left = 0; 63 | right = step; 64 | while (right + step <= this.dataStore.length) { 65 | mergeArrays(this.dataStore, left, left+step, right, right+step); 66 | left = right + step; 67 | right = left + step; 68 | } 69 | if (right < this.dataStore.length) { 70 | mergeArrays(this.dataStore, left, left+step, right, this.dataStore.length); 71 | } 72 | step *= 2; 73 | } 74 | } 75 | 76 | var nums = new CArray(10); 77 | nums.setData(); 78 | print(nums.toString()); 79 | nums.mergeSort(); 80 | print(nums.toString()); 81 | -------------------------------------------------------------------------------- /Chapter6/chap6-4.txt: -------------------------------------------------------------------------------- 1 | function Node(element) { 2 | this.element = element; 3 | this.next = null; 4 | this.previous = null; 5 | } 6 | 7 | function LList() { 8 | this.head = new Node("head"); 9 | this.find = find; 10 | this.insert = insert; 11 | this.display = display; 12 | this.remove = remove; 13 | this.findLast = findLast; 14 | this.dispReverse = dispReverse; 15 | } 16 | 17 | function dispReverse() { 18 | var currNode = this.head; 19 | currNode = this.findLast(); 20 | while (!(currNode.previous == null)) { 21 | console.log(currNode.element); 22 | currNode = currNode.previous; 23 | } 24 | } 25 | 26 | function findLast() { 27 | var currNode = this.head; 28 | while (!(currNode.next == null)) { 29 | currNode = currNode.next; 30 | } 31 | return currNode; 32 | } 33 | 34 | function remove(item) { 35 | var currNode = this.find(item); 36 | if (!(currNode.next == null)) { 37 | currNode.previous.next = currNode.next; 38 | currNode.next.previous = currNode.previous; 39 | currNode.next = null; 40 | currNode.previous = null; 41 | } 42 | } 43 | 44 | // findPrevious is no longer needed 45 | /*function findPrevious(item) { 46 | var currNode = this.head; 47 | while (!(currNode.next == null) && 48 | (currNode.next.element != item)) { 49 | currNode = currNode.next; 50 | } 51 | return currNode; 52 | }*/ 53 | 54 | function display() { 55 | var currNode = this.head; 56 | while (!(currNode.next == null)) { 57 | console.log(currNode.next.element); 58 | currNode = currNode.next; 59 | } 60 | } 61 | 62 | function find(item) { 63 | var currNode = this.head; 64 | while (currNode.element != item) { 65 | currNode = currNode.next; 66 | } 67 | return currNode; 68 | } 69 | 70 | function insert(newElement, item) { 71 | var newNode = new Node(newElement); 72 | var current = this.find(item); 73 | newNode.next = current.next; 74 | newNode.previous = current; 75 | current.next = newNode; 76 | } 77 | 78 | 79 | var cities = new LList(); 80 | cities.insert("Conway", "head"); 81 | cities.insert("Russellville", "Conway"); 82 | cities.insert("Carlisle", "Russellville"); 83 | cities.insert("Alma", "Carlisle"); 84 | cities.display(); 85 | console.log(); 86 | cities.remove("Carlisle"); 87 | cities.display(); 88 | console.log(); 89 | cities.dispReverse(); 90 | -------------------------------------------------------------------------------- /Chapter9/Set.js: -------------------------------------------------------------------------------- 1 | function Set() { 2 | this.dataStore = []; 3 | this.add = add; 4 | this.remove = remove; 5 | this.size = size; 6 | this.union = union; 7 | this.contains = contains; 8 | this.intersect = intersect; 9 | this.subset = subset; 10 | this.difference = difference; 11 | this.show = show; 12 | } 13 | 14 | function add(data) { 15 | if (this.dataStore.indexOf(data) < 0) { 16 | this.dataStore.push(data); 17 | return true; 18 | } 19 | else { 20 | return false; 21 | } 22 | } 23 | 24 | function remove(data) { 25 | var pos = this.dataStore.indexOf(data); 26 | if (pos > -1) { 27 | this.dataStore.splice(pos,1); 28 | return true; 29 | } 30 | else { 31 | return false; 32 | } 33 | } 34 | 35 | function size() { 36 | return this.dataStore.length; 37 | } 38 | 39 | function show() { 40 | return "[" + this.dataStore + "]"; 41 | } 42 | 43 | function contains(data) { 44 | if (this.dataStore.indexOf(data) > -1) { 45 | return true; 46 | } 47 | else { 48 | return false; 49 | } 50 | } 51 | 52 | function union(set) { 53 | var tempSet = new Set(); 54 | for (var i = 0; i < this.dataStore.length; ++i) { 55 | tempSet.add(this.dataStore[i]); 56 | } 57 | for (var i = 0; i < set.dataStore.length; ++i) { 58 | if (!tempSet.contains(set.dataStore[i])) { 59 | tempSet.dataStore.push(set.dataStore[i]); 60 | } 61 | } 62 | return tempSet; 63 | } 64 | 65 | function intersect(set) { 66 | var tempSet = new Set(); 67 | for (var i = 0; i < this.dataStore.length; ++i) { 68 | if (set.contains(this.dataStore[i])) { 69 | tempSet.add(this.dataStore[i]); 70 | } 71 | } 72 | return tempSet; 73 | } 74 | 75 | function subset(set) { 76 | if (this.size() > set.size()) { 77 | return false; 78 | } 79 | else { 80 | for each (var member in this.dataStore) { 81 | if (!set.contains(member)) { 82 | return false; 83 | } 84 | } 85 | } 86 | return true; 87 | } 88 | 89 | function difference(set) { 90 | var tempSet = new Set(); 91 | for (var i = 0; i < this.dataStore.length; ++i) { 92 | if (!set.contains(this.dataStore[i])) { 93 | tempSet.add(this.dataStore[i]); 94 | } 95 | } 96 | return tempSet; 97 | } 98 | 99 | 100 | // main program 101 | 102 | var cis = new Set(); 103 | var it = new Set(); 104 | cis.add("Clayton"); 105 | cis.add("Jennifer"); 106 | cis.add("Danny"); 107 | it.add("Bryan"); 108 | it.add("Clayton"); 109 | it.add("Jennifer"); 110 | var diff = new Set(); 111 | diff = cis.difference(it); 112 | print(cis.show() + " difference " + it.show() + " -> " + diff.show()); -------------------------------------------------------------------------------- /Chapter11/Chap11-6.js: -------------------------------------------------------------------------------- 1 | function Graph(v) { 2 | this.vertices = v; 3 | this.vertexList = []; 4 | this.edges = 0; 5 | this.adj = []; 6 | for (var i = 0; i < this.vertices; ++i) { 7 | this.adj[i] = []; 8 | this.adj[i].push(""); 9 | } 10 | this.addEdge = addEdge; 11 | this.showGraph = showGraph; 12 | this.dfs = dfs; 13 | this.marked = []; 14 | for (var i = 0; i < this.vertices; ++i) { 15 | this.marked[i] = false; 16 | } 17 | this.bfs = bfs; 18 | this.edgeTo = []; 19 | this.hasPathTo = hasPathTo; 20 | this.pathTo = pathTo; 21 | this.topSortHelper = topSortHelper; 22 | this.topSort = topSort; 23 | 24 | } 25 | 26 | function topSort() { 27 | var stack = []; 28 | var visited = []; 29 | for (var i = 0; i < this.vertices; i++) { 30 | visited[i] = false; 31 | } 32 | for (var i = 0; i < this.vertices; i++) { 33 | if (visited[i] == false) { 34 | this.topSortHelper(i, visited, stack); 35 | } 36 | } 37 | for (var i = 0; i < stack.length; i++) { 38 | if (stack[i] != undefined && stack[i] != false) { 39 | print(this.vertexList[stack[i]]); 40 | } 41 | } 42 | } 43 | 44 | function topSortHelper(v, visited, stack) { 45 | visited[v] = true; 46 | for each (var w in this.adj[v]) { 47 | if (!visited[w]) { 48 | this.topSortHelper(visited[w], visited, stack); 49 | } 50 | } 51 | stack.push(v); 52 | } 53 | 54 | function addEdge(v,w) { 55 | this.adj[v].push(w); 56 | this.adj[w].push(v); 57 | this.edges++; 58 | } 59 | 60 | /*function showGraph() { 61 | for (var i = 0; i < this.vertices; ++i) { 62 | putstr(i + " -> "); 63 | for (var j = 0; j < this.vertices; ++j) { 64 | if (this.adj[i][j] != undefined) 65 | putstr(this.adj[i][j] + ' '); 66 | } 67 | print(); 68 | } 69 | }*/ 70 | 71 | // a new function to display symbolic names instead of numbers 72 | function showGraph() { 73 | var visited = []; 74 | for (var i = 0; i < this.vertices; ++i) { 75 | putstr(this.vertexList[i] + " -> "); 76 | visited.push(this.vertexList[i]); 77 | for (var j = 0; j < this.vertices; ++j) { 78 | if (this.adj[i][j] != undefined) { 79 | if (visited.indexOf(this.vertexList[j]) < 0) { 80 | putstr(this.vertexList[j] + ' '); 81 | } 82 | } 83 | } 84 | print(); 85 | visited.pop(); 86 | } 87 | } 88 | 89 | function dfs(v) { 90 | this.marked[v] = true; 91 | if (this.adj[v] != undefined) { 92 | print("Visited vertex: " + v); 93 | } 94 | for each (var w in this.adj[v]) { 95 | if (!this.marked[w]) { 96 | this.dfs(w); 97 | } 98 | } 99 | } 100 | 101 | function bfs(s) { 102 | var queue = []; 103 | this.marked[s] = true; 104 | queue.unshift(s); 105 | while (queue.length > 0) { 106 | var v = queue.shift(); 107 | if (typeof(v) != "string") { 108 | print("Visited vertex: " + v); 109 | } 110 | for each (var w in this.adj[v]) { 111 | if (!this.marked[w]) { 112 | this.edgeTo[w] = v; 113 | this.marked[w] = true; 114 | queue.unshift(w); 115 | } 116 | } 117 | } 118 | } 119 | 120 | function hasPathTo(v) { 121 | return this.marked[v]; 122 | } 123 | 124 | function pathTo(v) { 125 | var source = 0; 126 | if (!this.hasPathTo(v)) { 127 | return undefined; 128 | } 129 | var path = []; 130 | for (var i = v; i != source; i = this.edgeTo[i]) { 131 | path.push(i); 132 | } 133 | path.push(s); 134 | return path; 135 | } 136 | -------------------------------------------------------------------------------- /Chapter8/hashing.js: -------------------------------------------------------------------------------- 1 | function HashTable() { 2 | this.table = new Array(137); 3 | this.simpleHash = simpleHash; 4 | this.betterHash = betterHash; 5 | this.showDistro = showDistro; 6 | this.put = put; 7 | this.get = get; 8 | } 9 | 10 | /*function put(data) { 11 | var pos = this.simpleHash(data); 12 | this.table[pos] = data; 13 | }*/ 14 | 15 | 16 | // put for linear probing 17 | function put(key, data) { 18 | var pos = this.betterHash(key); 19 | if (this.table[pos] == undefined) { 20 | this.table[pos] = key; 21 | this.values[pos] = data; 22 | } 23 | else { 24 | while (this.table[pos] != undefined) { 25 | pos++; 26 | } 27 | this.table[pos] = key; 28 | this.values[pos] = data; 29 | } 30 | } 31 | 32 | // put for separate chaining 33 | function put(key, data) { 34 | var pos = this.betterHash(key); 35 | var index = 0; 36 | if (this.table[pos][index] == undefined) { 37 | this.table[pos][index] = data; 38 | } 39 | ++index; 40 | else { 41 | while (this.table[pos][index] != undefined) { 42 | ++index; 43 | } 44 | this.table[pos][index] = data; 45 | } 46 | } 47 | 48 | function simpleHash(data) { 49 | var total = 0; 50 | for (var i = 0; i < data.length; ++i) { 51 | total += data.charCodeAt(i); 52 | } 53 | print("Hash value: " + data + " -> " + total); 54 | return total % this.table.length; 55 | } 56 | 57 | function betterHash(string) { 58 | const H = 37; 59 | var total = 0; 60 | for (var i = 0; i < string.length; ++i) { 61 | total += H * total + string.charCodeAt(i); 62 | } 63 | total = total % this.table.length; 64 | if (total < 0) { 65 | total += this.table.length-1; 66 | } 67 | return parseInt(total); 68 | } 69 | 70 | function showDistro() { 71 | var n = 0; 72 | for (var i = 0; i < this.table.length; ++i) { 73 | if (this.table[i] != undefined) { 74 | print(i + ": " + this.table[i]); 75 | } 76 | } 77 | } 78 | 79 | function getRandomInt (min, max) { 80 | return Math.floor(Math.random() * (max - min + 1)) + min; 81 | } 82 | 83 | function genStuData(arr) { 84 | for (var i = 0; i < arr.length; ++i) { 85 | var num = ""; 86 | for (var j = 1; j <= 9; ++j) { 87 | num += Math.floor(Math.random() * 10); 88 | } 89 | num += getRandomInt(50,100); 90 | arr[i] = num; 91 | } 92 | } 93 | 94 | function buildChains(arr) { 95 | for (var i = 0; i < arr.length; ++i) { 96 | arr[i] = new Array(); 97 | } 98 | } 99 | 100 | function inHash(key, arr) { 101 | var hash = simpleHash(key, arr); 102 | var n = 0; 103 | if (key == arr[hash][n]) { 104 | return true; 105 | } 106 | else { 107 | while (arr[hash][n] != undefined) { 108 | if (arr[hash][n] == key) { 109 | return true; 110 | } 111 | ++n; 112 | } 113 | } 114 | return false; 115 | } 116 | 117 | // get for separate chaining 118 | function get(key) { 119 | var index = 0; 120 | var hash = this.betterHash(key); 121 | if (this.table[pos][index] = key) { 122 | return this.table[pos][index+1]; 123 | } 124 | index += 2; 125 | else { 126 | while (this.table[pos][index] != key) { 127 | index += 2; 128 | } 129 | return this.table[pos][index+1]; 130 | } 131 | return undefined; 132 | } 133 | 134 | // get for linear probing 135 | function get(key) { 136 | var hash = -1; 137 | hash = this.betterHash(key); 138 | if (hash > -1) { 139 | for (var i = hash; this.table[hash] != undefined; i++) { 140 | if (this.table[hash] == key) { 141 | return this.values[hash]; 142 | } 143 | } 144 | } 145 | return undefined; 146 | } 147 | 148 | function get(key) { 149 | return this.table[this.betterHash(key)]; 150 | } 151 | 152 | -------------------------------------------------------------------------------- /Chapter10/Chap10-1.js: -------------------------------------------------------------------------------- 1 | function Node(data, left, right) { 2 | this.data = data; 3 | this.left = left; 4 | this.right = right; 5 | this.show = show; 6 | } 7 | 8 | function show() { 9 | return this.data; 10 | } 11 | 12 | function BST() { 13 | this.root = null; 14 | this.insert = insert; 15 | this.inOrder = inOrder; 16 | this.preOrder = preOrder; 17 | this.postOrder = postOrder; 18 | this.getmin = getmin; 19 | this.getmax = getmax; 20 | this.find = find; 21 | this.remove = remove; 22 | this.removeNode = removeNode; 23 | this.getSmallest = getSmallest; 24 | } 25 | 26 | function insert(data) { 27 | var n = new Node(data, null, null); 28 | if (this.root == null) { 29 | this.root = n; 30 | } 31 | else { 32 | var current = this.root; 33 | var parent; 34 | while (true) { 35 | parent = current; 36 | if (data < current.data) { 37 | current = current.left; 38 | if (current == null) { 39 | parent.left = n; 40 | break; 41 | } 42 | } 43 | else { 44 | current = current.right; 45 | if (current == null) { 46 | parent.right = n; 47 | break; 48 | } 49 | } 50 | } 51 | } 52 | } 53 | 54 | function inOrder(node) { 55 | if (!(node == null)) { 56 | inOrder(node.left); 57 | putstr(node.show() + " "); 58 | inOrder(node.right); 59 | } 60 | } 61 | 62 | function preOrder(node) { 63 | if (!(node == null)) { 64 | putstr(node.show() + " "); 65 | preOrder(node.left); 66 | preOrder(node.right); 67 | } 68 | } 69 | 70 | function postOrder(node) { 71 | if (!(node == null)) { 72 | postOrder(node.left); 73 | postOrder(node.right); 74 | putstr(node.show() + " "); 75 | } 76 | } 77 | 78 | function getmin() { 79 | var current = this.root; 80 | print("debug: " + current.data); 81 | while (!(current.left == null)) { 82 | current = current.left; 83 | } 84 | return current.data; 85 | } 86 | 87 | function getmax() { 88 | var current = this.root; 89 | while (!(current.right == null)) { 90 | current = current.right; 91 | } 92 | return current.data; 93 | } 94 | 95 | function find(data) { 96 | var current = this.root; 97 | while (current.data != data) { 98 | if (data < current.data) { 99 | current = current.left; 100 | } 101 | else { 102 | current = current.right; 103 | } 104 | if (current == null) { 105 | return null; 106 | } 107 | } 108 | return current; 109 | } 110 | 111 | function getSmallest(node) { 112 | if (node.left == null) { 113 | return node; 114 | } 115 | else { 116 | return getSmallest(node.left); 117 | } 118 | } 119 | 120 | function remove(data) { 121 | root = removeNode(this.root, data); 122 | } 123 | 124 | function removeNode(node, data) { 125 | if (node == null) { 126 | return null; 127 | } 128 | if (data == node.data) { 129 | // node has no children 130 | if (node.left == null && node.right == null) { 131 | return null; 132 | } 133 | // node has no left child 134 | if (node.left == null) { 135 | return node.right; 136 | } 137 | // node has no right child 138 | if (node.right == null) { 139 | return node.left; 140 | } 141 | // node has two children 142 | var tempNode = getSmallest(node.right); 143 | node.data = tempNode.data; 144 | node.right = removeNode(node.right, tempNode.data); 145 | return node; 146 | } 147 | else if (data < node.data) { 148 | node.left = removeNode(node.left, data); 149 | return node; 150 | } 151 | else { 152 | node.right = removeNode(node.right, data); 153 | return node; 154 | } 155 | } -------------------------------------------------------------------------------- /Chapter10/bst.js: -------------------------------------------------------------------------------- 1 | function Node(data, left, right) { 2 | this.data = data; 3 | this.left = left; 4 | this.right = right; 5 | this.show = show; 6 | } 7 | 8 | function show() { 9 | return this.data; 10 | } 11 | 12 | function BST() { 13 | this.root = null; 14 | this.insert = insert; 15 | this.inOrder = inOrder; 16 | this.preOrder = preOrder; 17 | this.postOrder = postOrder; 18 | this.getmin = getmin; 19 | this.getmax = getmax; 20 | this.find = find; 21 | this.remove = remove; 22 | this.removeNode = removeNode; 23 | this.getSmallest = getSmallest; 24 | } 25 | 26 | function insert(data) { 27 | var n = new Node(data, null, null); 28 | if (this.root == null) { 29 | this.root = n; 30 | } 31 | else { 32 | var current = this.root; 33 | var parent; 34 | while (true) { 35 | parent = current; 36 | if (data < current.data) { 37 | current = current.left; 38 | if (current == null) { 39 | parent.left = n; 40 | break; 41 | } 42 | } 43 | else { 44 | current = current.right; 45 | if (current == null) { 46 | parent.right = n; 47 | break; 48 | } 49 | } 50 | } 51 | } 52 | } 53 | 54 | function inOrder(node) { 55 | if (!(node == null)) { 56 | inOrder(node.left); 57 | putstr(node.show() + " "); 58 | inOrder(node.right); 59 | } 60 | } 61 | 62 | function preOrder(node) { 63 | if (!(node == null)) { 64 | putstr(node.show() + " "); 65 | preOrder(node.left); 66 | preOrder(node.right); 67 | } 68 | } 69 | 70 | function postOrder(node) { 71 | if (!(node == null)) { 72 | postOrder(node.left); 73 | postOrder(node.right); 74 | putstr(node.show() + " "); 75 | } 76 | } 77 | 78 | function getmin() { 79 | var current = this.root; 80 | print("debug: " + current.data); 81 | while (!(current.left == null)) { 82 | current = current.left; 83 | } 84 | return current.data; 85 | } 86 | 87 | function getmax() { 88 | var current = this.root; 89 | while (!(current.right == null)) { 90 | current = current.right; 91 | } 92 | return current.data; 93 | } 94 | 95 | function find(data) { 96 | var current = this.root; 97 | while (current.data != data) { 98 | if (data < current.data) { 99 | current = current.left; 100 | } 101 | else { 102 | current = current.right; 103 | } 104 | if (current == null) { 105 | return null; 106 | } 107 | } 108 | return current; 109 | } 110 | 111 | function getSmallest(node) { 112 | if (node.left == null) { 113 | return node; 114 | } 115 | else { 116 | return getSmallest(node.left); 117 | } 118 | } 119 | 120 | function remove(data) { 121 | root = removeNode(this.root, data); 122 | } 123 | 124 | function removeNode(node, data) { 125 | if (node == null) { 126 | return null; 127 | } 128 | if (data == node.data) { 129 | // node has no children 130 | if (node.left == null && node.right == null) { 131 | return null; 132 | } 133 | // node has no left child 134 | if (node.left == null) { 135 | return node.right; 136 | } 137 | // node has no right child 138 | if (node.right == null) { 139 | return node.left; 140 | } 141 | // node has two children 142 | var tempNode = getSmallest(node.right); 143 | node.data = tempNode.data; 144 | node.right = removeNode(node.right, tempNode.data); 145 | return node; 146 | } 147 | else if (data < node.data) { 148 | node.left = removeNode(node.left, data); 149 | return node; 150 | } 151 | else { 152 | node.right = removeNode(node.right, data); 153 | return node; 154 | } 155 | } 156 | 157 | 158 | 159 | var nums = new BST(); 160 | nums.insert(23); 161 | nums.insert(45); 162 | nums.insert(16); 163 | nums.insert(37); 164 | nums.insert(3); 165 | nums.insert(99); 166 | nums.insert(22); 167 | /*print("Inorder traversal: "); 168 | inOrder(nums.root); 169 | print("\n"); 170 | print("Preorder traversal: "); 171 | preOrder(nums.root); 172 | print("\n"); 173 | print("Postorder traversal: "); 174 | postOrder(nums.root); 175 | print("\n"); 176 | var min = nums.getmin(); 177 | print("The minimum value of the BST is: " + min); 178 | var max = nums.getmax(); 179 | print("The maximum value of the BST is: " + max); 180 | inOrder(nums.root); 181 | print("\n"); 182 | putstr("Enter a value to search for: "); 183 | var value = parseInt(readline()); 184 | var found = nums.find(value); 185 | if (found != null) { 186 | print("Found " + value + " in the BST."); 187 | } 188 | else { 189 | print(value + " was not found in the BST."); 190 | }*/ 191 | inOrder(nums.root); 192 | print("\n"); 193 | var num = parseInt(readline()); 194 | nums.remove(num); 195 | inOrder(nums.root); 196 | --------------------------------------------------------------------------------