Skip to content

Instantly share code, notes, and snippets.

@ViniciusFXavier
Last active August 30, 2021 17:38
Show Gist options
  • Save ViniciusFXavier/4725aace7f2a277653360281890449bc to your computer and use it in GitHub Desktop.
Save ViniciusFXavier/4725aace7f2a277653360281890449bc to your computer and use it in GitHub Desktop.
Algs
interface INode {
value: number;
right?: Node;
left?: Node;
}
class Node implements INode {
value: number;
right?: Node;
left?: Node;
constructor({ value, right = null, left = null }: INode) {
this.value = value;
this.right = right;
this.left = left;
}
addNode(value: number) {
if (value < this.value) {
if (this.left === null) {
this.left = new Node({ value });
} else {
this.left.addNode(value);
}
} else {
if (this.right === null) {
this.right = new Node({ value });
} else {
this.right.addNode(value);
}
}
}
search(value: number): Node | null {
if (this.value === value) {
return this;
} else if (value < this.value && this.left !== null) {
return this.left.search(value);
} else if (value > this.value && this.right !== null) {
return this.right.search(value);
}
return null;
}
height(): number {
let leftHeight = 0;
let rightHeight = 0;
if (this.left !== null) {
leftHeight = this.left.height();
}
if (this.right !== null) {
rightHeight = this.right.height();
}
if (leftHeight > rightHeight) {
return leftHeight + 1
} else {
return rightHeight + 1
}
}
visit(): Array<number> {
const result = []
// result.push(this.value); // Pre-order (ROOT, LEFT, RIGHT)
if (this.left !== null) {
result.push(...this.left.visit());
}
result.push(this.value); // In-order (LEFT, ROOT, RIGHT)
if (this.right !== null) {
result.push(...this.right.visit());
}
// result.push(this.value); // Post-order (LEFT, RIGHT, ROOT)
return result
}
}
class BinaryTree {
root: Node;
constructor() {
this.root = null;
}
addNode(value: number): void {
const newNode = new Node({ value });
if (this.root === null) {
this.root = newNode;
} else {
this.root.addNode(value);
}
}
search(value: number) {
return this.root.search(value);
}
traversal() {
return this.root.visit();
}
height() {
return this.root.height() - 1;
}
}
const tree = new BinaryTree();
const input = [3, 5, 2, 1, 4, 6, 7]
for (let value of input) {
tree.addNode(parseInt(value));
}
console.log(tree.height()) // 3
let Node = function() {
this.keys = new Map();
this.end = false;
};
let Trie = function() {
this.root = new Node();
this.add = function(input, node = this.root) {
if (input.length == 0) {
node.end = true;
return;
} else if (!node.keys.has(input[0])) {
node.keys.set(input[0], new Node());
return this.add(input.substr(1), node.keys.get(input[0]));
} else {
return this.add(input.substr(1), node.keys.get(input[0]));
};
};
this.isWord = function(word) {
let node = this.root;
while (word.length > 1) {
if (!node.keys.has(word[0])) {
return false;
} else {
node = node.keys.get(word[0]);
word = word.substr(1);
};
};
return (node.keys.has(word) && node.keys.get(word).end) ?
true : false;
};
this.print = function() {
let words = new Array();
let search = function(node, string) {
if (node.keys.size != 0) {
for (let letter of node.keys.keys()) {
search(node.keys.get(letter), string.concat(letter));
};
if (node.end) {
words.push(string);
};
} else {
string.length > 0 ? words.push(string) : undefined;
return;
};
};
search(this.root, new String());
return words.length > 0 ? words : mo;
};
};
myTrie = new Trie()
myTrie.add('ball');
myTrie.add('bat');
myTrie.add('doll');
myTrie.add('dork');
myTrie.add('do');
myTrie.add('dorm')
myTrie.add('send')
myTrie.add('sense')
console.log(myTrie.isWord('doll'))
console.log(myTrie.isWord('dor'))
console.log(myTrie.isWord('dorf'))
console.log(myTrie.print())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment