Skip to content

Instantly share code, notes, and snippets.

@dineshbalaji
dineshbalaji / heapsort.js
Created November 3, 2019 09:05
Heap Datastrcture / Heap sort
function heapify(array,parent){
let left = 2*parent+1;
let right = 2*parent+2;
let max = index; // consider parent as a max value;
if(array[left] > array[max]){
max = left;
}
if(array[right] > array[max]){
class AVLTree {
constructor(value) {
// preparing root element
this.value = value;
this.left = null;
this.right = null;
this.height = 1;
}
add(value) {
@dineshbalaji
dineshbalaji / binary_tree_algorithm.js
Created March 31, 2019 02:24
binary tree algorithm
/* max hight of tree */
function traceMaxHight(tree) {
var queue = [tree, 'reset'];
var count = 0;
while (queue.length > 1) {
var node = queue.shift();
function breathTraversal(tree) {
var queue = [tree];
var result = [];
while (queue.length > 0) {
var node = queue.shift();
result.push(node.value);
function deepthTraversalUsingRecursive(tree) {
let result = [];
function trace(node) {
/* result.push(node.value); // pre-order tracing */
node.left && trace(node.left);
result.push(node.value); // in-order tracing
node.right && trace(node.right);
/* Binary Tree design - deepthTraversal using loop - pre-order*/
function deepthTraversalUsingLoop(tree) {
let result = [];
let stackList = [tree];
while (stackList.length > 0) {
let node = stackList.pop();
@dineshbalaji
dineshbalaji / BinaryTree.js
Created March 13, 2019 01:58
Binary Tree
class BinaryTree {
constructor(value) {
// preparing root element
this.value = value;
this.left = null;
this.right = null;
}
add(value) {
if (value < this.value) {
@dineshbalaji
dineshbalaji / doublyLinkedList.js
Last active March 13, 2019 01:05
Doubly Linked List
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
}
createElement(value, next, prev) {
return { value, next, prev }
}
addToHead(value) {
let node = this.createElement(value,null ,this.head)