Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: knaxus/problem-solving-javascript
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: akash7948/problem-solving-javascript
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: BTree
Choose a head ref
  • 3 commits
  • 3 files changed
  • 1 contributor

Commits on Oct 17, 2019

  1. --update : add BTree

    TheSTL committed Oct 17, 2019
    Copy the full SHA
    2067168 View commit details
  2. Copy the full SHA
    7a88efe View commit details
  3. Copy the full SHA
    0fe3887 View commit details
Showing with 122 additions and 0 deletions.
  1. +1 −0 README.md
  2. +8 −0 src/_DataStructures_/Trees/BTree/Node.js
  3. +113 −0 src/_DataStructures_/Trees/BTree/index.js
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
- [Find Height of BST](src/_DataStructures_/Trees/BinarySearchTree/height-of-bst)
- [Find k Nodes from Root of BST](src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root)
- [Suffix Tree](src/_DataStructures_/SuffixTree)
- [BTree](src/_DataStructures_/BTree)

### Logical Problems

8 changes: 8 additions & 0 deletions src/_DataStructures_/Trees/BTree/Node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = class Node {
constructor(isLeaf) {
this.isLeaf = isLeaf;
this.keys = [];
this.child = [];
this.totalKey = 0;
}
};
113 changes: 113 additions & 0 deletions src/_DataStructures_/Trees/BTree/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* eslint-disable class-methods-use-this */
/* eslint-disable no-param-reassign */
const Node = require('./Node');

class BTree {
constructor(t) {
this.t = t;
this.root = null;
}

splitChild(index, parent, y) {
const z = new Node(y.isLeaf);
z.totalKey = this.t - 1;

for (let i = 0; i < this.t - 1; i += 1) {
z.keys.push(y.keys[this.t + i]);
}

if (y.isLeaf === false) {
for (let i = 0; i < this.t; i += 1) {
z.child.push(y.child[this.t + 1]);
}
y.child = y.child.slice(0, this.t);
}
y.totalKey = this.t - 1;

for (let i = parent.totalKey; i >= index + 1; i -= 1) {
parent.child[i + 1] = parent.child[i];
}
parent.child[index + 1] = z;

for (let i = parent.totalKey - 1; i >= index; i -= 1) {
parent.keys[i + 1] = parent.keys[i];
}

parent.keys[index] = y.keys[this.t - 1];
y.keys = y.keys.slice(0, this.t - 1);
parent.totalKey += 1;
}

insertNonFull(node, key) {
let i = node.totalKey - 1;

if (node.isLeaf === true) {
while (i >= 0 && node.keys[i] > key) {
node.keys[i + 1] = node.keys[i];
i -= 1;
}
node.keys[i + 1] = key;
node.totalKey += 1;
} else {
while (i >= 0 && node.keys[i] > key) i -= 1;

if (node.child[i + 1].totalKey === 2 * this.t - 1) {
this.splitChild(i + 1, node, node.child[i + 1]);
if (node.keys[i + 1] < key) i += 1;
}
this.insertNonFull(node.child[i + 1], key);
}
}

insert(key) {
if (this.root === null) {
this.root = new Node(true);
this.root.keys.push(key);
this.root.totalKey = 1;
} else if (this.root.totalKey === 2 * this.t - 1) {
const newRoot = new Node(false);
newRoot.child[0] = this.root;
this.splitChild(0, newRoot, this.root);
let i = 0;
if (newRoot.keys[i] < key) i += 1;
this.insertNonFull(newRoot.child[i], key);
this.root = newRoot;
} else {
this.insertNonFull(this.root, key);
}
}
}

// const tree = new BTree(3);
// tree.insert(10);
// tree.insert(20);
// tree.insert(30);
// tree.insert(40);
// tree.insert(50);
// tree.insert(60);
// tree.insert(70);
// tree.insert(80);
// tree.insert(90);
// tree.insert(100);
// tree.insert(110);
// tree.insert(120);
// tree.insert(130);
// tree.insert(140);
// tree.insert(150);
// tree.insert(160);


// console.log('----root---');
// console.log(tree.root);

// console.log('----child0---');
// console.log(tree.root.child[0]);

// console.log('----child1---');
// console.log(tree.root.child[1]);

// console.log('----child2---');
// console.log(tree.root.child[2]);


module.export = BTree;