Skip to content

Instantly share code, notes, and snippets.

View helabenkhalfallah's full-sized avatar
🎯
Focusing

Héla Ben Khalfallah helabenkhalfallah

🎯
Focusing
View GitHub Profile
@helabenkhalfallah
helabenkhalfallah / BTreeNode.js
Last active February 11, 2024 20:21
BTreeNode
class BTreeNode {
constructor(isLeaf) {
/**
* @type {number[]} list of values in the node
*/
this.values = [];
/**
* @type {boolean} is a leaf
*/
this.leaf = isLeaf;
class RedBlackTree {
constructor() {
this.root = null
}
insert(value) {
// Define an inner helper function for the recursive insertion
const insertHelper = (node) => {
// The current node we're considering
const currNode = node
const NodeColor = {
RED: 'RED',
BLACK: 'BLACK',
}
class RBTNode {
constructor(value, parent = null) {
this.value = value
this.left = null
this.right = null
@helabenkhalfallah
helabenkhalfallah / BinarySearchTree.js
Created February 10, 2024 21:14
BinarySearchTree
class BinarySearchTree {
constructor(key, value = key) {
this.root = new BinarySearchTreeNode(key, value);
}
*inOrderTraversal(node = this.root) {
if (node.left) yield* this.inOrderTraversal(node.left);
yield node;
if (node.right) yield* this.inOrderTraversal(node.right);
}
@helabenkhalfallah
helabenkhalfallah / BinarySearchTreeNode.js
Last active February 10, 2024 21:19
BinarySearchTreeNode
class BinarySearchTreeNode {
constructor(key, value = key, parent = null) {
this.key = key;
this.value = value;
this.parent = parent;
this.left = null;
this.right = null;
}
get isLeaf() {
@helabenkhalfallah
helabenkhalfallah / devoxx-1.js
Created April 12, 2023 16:05
Algorithme Simple
const getItems = (data, start, end) => {
if (data &&
data.length > 0 &&
start >= 0 &&
end <= data.length &&
start < end) {
return data.slice(start, end); // immutable
}
return ([]);
const vintageLinearSearch = (data, target) => {
for (let i in data) {
if (data[i] === target) return i
}
return -1
}
console.log(vintageLinearSearch([1, 2, 3, 4], 1)) // 0
console.log(vintageLinearSearch([1, 2, 3, 4], 4)) // 3
console.log(vintageLinearSearch([1, 2, 3, 4], 6)) // -1
@helabenkhalfallah
helabenkhalfallah / vintage-insertion-sort.js
Last active July 10, 2022 16:05
Vintage Insertion Sort
const vintageInsertionSort = (data) => {
console.log('data input : ', data);
for (let i = 1; i < data.length; i++) {
console.log('data i : ', data);
// First, choose the element at index 1
let current = data[i];
let j;
const merge = (arr1, arr2) => {
let sorted = [];
while (arr1.length && arr2.length) {
if (arr1[0] < arr2[0]) {
sorted.push(arr1.shift());
} else {
sorted.push(arr2.shift());
}
}
@helabenkhalfallah
helabenkhalfallah / vintage-bubble-sort.js
Last active July 10, 2022 15:54
Vintage Bubble Sort
const vintageBubbleSort = (data) => {
console.log('data input : ', data);
for (let i = 0; i < data.length; i++) {
console.log('data i : ', data);
for (let j = 0; j < data.length; j++) {
if (data[j] > data[j + 1]) {
let temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}