Permalink
Browse files

Adding js searches, except for A*

  • Loading branch information...
1 parent 2097dee commit 44ca6d34380942600fb3a61a09e6a2dc4a4841c1 @clarle clarle committed Oct 18, 2011
Showing with 68 additions and 0 deletions.
  1. +2 −0 unit02/js/search.js
  2. +66 −0 unit02/js/structures.js
View
@@ -10,6 +10,7 @@ function tree_search(tree, source, dest, frontier) {
return node;
}
frontier.expand(tree[node]);
+ }
return null;
}
@@ -19,6 +20,7 @@ function graph_search(graph, source, dest, frontier) {
var node;
while (!frontier.empty()) {
node = frontier.pop();
+ console.log(node);
if (node == dest) {
return node;
}
View
@@ -0,0 +1,66 @@
+// Stack acts as a wrapper for the Array object, adding empty() and extend()
+// Can probably be written in a better fashion, rather than wrapping like this
+
+function Stack() {
+ this.array = [];
+ this.empty = function() {
+ return (this.array.length == 0);
+ };
+ this.length = function() {
+ return this.array.length;
+ };
+ this.indexOf = function(item) {
+ return this.array.indexOf(item);
+ };
+ this.push = function(item) {
+ return this.array.push(item);
+ };
+ this.pop = function() {
+ return this.array.pop();
+ };
+ this.extend = function(items) {
+ for (item in items) { this.push(item); }
+ };
+ this.toString = function() {
+ return this.array.toString();
+ };
+}
+
+function Queue() { }
+
+Queue.prototype = new Stack();
+
+Queue.prototype.pop = function(item) {
+ return this.array.shift(item);
+}
+
+function PriorityQueue(order) {
+ this.sorted = false;
+ this.order = order;
+ var sort_lo = function(a, b) { return b.pri - a.pri; }
+ var sort_hi = function(a, b) { return a.pri - b.pri; }
+ var style;
+ if (order.lo) {
+ style = sort_lo;
+ } else {
+ style = sort_hi;
+ }
+
+ this.sort = function() {
+ this.array.sort(style);
+ this.sorted = true;
+ }
+}
+
+PriorityQueue.prototype = new Stack();
+
+PriorityQueue.prototype.push = function(item) {
+ if (!item.pri || !item.obj) { throw "TypeError: Not a valid priority queue object"; }
+ this.sorted = false;
+ return this.array.push(item);
+}
+
+PriorityQueue.prototype.pop = function() {
+ if (!this.sorted) this.sort();
+ return this.array.pop();
+}

0 comments on commit 44ca6d3

Please sign in to comment.