Skip to content

Instantly share code, notes, and snippets.

@tejzpr
Last active February 6, 2018 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tejzpr/a26992f8f1ca105095a635978a0035ac to your computer and use it in GitHub Desktop.
Save tejzpr/a26992f8f1ca105095a635978a0035ac to your computer and use it in GitHub Desktop.
Word counter using a B-Tree
class Node {
constructor(data) {
this.data = data;
this.count = 1;
this.left = null;
this.right = null;
this.inorderOut = [];
this.preorderOut = [];
this.postorderOut = [];
}
insert(data) {
if(data < this.data) {
if(this.left != null) {
this.left.insert(data);
} else {
this.left = new Node(data);
}
} else if(data > this.data) {
if(this.right != null) {
this.right.insert(data);
} else {
this.right = new Node(data);
}
} else if(data == this.data) {
this.count++;
}
}
contains(item) {
if(item === this.data) {
return true;
}
if(item < this.data) {
if(this.left != null) {
return this.left.contains(item);
} else {
return false;
}
} else {
if(this.right != null) {
return this.right.contains(item);
} else {
return false;
}
}
}
inorder(out) {
if(!out) {
out=this.inorderOut;
}
if(this.left != null) {
this.left.inorder(out);
}
out.push([this.data, this.count]);
if(this.right != null) {
this.right.inorder(out);
}
return this.inorderOut;
}
preorder(out) {
if(!out) {
out=this.preorderOut;
}
out.push(this.data);
if(this.left != null) {
this.left.preorder(out);
}
if(this.right != null) {
this.right.preorder(out);
}
return this.preorderOut;
}
postorder(out) {
if(!out) {
out=this.postorderOut;
}
if(this.left != null) {
this.left.postorder(out);
}
if(this.right != null) {
this.right.postorder(out);
}
out.push(this.data);
return this.postorderOut;
}
}
let root = null;
let sentence = `If you're visiting this page, you're likely here because you're searching for a random sentence. Sometimes a random word just isn't enough, and that is where the random sentence generator comes into play. By inputting the desired number, you can make a list of as many random sentences as you want or need. Producing random sentences can be helpful in a number of different ways. For writers, a random sentence can help them get their creative juices flowing. Since the topic of the sentence is completely unknown, it forces the writer to be creative when the sentence appears. There are a number of different ways a writer can use the random sentence for creativity. The most common way to use the sentence is to begin a story. Another option is to include it somewhere in the story. A much more difficult challenge is to use it to end a story. In any of these cases, it forces the writer to think creatively since they have no idea what sentence will appear from the tool. For those writers who have writers' block, this can be an excellent way to take a step to crumbling those walls. By taking the writer away from the subject matter that is causing the block, a random sentence may allow them to see the project they're working on in a different light and perspective. Sometimes all it takes is to get that first sentence down to help break the block. It can also be successfully used as a daily exercise to get writers to begin writing. Being shown a random sentence and using it to complete a paragraph each day can be an excellent way to begin any writing session.`.split(" ")
for(let item of sentence) {
if(item.length > 0 && item !== ".") {
if(root == null) {
root = new Node((item.toLowerCase()));
} else {
root.insert((item.toLowerCase()));
}
}
}
console.log(root.inorder());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment