Skip to content

Commit

Permalink
add callback to the put method of binary search tree
Browse files Browse the repository at this point in the history
  • Loading branch information
marty-wang committed Oct 19, 2011
1 parent 4f9a864 commit b4efa29
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 15 deletions.
24 changes: 23 additions & 1 deletion demo/app/coffee/bst_app.coffee
Expand Up @@ -6,10 +6,24 @@ do (App) ->
@_width = width
@_height = height
@_paper = Raphael container, width, height
@_data = new Alg.BST()

_setup.call this

put: (key, value) ->
trace = new Alg.Stack()
@_data.put key, value, (obj)->
trace.push obj

iterator = trace.iterator()
traceStr = ""
while iterator.hasNext()
item = iterator.next()
if traceStr isnt ""
traceStr += "; "
traceStr += "branch #{item.branch} value: #{item.value} isNew: #{item.isNew}"

console.log traceStr

get: (key) ->

Expand All @@ -25,6 +39,14 @@ do (App) ->
###############################################################################

$ ->
console.log "BST Demo"

bstDemo = new App.BSTDemo "bst-demo"
bstDemo.put 4, "node 4"
bstDemo.put 2, "node 2"
bstDemo.put 5, "node 5"
bstDemo.put 3, "node 3"
bstDemo.put 1, "node 1"

bstDemo.put 1, "node 1 updated"

console.log bstDemo
27 changes: 25 additions & 2 deletions demo/app/compiled/bst_app.js
Expand Up @@ -12,9 +12,26 @@
this._width = width;
this._height = height;
this._paper = Raphael(container, width, height);
this._data = new Alg.BST();
_setup.call(this);
}
BSTDemo.prototype.put = function(key, value) {};
BSTDemo.prototype.put = function(key, value) {
var item, iterator, trace, traceStr;
trace = new Alg.Stack();
this._data.put(key, value, function(obj) {
return trace.push(obj);
});
iterator = trace.iterator();
traceStr = "";
while (iterator.hasNext()) {
item = iterator.next();
if (traceStr !== "") {
traceStr += "; ";
}
traceStr += "branch " + item.branch + " value: " + item.value + " isNew: " + item.isNew;
}
return console.log(traceStr);
};
BSTDemo.prototype.get = function(key) {};
return BSTDemo;
})();
Expand All @@ -28,7 +45,13 @@
})(App);
$(function() {
var bstDemo;
console.log("BST Demo");
bstDemo = new App.BSTDemo("bst-demo");
return console.log(bstDemo);
bstDemo.put(4, "node 4");
bstDemo.put(2, "node 2");
bstDemo.put(5, "node 5");
bstDemo.put(3, "node 3");
bstDemo.put(1, "node 1");
return bstDemo.put(1, "node 1 updated");
});
}).call(this);
1 change: 1 addition & 0 deletions demo/binary_search_tree.html
Expand Up @@ -8,6 +8,7 @@
<script type="text/javascript" src="app/lib/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="app/lib/raphael-min.js"></script>

<script type="text/javascript" src="../src/compiled/basics.js"></script>
<script type="text/javascript" src="../src/compiled/binary_search_tree.js"></script>

<script type="text/javascript" src="app/compiled/app.js"></script>
Expand Down
30 changes: 24 additions & 6 deletions src/coffee/binary_search_tree.coffee
Expand Up @@ -17,9 +17,9 @@ class BST
constructor: ->
@root = null

put: (key, value) ->
put: (key, value, fn) ->
throw "Key and value cannot be null or undefined" if (not key? or not value?)
@root = _put @root, key, value
@root = _put.call this, @root, key, value, fn

# return value, or return null if nothing found
get: (key) ->
Expand All @@ -42,17 +42,35 @@ class BST
else
return node.value

_put = (node, key, value) ->
return new Node key, value unless node?
_put = (node, key, value, fn) ->
branch = 0

unless node?
newNode = new Node key, value unless node?
fn.call this, {
branch: branch
key: key
value: value
isNew: true
} if fn?
return newNode

if node.key > key
node.left = _put node.left, key, value
branch = -1
node.left = _put.call this, node.left, key, value, fn
else if node.key < key
node.right = _put node.right, key, value
branch = 1
node.right = _put.call this, node.right, key, value, fn
else
node.value = value

node.n = _size(node.left) + _size(node.right) + 1
fn.call this, {
branch: branch
key: node.key
value: node.value
isNew: false
} if fn?
node

_size = (node) ->
Expand Down
35 changes: 29 additions & 6 deletions src/compiled/binary_search_tree.js
Expand Up @@ -16,11 +16,11 @@
function BST() {
this.root = null;
}
BST.prototype.put = function(key, value) {
BST.prototype.put = function(key, value, fn) {
if (!(key != null) || !(value != null)) {
throw "Key and value cannot be null or undefined";
}
return this.root = _put(this.root, key, value);
return this.root = _put.call(this, this.root, key, value, fn);
};
BST.prototype.get = function(key) {
if (key == null) {
Expand All @@ -43,18 +43,41 @@
return node.value;
}
};
_put = function(node, key, value) {
_put = function(node, key, value, fn) {
var branch, newNode;
branch = 0;
if (node == null) {
return new Node(key, value);
if (node == null) {
newNode = new Node(key, value);
}
if (fn != null) {
fn.call(this, {
branch: branch,
key: key,
value: value,
isNew: true
});
}
return newNode;
}
if (node.key > key) {
node.left = _put(node.left, key, value);
branch = -1;
node.left = _put.call(this, node.left, key, value, fn);
} else if (node.key < key) {
node.right = _put(node.right, key, value);
branch = 1;
node.right = _put.call(this, node.right, key, value, fn);
} else {
node.value = value;
}
node.n = _size(node.left) + _size(node.right) + 1;
if (fn != null) {
fn.call(this, {
branch: branch,
key: node.key,
value: node.value,
isNew: false
});
}
return node;
};
_size = function(node) {
Expand Down

0 comments on commit b4efa29

Please sign in to comment.