Skip to content

Instantly share code, notes, and snippets.

@PulkitAgg
Last active December 26, 2020 14:18
Show Gist options
  • Save PulkitAgg/798309429c981706c272e6f888942200 to your computer and use it in GitHub Desktop.
Save PulkitAgg/798309429c981706c272e6f888942200 to your computer and use it in GitHub Desktop.
Binary Tree Implementation From Array
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor(){
this.root = null;
}
getRootNode() {
return this.root;
}
createBTFromArray(arr) {
if(arr.length === 0) {
this.root = null;
return this.root;
}
this.root = new Node(arr[0]);
this.root = this.insertNode(arr, this.root, 0, arr.length);
}
// -1 means there is null
createBTFromArrayWithNegativeValue(arr){
if(arr.length === 0 || arr[0] === -1) {
this.root = null;
return this.root;
}
this.root = new Node(arr[0]);
this.root = this.insertNodewithNegativeVal(arr, this.root, 0, arr.length);
}
insertNode(arr, root, pos, arrLen) {
if( pos < arrLen) {
root = new Node(arr[pos]);
root.left = this.insertNode(arr, root.left, 2*pos +1, arrLen);
root.right = this.insertNode(arr, root.right, 2*pos +2, arrLen);
}
return root;
}
insertNodewithNegativeVal(arr, root, pos, arrLen) {
if(pos < arrLen) {
if(arr[pos] === -1) {
return root;
}
root = new Node(arr[pos]);
root.left = this.insertNodewithNegativeVal(arr, root.left, 2*pos + 1, arrLen);
root.right = this.insertNodewithNegativeVal(arr, root.right, 2*pos + 2, arrLen);
}
return root;
}
inOrder(node) {
if(node) {
this.inOrder(node.left);
console.log(node.data);
this.inOrder(node.right);
}
return;
}
inOrderWithoutRecursion() {
let root = this.getRootNode();
let stack = [], curr = null;
if(root) {
curr = root;
}
while(stack.length !== 0 || curr !== null) {
while(curr !== null) {
stack.push(curr);
curr = curr.left
}
let temp = stack.pop();
console.log(temp.data);
curr = temp.right;
}
}
preOrder(root) {
if(root) {
console.log(root.data);
this.preOrder(root.left);
this.preOrder(root.right);
}
return;
}
preOrderWithoutRecursion() {
let root = this.getRootNode();
let stack = [];
let curr = null;
if(root){
curr = root;
stack.push(root);
}
while(stack.length !==0) {
let temp = stack.pop();
console.log(temp.data);
if(temp.right) {
stack.push(temp.right);
}
if(temp.left) {
stack.push(temp.left);
}
}
return;
}
postOrder(node) {
if(node){
this.postOrder(node.left);
this.postOrder(node.right);
console.log(node.data)
}
}
postOrderWithoutRecursion() {
let root = this.getRootNode();
let stack1 = [], stack2 = [];
if(root){
stack1.push(root);
while(stack1.length !==0) {
let temp = stack1.pop();
stack2.push(temp.data);
if(temp.left){
stack1.push(temp.left);
}
if(temp.right){
stack1.push(temp.right);
}
}
while(stack2.length !== 0) {
let temp = stack2.pop();
console.log(temp);
}
}
return;
}
search(root, value){
if(root){
if(root.data === value) {
return true;
}
//check in left sub tree
let isInLeftSubTree = this.search(root.left, value);
// if exist then return
if(isInLeftSubTree) {
return true;
}
// check in right sub tree
return this.search(root.right, value);
}
return false;
}
levelOrderTraversal(root, arr, pos) {
if(root){
arr[pos] = root.data;
this.levelOrderTraversal(root.left, arr, 2*pos + 1);
this.levelOrderTraversal(root.right, arr, 2*pos + 2);
}
return arr;
}
spiralTraversal(root) {
let arr = [], result = [];
arr = this.levelByLevelTraversalForSpiral(root, 0, arr);
for(let i =0; i<arr.length; i++) {
if(i%2 === 0 ) {
result = [...result, ...arr[i]];
} else {
result = [...result, ...arr[i].reverse()];
}
}
return result;
}
levelByLevelTraversalForSpiral(root, level, arr) {
if(root) {
if(Array.isArray(arr[level])){
arr[level].push(root.data);
} else {
arr[level] = [root.data];
}
this.levelByLevelTraversalForSpiral(root.left, level+1, arr);
this.levelByLevelTraversalForSpiral(root.right, level+1, arr);
}
return arr;
}
}
let tree = new BinaryTree();
tree.createBTFromArray([1,2,3,4,5,6,7,8,9,10])
tree.inOrder(tree.getRootNode())
console.log('--------------');
let treeWithNull = new BinaryTree();
treeWithNull.createBTFromArrayWithNegativeValue([1,2,3,4,-1,6,-1,8,9, -1, -1, 12]);
treeWithNull.inOrder(treeWithNull.getRootNode())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment