diff --git a/src/algorithms/string/z-algorithm/README.md b/src/algorithms/string/z-algorithm/README.md index 0f12bc7333..2351d2af41 100644 --- a/src/algorithms/string/z-algorithm/README.md +++ b/src/algorithms/string/z-algorithm/README.md @@ -4,7 +4,7 @@ The Z-algorithm finds occurrences of a "word" `W` within a main "text string" `T` in linear time `O(|W| + |T|)`. Given a string `S` of length `n`, the algorithm produces -an array, `Z` where `Z[i]` represents the longest substring +an array, `Z` where `Z[i]` represents the longest substring starting from `S[i]` which is also a prefix of `S`. Finding `Z` for the string obtained by concatenating the word, `W` with a nonce character, say `$` followed by the text, `T`, diff --git a/src/algorithms/string/z-algorithm/zAlgorithm.js b/src/algorithms/string/z-algorithm/zAlgorithm.js index 1c532f5f8f..d37769e7af 100644 --- a/src/algorithms/string/z-algorithm/zAlgorithm.js +++ b/src/algorithms/string/z-algorithm/zAlgorithm.js @@ -93,7 +93,6 @@ function buildZArray(zString) { } } } - // Return generated zArray. return zArray; } diff --git a/src/data-structures/tree/b-tree/BTree.js b/src/data-structures/tree/b-tree/BTree.js new file mode 100644 index 0000000000..8c73938189 --- /dev/null +++ b/src/data-structures/tree/b-tree/BTree.js @@ -0,0 +1,30 @@ +import BTreeNode from './BTreeNode'; + +export default class BTree { + constructor(degree = 0) { + this.root = null; + this.degree = degree; + } + + insert(value) { + if (this.root === null) { + this.root = new BTreeNode(this.degree, true); + this.root.keys[0] = value; + this.root.numKeys = 1; + } else { + if (root.numKeys === (2 * this.degree) - 1) { + let i = 0; + const newRoot = new BTreeNode(this.degree, false); + newRoot.children[0] = this.root; + newRoot.splitChild(0, this.root); + if (newRoot.keys[0] < value) { + i += 1; + } + newRoot.children[i].insert(value); + this.root = newRoot; + } else { + this.root.insert(value); + } + } + } +} diff --git a/src/data-structures/tree/b-tree/BTreeNode.js b/src/data-structures/tree/b-tree/BTreeNode.js new file mode 100644 index 0000000000..125b92a21a --- /dev/null +++ b/src/data-structures/tree/b-tree/BTreeNode.js @@ -0,0 +1,92 @@ +import Comparator from '../../../utils/comparator/Comparator'; + +export default class BTreeNode { + constructor(degree = 0, isLeaf = true) { + this.degree = degree; + this.isLeaf = isLeaf; + this.keys = new Array((2 * this.degree) - 1); + this.children = new Array(2 * this.degree); + this.numKeys = 0; + this.nodeComparator = new Comparator(); + } + + setKeys(value) { + this.numKeys = value; + } + + traversal() { + let i; + let traverse = []; + for (i = 0; i < this.numKeys; i += 1) { + if (!this.isLeaf) { + traverse = traverse.concat(this.children[i].traversal()); + } + traverse.push(this.keys[i]); + } + if (!this.isLeaf) { + traverse = traverse.concat(this.children[i].traversal()); + } + return traverse; + } + + find(value) { + let i = 0; + while (i < this.numKeys && value > this.keys[i]) { + i += 1; + } + if (this.nodeComparator.equal(value, this.keys[i])) { + return this; + } + if (this.isLeaf) { + return null; + } + return this.children[i].find(value); + } + + insert(value) { + let i = this.numKeys - 1; + if (this.isLeaf) { + while (i >= 0 && this.keys[i] > value) { + this.keys[i + 1] = this.keys[i]; + i -= 1; + } + this.keys[i + 1] = value; + this.numKeys += 1; + } else { + while (i >= 0 && this.keys[i] > value) { + i -= 1; + } + if (this.children[i + 1].numKeys === (2 * this.degree) - 1) { + this.splitChild(i + 1, this.children[i + 1]); + if (this.keys[i + 1] < value) { + i += 1; + } + } + this.children[i + 1].insert(value); + } + } + + splitChild(i, node) { + const temp = new BTreeNode(node.degree, node.isLeaf); + let j; + temp.numKeys = this.degree - 1; + for (j = 0; j < this.degree - 1; j += 1) { + temp.keys[j] = node.keys[j + this.degree]; + } + if (!node.isLeaf) { + for (j = 0; j < this.degree; j += 1) { + temp.children[j] = node.children[j + this.degree]; + } + } + node.setKeys(this.degree - 1); + for (j = this.numKeys; j >= i + 1; j -= 1) { + this.children[j + 1] = this.children[j]; + } + this.children[i + 1] = temp; + for (j = this.numKeys - 1; j >= i; j -= 1) { + this.keys[j + 1] = this.keys[j]; + } + this.keys[i] = node.keys[this.degree - 1]; + this.numKeys = this.numKeys + 1; + } +} diff --git a/src/data-structures/tree/b-tree/README.md b/src/data-structures/tree/b-tree/README.md new file mode 100644 index 0000000000..e69de29bb2