This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AVLTree { | |
constructor(value) { | |
// preparing root element | |
this.value = value; | |
this.left = null; | |
this.right = null; | |
this.height = 1; | |
} | |
add(value) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* max hight of tree */ | |
function traceMaxHight(tree) { | |
var queue = [tree, 'reset']; | |
var count = 0; | |
while (queue.length > 1) { | |
var node = queue.shift(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function breathTraversal(tree) { | |
var queue = [tree]; | |
var result = []; | |
while (queue.length > 0) { | |
var node = queue.shift(); | |
result.push(node.value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Binary Tree design - deepthTraversal using loop - pre-order*/ | |
function deepthTraversalUsingLoop(tree) { | |
let result = []; | |
let stackList = [tree]; | |
while (stackList.length > 0) { | |
let node = stackList.pop(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BinaryTree { | |
constructor(value) { | |
// preparing root element | |
this.value = value; | |
this.left = null; | |
this.right = null; | |
} | |
add(value) { | |
if (value < this.value) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |