Skip to content

Commit

Permalink
Created shell for the AVL tree
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed May 28, 2013
1 parent cef0653 commit c32dfb7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
2 changes: 2 additions & 0 deletions index.js
@@ -0,0 +1,2 @@
module.exports.BinarySearchTree = require('./lib/bst');
module.exports.AVLTree = require('./lib/avltree');
40 changes: 40 additions & 0 deletions lib/avltree.js
@@ -0,0 +1,40 @@
/**
* Self-balancing binary search tree using the AVL implementation
*/
var BinarySearchTree = require('./bst')
, customUtils = require('./customUtils')
, util = require('util')
;

/**
* Constructor
* @param {Object} options Optional
* @param {Boolean} options.unique Whether to enforce a 'unique' constraint on the key or not
* @param {Key} options.key Initialize this BST's key with key
* @param {Value} options.value Initialize this BST's data with [value]
* @param {Function} options.compareKeys Initialize this BST's compareKeys
*/
function AVLTree (options) {
options = options || {};

this.left = null;
this.right = null;
this.parent = options.parent !== undefined ? options.parent : null;
this.key = options.key !== undefined ? options.key : null;
this.data = options.value ? [options.value] : [];
this.unique = options.unique || false;

this.compareKeys = options.compareKeys || customUtils.defaultCompareKeysFunction;
this.checkValueEquality = options.checkValueEquality || customUtils.defaultCheckValueEquality;
}


/**
* Inherit basic functions from the basic binary search tree
*/
util.inherits(AVLTree, BinarySearchTree);



// Interface
module.exports = AVLTree;
24 changes: 3 additions & 21 deletions lib/bst.js
@@ -1,25 +1,7 @@
/**
* Simple binary search tree
*/

/*
* Default compareKeys function will work for numbers, strings and dates
*/
function defaultCompareKeysFunction (a, b) {
if (a < b) { return -1; }
if (a > b) { return 1; }
if (a === b) { return 0; }

throw { message: "Couldn't compare elements", a: a, b: b };
}


/**
* Check whether two values are equal (used in non-unique deletion)
*/
function defaultCheckValueEquality (a, b) {
return a === b;
}
var customUtils = require('./customUtils');


/**
Expand All @@ -40,8 +22,8 @@ function BinarySearchTree (options) {
this.data = options.value ? [options.value] : [];
this.unique = options.unique || false;

this.compareKeys = options.compareKeys || defaultCompareKeysFunction;
this.checkValueEquality = options.checkValueEquality || defaultCheckValueEquality;
this.compareKeys = options.compareKeys || customUtils.defaultCompareKeysFunction;
this.checkValueEquality = options.checkValueEquality || customUtils.defaultCheckValueEquality;
}


Expand Down
22 changes: 22 additions & 0 deletions lib/customUtils.js
Expand Up @@ -14,3 +14,25 @@ function getRandomArray (n) {
return res;
};
module.exports.getRandomArray = getRandomArray;


/*
* Default compareKeys function will work for numbers, strings and dates
*/
function defaultCompareKeysFunction (a, b) {
if (a < b) { return -1; }
if (a > b) { return 1; }
if (a === b) { return 0; }

throw { message: "Couldn't compare elements", a: a, b: b };
}
module.exports.defaultCompareKeysFunction = defaultCompareKeysFunction;


/**
* Check whether two values are equal (used in non-unique deletion)
*/
function defaultCheckValueEquality (a, b) {
return a === b;
}
module.exports.defaultCheckValueEquality = defaultCheckValueEquality;

0 comments on commit c32dfb7

Please sign in to comment.