Skip to content

Instantly share code, notes, and snippets.

View cagataycali's full-sized avatar
🧬

./c² cagataycali

🧬
View GitHub Profile
@cagataycali
cagataycali / bind.js
Created November 22, 2021 12:21
[JavaScript] Naive bind
// Bind returns function closure for later usage,
Function.prototype.myBind = function (context) {
const fn = this;
return function () {
fn.apply(context, arguments);
}
}
// Apply, bind invokes the function immediately.
@cagataycali
cagataycali / depths.js
Created November 17, 2021 06:46
[BinaryTree] Node depths
const assert = require("assert");
// O(n) time | O(h) space - h is height.
function nodeDepths(root, depth = 0) {
if (root === null) {
return 0;
}
return depth + nodeDepths(root.left, depth + 1) + nodeDepths(root.right, depth + 1);
}
@cagataycali
cagataycali / invert.js
Created November 16, 2021 20:00
[JavaScript] Invert binary tree
// O(n) time & space
function invertBinaryTree(tree) {
if (tree === null) return;
swapLeftAndRight(tree);
invertBinaryTree(tree.left);
invertBinaryTree(tree.right);
}
function swapLeftAndRight(tree) {
const left = tree.left;
@cagataycali
cagataycali / find-kth-largest.js
Last active November 16, 2021 19:34
[JavaScript] Binary search tree find kth largest value
const assert = require("assert");
function BST(value) {
this.value = value;
this.left = null;
this.right = null;
}
function findKthLargestValueInBst(tree, k) {
const result = [];
@cagataycali
cagataycali / fast-coding.js
Last active November 16, 2021 00:22
[JavaScript] Binary tree find successor
const assert = require('assert');
// Finding successor means, traverse the tree by inorder and the selected node's previous node is successor.
function findSuccessor(root, node) {
// Right child exists, move to the left most child for right branch,
// If the right child does not exists, successor will be rigth most parent,
if (node.right !== null) return getLeftmostChild(node.right);
return getRightmostParent(node);
}
@cagataycali
cagataycali / bst-min-height.js
Last active November 16, 2021 00:15
[JavaScript] Binary search tree find min height
const assert = require('assert');
// Write function that takes in a non-empty sorted array of distinct integers,
// constructs a BST from the integers, and returns the root of the BST.
function minHeightBst(array) {
// Find the median of array,
return constructMinHeightBST(array, 0, array.length - 1)
}
function constructMinHeightBST(array, startIndex, endIndex) {
@cagataycali
cagataycali / traversal.js
Created November 13, 2021 23:18
[JavaScript] Binary search tree traversals
const assert = require("assert");
function BST(value) {
this.value = value;
this.left = null;
this.right = null;
}
// Left, root, right - root is mid
function inOrderTraverse(tree, array) {
@cagataycali
cagataycali / auto-complete.html
Created November 13, 2021 22:48
[JavaScript] Auto complete
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Auto complete</title>
<style>
.auto-complete {
width: 300px;
@cagataycali
cagataycali / accordion.html
Last active November 12, 2021 21:15
[JavaScript] Over engineered accordion
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Accordion</title>
<style>
.accordion-wrapper {
display: flex;
@cagataycali
cagataycali / breath-first-search.js
Created November 8, 2021 23:07
[JavaScript] Graph breath first search
class Node {
constructor(name) {
this.name = name;
this.children = [];
}
addChild(name) {
this.children.push(new Node(name));
return this;
}