Last active
December 29, 2015 22:59
-
-
Save OfTheDelmer/7739702 to your computer and use it in GitHub Desktop.
rough thought of a trie, needs work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var DigitalNode = function(data){ | |
| this.ref = null; | |
| data === undefined? this.data = null: this.data = data; | |
| }; | |
| var Trie = function(){ | |
| this.primes = [2]; | |
| this.members = Object(); | |
| } | |
| Trie.prototype.nextPrime = function(){ | |
| num = this.primes[this.primes.length - 1] + 1; | |
| for(var i = 0; i < this.primes.length; i++){ | |
| if(num % this.primes[i] === 0){ | |
| num += 1; | |
| i = -1; | |
| } | |
| } | |
| return this.primes.push(num); | |
| } | |
| Trie.prototype.getChildBase = function(node){ | |
| if(node.ref !== null){ | |
| var ref = parseInt(node.ref,36); | |
| for(var index = 0; index < this.primes.length; index++){ | |
| if( ref % this.primes[index] !== 0){ | |
| return index; | |
| } | |
| } | |
| this.nextPrime(); | |
| return this.primes.length - 1; | |
| } | |
| } | |
| Trie.prototype.encodeChild = function(data, parent){ | |
| var newNode = new DigitalNode(data); | |
| var newRef; | |
| if(parent !== undefined){ | |
| var base = this.getChildBase(parent); | |
| newRef = parseInt(parent.ref,36)*this.primes[base]; | |
| while(this.members[newRef.toString(36)] !== undefined){ | |
| newRef = newRef*this.primes[base]; | |
| } | |
| } else { | |
| newRef = 2; | |
| } | |
| console.log(newRef) | |
| newNode.ref = newRef.toString(36); | |
| this.currentNode = newNode; | |
| this.members[newNode.ref] = newNode; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment