|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>CMPS 260: Module 5 Programming Assignment</title> |
| 6 | +<style>* { font-family: monospace; }</style> |
| 7 | +<script> |
| 8 | + |
| 9 | +// NOTE: You must implement the data structures using the no prototype approach. |
| 10 | +// This is what the book uses, so you can copy it. |
| 11 | +// See also: https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/ |
| 12 | + |
| 13 | +// NOTE: Please review the following links regularly: |
| 14 | +// https://it.pointpark.edu/tutorials/arrays-vs-objects/ |
| 15 | +// https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/ |
| 16 | +// https://it.pointpark.edu/tutorials/implementation-vs-interface/ |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +//------------------------------------// |
| 21 | +// The binary and binary search trees // |
| 22 | +//------------------------------------// |
| 23 | +console.log("The binary and binary search trees"); |
| 24 | + |
| 25 | +function BinarySearchTree() { |
| 26 | + function Node(key) { |
| 27 | + this.key = key; |
| 28 | + this.left = null; |
| 29 | + this.right = null |
| 30 | + } |
| 31 | + |
| 32 | + // the root node |
| 33 | + var root = null; |
| 34 | + |
| 35 | + |
| 36 | + // helper functions |
| 37 | + |
| 38 | + |
| 39 | + function insertNode(node, newNode) { |
| 40 | + if (newNode.key < node.key) { |
| 41 | + // go to left |
| 42 | + if (node.left === null) { |
| 43 | + // no left node yet so assign |
| 44 | + node.left = newNode; |
| 45 | + } |
| 46 | + else { |
| 47 | + // move down tree and repeat |
| 48 | + insertNode(node.left, newNode); |
| 49 | + } |
| 50 | + } |
| 51 | + else { |
| 52 | + // go to right |
| 53 | + if (node.right === null) { |
| 54 | + // no right node yet so assign |
| 55 | + node.right = newNode; |
| 56 | + } |
| 57 | + else { |
| 58 | + // move down tree and repeat |
| 59 | + insertNode(node.right, newNode); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + this.insert = function(key) { |
| 66 | + // insert new key in the tree |
| 67 | + var newNode = new Node(key); |
| 68 | + if (root === null) { |
| 69 | + // no nodes yet |
| 70 | + root = newNode; |
| 71 | + } |
| 72 | + else { |
| 73 | + // find insert location through insertNode |
| 74 | + insertNode(root, newNode); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + this.search = function(key) { |
| 79 | + // search for key and return true if found, false otherwise |
| 80 | + }; |
| 81 | + |
| 82 | + this.inOrderTraverse = function(callback) { |
| 83 | + inOrderTraverseNode(root, callback); |
| 84 | + }; |
| 85 | + |
| 86 | + var inOrderTraverseNode = function (node, callback) { |
| 87 | + if (node !== null) { |
| 88 | + inOrderTraverseNode(node.left, callback); |
| 89 | + callback(node.key); |
| 90 | + inOrderTraverseNode(node.right, callback); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + this.preOrderTraverse = function(callback) { |
| 95 | + preOrderTraverseNode(root, callback); |
| 96 | + }; |
| 97 | + |
| 98 | + var preOrderTraverseNode = function (node, callback) { |
| 99 | + if (node !== null) { |
| 100 | + callback(node.key); |
| 101 | + preOrderTraverseNode(node.left, callback); |
| 102 | + preOrderTraverseNode(node.right, callback); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + this.postOrderTraverse = function(callback) { |
| 107 | + postOrderTraverseNode(root, callback); |
| 108 | + }; |
| 109 | + |
| 110 | + var postOrderTraverseNode = function (node, callback) { |
| 111 | + if (node !== null) { |
| 112 | + postOrderTraverseNode(node.left, callback); |
| 113 | + postOrderTraverseNode(node.right, callback); |
| 114 | + callback(node.key); |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + this.min = function() { |
| 119 | + return minNode(root); |
| 120 | + }; |
| 121 | + |
| 122 | + var minNode = function (node) { |
| 123 | + if (node) { |
| 124 | + while (node && node.left !== null) { |
| 125 | + node = node.left; |
| 126 | + } |
| 127 | + return node.key; |
| 128 | + } |
| 129 | + return null; |
| 130 | + }; |
| 131 | + |
| 132 | + this.max = function() { |
| 133 | + return maxNode(root); |
| 134 | + }; |
| 135 | + |
| 136 | + var maxNode = function(node) { |
| 137 | + if (node) { |
| 138 | + while (node && node.right !==null) { |
| 139 | + node = node.right; |
| 140 | + } |
| 141 | + return node.key; |
| 142 | + } |
| 143 | + return null; |
| 144 | + }; |
| 145 | + |
| 146 | + this.remove = function(key) { |
| 147 | + root = removeNode(root, key); |
| 148 | + }; |
| 149 | + |
| 150 | + var removeNode = function(node, key) { |
| 151 | + if (node === null) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + if (key < node.key) { |
| 155 | + node.left(removeNode(node.left, key)); |
| 156 | + return node; |
| 157 | + |
| 158 | + } else if (key > node.key) { |
| 159 | + node.right = removeNode(node.right, key); |
| 160 | + return node; |
| 161 | + |
| 162 | + } else { |
| 163 | + |
| 164 | + if (node.left === null && node.right === null) { |
| 165 | + node = null; |
| 166 | + return node; |
| 167 | + } |
| 168 | + if (node.left === null) { |
| 169 | + node = node.right; |
| 170 | + return node; |
| 171 | + |
| 172 | + } else if (node.right === null) { |
| 173 | + node = node.left; |
| 174 | + return node; |
| 175 | + }; |
| 176 | + |
| 177 | + var aux = findMinNode(node.right); |
| 178 | + node.key = aux.key; |
| 179 | + node.right = removeNode(node.right, aux.key); |
| 180 | + return node; |
| 181 | + } |
| 182 | + }; |
| 183 | + |
| 184 | + this.print = function() { |
| 185 | + function print(node) { |
| 186 | + // check if not is not null |
| 187 | + if (node !== null) { |
| 188 | + // text for left child |
| 189 | + var leftChild = node.left !== null ? node.left.key : "None"; |
| 190 | + // text for right child |
| 191 | + var rightChild = node.right !== null ? node.right.key : "None"; |
| 192 | + // print output |
| 193 | + console.log(leftChild + " <-- " + node.key + " --> " + rightChild); |
| 194 | + // traverse left |
| 195 | + print(node.left); |
| 196 | + // traverse right |
| 197 | + print(node.right); |
| 198 | + } |
| 199 | + } |
| 200 | + console.log("===tree==="); |
| 201 | + print(root); |
| 202 | + console.log("=========="); |
| 203 | + }; |
| 204 | +} |
| 205 | + |
| 206 | +// 1. Complete the construction of the tree below based on the example from |
| 207 | +// the book. |
| 208 | + |
| 209 | +var tree = new BinarySearchTree(); |
| 210 | +tree.insert(11); |
| 211 | +tree.insert(7); |
| 212 | +tree.insert(15); |
| 213 | +tree.insert(21); |
| 214 | +tree.insert(19); |
| 215 | +tree.insert(3); |
| 216 | +tree.insert(27); |
| 217 | +tree.insert(13); |
| 218 | +// copy rest from book |
| 219 | +tree.print(); |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | +//----------------// |
| 224 | +// Tree traversal // |
| 225 | +//----------------// |
| 226 | +console.log("Tree traversal"); |
| 227 | + |
| 228 | +function printNode(value) { |
| 229 | + console.log(value); |
| 230 | +} |
| 231 | + |
| 232 | +// 1. Implement in-order traversal. Test your implementation with the |
| 233 | +// printNode() function given above. |
| 234 | +// NOTE: in-order means all keys are visited in sorted order. |
| 235 | + |
| 236 | +console.log("In Order Traverse"); |
| 237 | + |
| 238 | +tree.inOrderTraverse(printNode); |
| 239 | + |
| 240 | +// 2. Implement pre-order traversal. Test your implementation with the |
| 241 | +// printNode() function given above. |
| 242 | +// NOTE: pre-order means a node is visited prior to its descendants. |
| 243 | + |
| 244 | +console.log("Pre Order Traverse"); |
| 245 | + |
| 246 | +tree.preOrderTraverse(printNode); |
| 247 | + |
| 248 | +// 3. Implement post-order traversal. Test your implementation with the |
| 249 | +// printNode() function given above. |
| 250 | +// NOTE: pre-order means a node is visited after its descendants. |
| 251 | + |
| 252 | +console.log("Post Order Traverse"); |
| 253 | + |
| 254 | +tree.postOrderTraverse(printNode); |
| 255 | + |
| 256 | + |
| 257 | + |
| 258 | +//--------------------------------// |
| 259 | +// Searching for values in a tree // |
| 260 | +//--------------------------------// |
| 261 | +console.log("Searching for values in a tree"); |
| 262 | + |
| 263 | +// 1. Implement the min method above and show that it works. |
| 264 | +console.log("Min Method"); |
| 265 | + |
| 266 | +console.log("The minimum key is " + tree.min()); |
| 267 | + |
| 268 | +// 2. Implement the max method above and show that it works. |
| 269 | +console.log("Max Method"); |
| 270 | + |
| 271 | +console.log("The maximum key is " + tree.max()); |
| 272 | + |
| 273 | +// 3. Implement the search method above and show that it works. |
| 274 | +console.log("Search Method"); |
| 275 | + |
| 276 | + |
| 277 | + |
| 278 | +if (tree.search(19)) { |
| 279 | + console.log("The key WAS found."); |
| 280 | +} |
| 281 | +else { |
| 282 | + console.log("The key was NOT found."); |
| 283 | +} |
| 284 | + |
| 285 | + |
| 286 | + |
| 287 | + |
| 288 | +//--------------------------// |
| 289 | +// Creating the Graph class // |
| 290 | +//--------------------------// |
| 291 | +console.log("Creating the Graph class"); |
| 292 | + |
| 293 | +// 1. Implement the Graph class. |
| 294 | + |
| 295 | +function Graph() { |
| 296 | + var vertices = []; |
| 297 | + var adjList = new Dictionary(); |
| 298 | +} |
| 299 | + |
| 300 | +this.addVertex = function(v) { |
| 301 | + vertices.push(v); |
| 302 | + adjList.set(v, []); |
| 303 | +}; |
| 304 | + |
| 305 | +this.addEdge = function(v, w) { |
| 306 | + adjList.get(v).push(w); |
| 307 | + adjList.get(w).push(v); |
| 308 | +}; |
| 309 | + |
| 310 | +this.toString = function() { |
| 311 | + var s = ''; |
| 312 | + for (var i=0; i<vertices.length; i++) { |
| 313 | + s += vertices[i] + ' -> '; |
| 314 | + var neighbors = adjList.get(vertices[i]); |
| 315 | + for (var j=0; j<neighbors.length; j++) { |
| 316 | + s += neighbors[j] + ' '; |
| 317 | + } |
| 318 | + s += '\n'; |
| 319 | + } |
| 320 | + return s; |
| 321 | +}; |
| 322 | + |
| 323 | + |
| 324 | + |
| 325 | +// 2. Test the Graph class with the example from the book. |
| 326 | + |
| 327 | +var graph = new Graph(); |
| 328 | + |
| 329 | +var myVertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']; |
| 330 | +for (var i = 0; i < myVertices.length; i++) { |
| 331 | + graph.addVertex(myVertices[i]); |
| 332 | +} |
| 333 | + |
| 334 | +graph.addEdge('A', 'B'); |
| 335 | +graph.addEdge('A', 'C'); |
| 336 | +graph.addEdge('A', 'D'); |
| 337 | +graph.addEdge('C', 'D'); |
| 338 | +graph.addEdge('C', 'G'); |
| 339 | +graph.addEdge('D', 'G'); |
| 340 | +graph.addEdge('D', 'H'); |
| 341 | +graph.addEdge('B', 'E'); |
| 342 | +graph.addEdge('B', 'F'); |
| 343 | +graph.addEdge('E', 'I'); |
| 344 | + |
| 345 | +console.log(graph.toString()); |
| 346 | + |
| 347 | +//------------------// |
| 348 | +// Graph traversals // |
| 349 | +//------------------// |
| 350 | +console.log("Graph traversals"); |
| 351 | + |
| 352 | +// 1. Implement breadth-first search in the Graph class above. |
| 353 | + |
| 354 | +// 2. Implement depth-first search in the Graph class above. |
| 355 | + |
| 356 | + |
| 357 | + |
| 358 | +//--------------------------// |
| 359 | +// Shortest path algorithms // |
| 360 | +//--------------------------// |
| 361 | +console.log("Shortest path algorithms"); |
| 362 | + |
| 363 | +// 1. Implement Dijkstra's algorithm. |
| 364 | +// NOTE: Replace INF (used in the book) with Infinity. |
| 365 | + |
| 366 | +// 2. Test your implementation of Dijkstra's algorithm. |
| 367 | + |
| 368 | + |
| 369 | + |
| 370 | +</script> |
| 371 | +</head> |
| 372 | +<body> |
| 373 | + See console! |
| 374 | +</body> |
| 375 | +</html> |
0 commit comments