Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

stack using array in javaScript #252

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions JavaScript/BinaryTreeTraversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* @description Tree Traversal (Preorder, Inorder, Postorder)
* @author prasanna kale
* @language Javascript
*/
class TreeNode {
constructor(val, left, right) {
this.val = (val === undefined) ? 0 : val;
this.left = (left === undefined) ? null : left;
this.right = (right === undefined) ? null : right;
}
}

/**
* @order root -> left -> right
* @param {TreeNode} root
*/
function preorder(root){
if (root){
console.log(root.val)
preorder(root.left)
preorder(root.right)
}
}

/**
* @order left -> root -> right
* @param {TreeNode} root
*/
function inorder(root){
if (root){
inorder(root.left)
console.log(root.val)
inorder(root.right)
}
}


/**
* @order left -> right -> root
* @param {TreeNode} root
*/
function postorder(root){
if (root){
postorder(root.left)
postorder(root.right)
console.log(root.val)
}
}

/**
* @example
*/
let myTree = new TreeNode(1)
myTree.left = new TreeNode(2)
myTree.right = new TreeNode(3)
myTree.left.left = new TreeNode(4)
myTree.left.right = new TreeNode(5)
myTree.right.left = new TreeNode(6)
myTree.right.right = new TreeNode(7)

/** Tree Structure
* 1
* | |
* 2 3
* | | | |
* 4 5 6 7
*/


preorder(myTree)
/**
* @output preorder
* 1 2 4 5 3 6 7
*/

inorder(myTree)
/**
* @output for inorder
* 4 2 5 1 6 3 7
*/

postorder(myTree)
/**
* @output for postorder
* 4 5 2 6 7 3 1
*/


/**
* @Complexity-analysis
* Time-complexity = O(n) // for visiting all nodes
* Space-complexity = O(n) // recursion stack space
*/
4 changes: 4 additions & 0 deletions JavaScript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Format: -[Program name](name of the file)

[Bubble Sort](bubble_sort.js)

[Binary Tree Recursive Traversals ( Inorder , Preorder , Postorder )](BinaryTreeTraversal.js)

[Fibonacci](fibonacci.js)

[Insertion Sort](insertion_sort.js)
Expand All @@ -23,6 +25,8 @@ Format: -[Program name](name of the file)

[Selection Sort](selection_sort.js)

[Stack Using Array](stack_using_array.js)

[Minimum Number of 1s](./min_number_of_1.js)

[Longest Substring Without Repeating Characters](longest_substring_without_repeating_characters.js)
Expand Down
74 changes: 74 additions & 0 deletions JavaScript/stack_using_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @description stack implementation using array
* @author prasanna kale
* @language Javascript
*/


/**
* @class Stack
*/
class Stack{

constructor() {
this.stack = []
this.top = -1
}

// push data onto the stack
push(data){
this.stack.push(data)
this.top++
}

// pop data from the top of stack
pop(){

// Underflow condition
if (this.top == -1) {
return -1
}
let data = this.stack.pop()
this.top--
return data
}

// check value on the top of stack
peek(){
if (this.top == -1) {
return -1
}
let data = this.stack[this.top]
return data
}
}


/**
* @example
*/

let myStack = new Stack()
myStack.push(1)
myStack.push(2)
myStack.push(3)

console.log(myStack.pop())
console.log(myStack.pop())
console.log(myStack.pop())
console.log(myStack.pop())

/**
* @Output
* 3
* 2
* 1
* -1 // underflow condition
*/


/**
* @Complexity analysis
* Time-complexity = O(1)
* Space-complexity = O(n)
*/