mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
Javascript/Math: editing file name
This commit is contained in:
44
Data-Structures/Graph/Graph.js
Normal file
44
Data-Structures/Graph/Graph.js
Normal file
@ -0,0 +1,44 @@
|
||||
class Graph {
|
||||
constructor () {
|
||||
this.adjacencyMap = {}
|
||||
}
|
||||
|
||||
addVertex (v) {
|
||||
this.adjacencyMap[v] = []
|
||||
}
|
||||
|
||||
containsVertex (vertex) {
|
||||
return typeof (this.adjacencyMap[vertex]) !== 'undefined'
|
||||
}
|
||||
|
||||
addEdge (v, w) {
|
||||
let result = false
|
||||
if (this.containsVertex(v) && this.containsVertex(w)) {
|
||||
this.adjacencyMap[v].push(w)
|
||||
this.adjacencyMap[w].push(v)
|
||||
result = true
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
printGraph () {
|
||||
const keys = Object.keys(this.adjacencyMap)
|
||||
for (const i of keys) {
|
||||
const values = this.adjacencyMap[i]
|
||||
let vertex = ''
|
||||
for (const j of values) { vertex += j + ' ' }
|
||||
console.log(i + ' -> ' + vertex)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const example = () => {
|
||||
const g = new Graph()
|
||||
g.addVertex(1)
|
||||
g.addVertex(2)
|
||||
g.addVertex(3)
|
||||
g.addEdge(1, 2)
|
||||
g.addEdge(1, 3)
|
||||
g.printGraph()
|
||||
}
|
||||
example()
|
125
Data-Structures/Heap/MinPriorityQueue.js
Normal file
125
Data-Structures/Heap/MinPriorityQueue.js
Normal file
@ -0,0 +1,125 @@
|
||||
|
||||
/* Minimum Priority Queue
|
||||
* It is a part of heap data structure
|
||||
* A heap is a specific tree based data structure
|
||||
* in which all the nodes of tree are in a specific order.
|
||||
* that is the children are arranged in some
|
||||
* respect of their parents, can either be greater
|
||||
* or less than the parent. This makes it a min priority queue
|
||||
* or max priority queue.
|
||||
*/
|
||||
|
||||
// Functions: insert, delete, peek, isEmpty, print, heapSort, sink
|
||||
|
||||
class MinPriorityQueue {
|
||||
// calls the constructor and initializes the capacity
|
||||
constructor (c) {
|
||||
this.heap = []
|
||||
this.capacity = c
|
||||
this.size = 0
|
||||
}
|
||||
|
||||
// inserts the key at the end and rearranges it
|
||||
// so that the binary heap is in appropriate order
|
||||
insert (key) {
|
||||
if (this.isFull()) return
|
||||
this.heap[this.size + 1] = key
|
||||
let k = this.size + 1
|
||||
while (k > 1) {
|
||||
if (this.heap[k] < this.heap[Math.floor(k / 2)]) {
|
||||
const temp = this.heap[k]
|
||||
this.heap[k] = this.heap[Math.floor(k / 2)]
|
||||
this.heap[Math.floor(k / 2)] = temp
|
||||
}
|
||||
k = Math.floor(k / 2)
|
||||
}
|
||||
this.size++
|
||||
}
|
||||
|
||||
// returns the highest priority value
|
||||
peek () {
|
||||
return this.heap[1]
|
||||
}
|
||||
|
||||
// returns boolean value whether the heap is empty or not
|
||||
isEmpty () {
|
||||
return this.size === 0
|
||||
}
|
||||
|
||||
// returns boolean value whether the heap is full or not
|
||||
isFull () {
|
||||
if (this.size === this.capacity) return true
|
||||
return false
|
||||
}
|
||||
|
||||
// prints the heap
|
||||
print () {
|
||||
console.log(this.heap.slice(1))
|
||||
}
|
||||
|
||||
// heap sorting can be done by performing
|
||||
// delete function to the number of times of the size of the heap
|
||||
// it returns reverse sort because it is a min priority queue
|
||||
heapSort () {
|
||||
for (let i = 1; i < this.capacity; i++) {
|
||||
this.delete()
|
||||
}
|
||||
}
|
||||
|
||||
// this function reorders the heap after every delete function
|
||||
sink () {
|
||||
let k = 1
|
||||
while (2 * k <= this.size || 2 * k + 1 <= this.size) {
|
||||
let minIndex
|
||||
if (this.heap[2 * k] >= this.heap[k]) {
|
||||
if (2 * k + 1 <= this.size && this.heap[2 * k + 1] >= this.heap[k]) {
|
||||
break
|
||||
} else if (2 * k + 1 > this.size) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if (2 * k + 1 > this.size) {
|
||||
minIndex = this.heap[2 * k] < this.heap[k] ? 2 * k : k
|
||||
} else {
|
||||
if (
|
||||
this.heap[k] > this.heap[2 * k] ||
|
||||
this.heap[k] > this.heap[2 * k + 1]
|
||||
) {
|
||||
minIndex =
|
||||
this.heap[2 * k] < this.heap[2 * k + 1] ? 2 * k : 2 * k + 1
|
||||
} else {
|
||||
minIndex = k
|
||||
}
|
||||
}
|
||||
const temp = this.heap[k]
|
||||
this.heap[k] = this.heap[minIndex]
|
||||
this.heap[minIndex] = temp
|
||||
k = minIndex
|
||||
}
|
||||
}
|
||||
|
||||
// deletes the highest priority value from the heap
|
||||
delete () {
|
||||
const min = this.heap[1]
|
||||
this.heap[1] = this.heap[this.size]
|
||||
this.heap[this.size] = min
|
||||
this.size--
|
||||
this.sink()
|
||||
return min
|
||||
}
|
||||
}
|
||||
|
||||
// testing
|
||||
const q = new MinPriorityQueue(8)
|
||||
|
||||
q.insert(5)
|
||||
q.insert(2)
|
||||
q.insert(4)
|
||||
q.insert(1)
|
||||
q.insert(7)
|
||||
q.insert(6)
|
||||
q.insert(3)
|
||||
q.insert(8)
|
||||
q.print() // [ 1, 2, 3, 5, 7, 6, 4, 8 ]
|
||||
q.heapSort()
|
||||
q.print() // [ 8, 7, 6, 5, 4, 3, 2, 1 ]
|
202
Data-Structures/Linked List/DoublyLinkedList.js
Normal file
202
Data-Structures/Linked List/DoublyLinkedList.js
Normal file
@ -0,0 +1,202 @@
|
||||
// Hamza chabchoub contribution for a university project
|
||||
function DoubleLinkedList () {
|
||||
const Node = function (element) {
|
||||
this.element = element
|
||||
this.next = null
|
||||
this.prev = null
|
||||
}
|
||||
|
||||
let length = 0
|
||||
let head = null
|
||||
let tail = null
|
||||
|
||||
// Add new element
|
||||
this.append = function (element) {
|
||||
const node = new Node(element)
|
||||
|
||||
if (!head) {
|
||||
head = node
|
||||
tail = node
|
||||
} else {
|
||||
node.prev = tail
|
||||
tail.next = node
|
||||
tail = node
|
||||
}
|
||||
|
||||
length++
|
||||
}
|
||||
|
||||
// Add element
|
||||
this.insert = function (position, element) {
|
||||
// Check of out-of-bound values
|
||||
if (position >= 0 && position <= length) {
|
||||
const node = new Node(element)
|
||||
let current = head
|
||||
let previous = 0
|
||||
let index = 0
|
||||
|
||||
if (position === 0) {
|
||||
if (!head) {
|
||||
head = node
|
||||
tail = node
|
||||
} else {
|
||||
node.next = current
|
||||
current.prev = node
|
||||
head = node
|
||||
}
|
||||
} else if (position === length) {
|
||||
current = tail
|
||||
current.next = node
|
||||
node.prev = current
|
||||
tail = node
|
||||
} else {
|
||||
while (index++ < position) {
|
||||
previous = current
|
||||
current = current.next
|
||||
}
|
||||
|
||||
node.next = current
|
||||
previous.next = node
|
||||
|
||||
// New
|
||||
current.prev = node
|
||||
node.prev = previous
|
||||
}
|
||||
|
||||
length++
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Remove element at any position
|
||||
this.removeAt = function (position) {
|
||||
// look for out-of-bounds value
|
||||
if (position > -1 && position < length) {
|
||||
let current = head
|
||||
let previous = 0
|
||||
let index = 0
|
||||
|
||||
// Removing first item
|
||||
if (position === 0) {
|
||||
head = current.next
|
||||
|
||||
// if there is only one item, update tail //NEW
|
||||
if (length === 1) {
|
||||
tail = null
|
||||
} else {
|
||||
head.prev = null
|
||||
}
|
||||
} else if (position === length - 1) {
|
||||
current = tail
|
||||
tail = current.prev
|
||||
tail.next = null
|
||||
} else {
|
||||
while (index++ < position) {
|
||||
previous = current
|
||||
current = current.next
|
||||
}
|
||||
|
||||
// link previous with current's next - skip it
|
||||
previous.next = current.next
|
||||
current.next.prev = previous
|
||||
}
|
||||
|
||||
length--
|
||||
return current.element
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// Get the indexOf item
|
||||
this.indexOf = function (elm) {
|
||||
let current = head
|
||||
let index = -1
|
||||
|
||||
// If element found then return its position
|
||||
while (current) {
|
||||
if (elm === current.element) {
|
||||
return ++index
|
||||
}
|
||||
|
||||
index++
|
||||
current = current.next
|
||||
}
|
||||
|
||||
// Else return -1
|
||||
return -1
|
||||
}
|
||||
|
||||
// Find the item in the list
|
||||
this.isPresent = (elm) => {
|
||||
return this.indexOf(elm) !== -1
|
||||
}
|
||||
|
||||
// Delete an item from the list
|
||||
this.delete = (elm) => {
|
||||
return this.removeAt(this.indexOf(elm))
|
||||
}
|
||||
|
||||
// Delete first item from the list
|
||||
this.deleteHead = function () {
|
||||
this.removeAt(0)
|
||||
}
|
||||
|
||||
// Delete last item from the list
|
||||
this.deleteTail = function () {
|
||||
this.removeAt(length - 1)
|
||||
}
|
||||
|
||||
// Print item of the string
|
||||
this.toString = function () {
|
||||
let current = head
|
||||
let string = ''
|
||||
|
||||
while (current) {
|
||||
string += current.element + (current.next ? '\n' : '')
|
||||
current = current.next
|
||||
}
|
||||
|
||||
return string
|
||||
}
|
||||
|
||||
// Convert list to array
|
||||
this.toArray = function () {
|
||||
const arr = []
|
||||
let current = head
|
||||
|
||||
while (current) {
|
||||
arr.push(current.element)
|
||||
current = current.next
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
// Check if list is empty
|
||||
this.isEmpty = function () {
|
||||
return length === 0
|
||||
}
|
||||
|
||||
// Get the size of the list
|
||||
this.size = function () {
|
||||
return length
|
||||
}
|
||||
|
||||
// Get the head
|
||||
this.getHead = function () {
|
||||
return head
|
||||
}
|
||||
|
||||
// Get the tail
|
||||
this.getTail = function () {
|
||||
return tail
|
||||
}
|
||||
}
|
||||
|
||||
const newDoubleLinkedList = new DoubleLinkedList()
|
||||
newDoubleLinkedList.append(1)
|
||||
newDoubleLinkedList.append(2)
|
||||
console.log('Testing: ' + newDoubleLinkedList.size()) // returns 2
|
209
Data-Structures/Linked List/singlylinklist.js
Normal file
209
Data-Structures/Linked List/singlylinklist.js
Normal file
@ -0,0 +1,209 @@
|
||||
/* SinglyLinkedList!!
|
||||
* A linked list is implar to an array, it hold values.
|
||||
* However, links in a linked list do not have indexes. With
|
||||
* a linked list you do not need to predetermine it's size as
|
||||
* it grows and shrinks as it is edited. This is an example of
|
||||
* a singly linked list.
|
||||
*/
|
||||
|
||||
// Functions - add, remove, indexOf, elementAt, addAt, removeAt, view
|
||||
|
||||
// class LinkedList and constructor
|
||||
// Creates a LinkedList
|
||||
var LinkedList = (function () {
|
||||
function LinkedList () {
|
||||
// Length of linklist and head is null at start
|
||||
this.length = 0
|
||||
this.head = null
|
||||
}
|
||||
|
||||
// class node (constructor)
|
||||
// Creating Node with element's value
|
||||
var Node = (function () {
|
||||
function Node (element) {
|
||||
this.element = element
|
||||
this.next = null
|
||||
}
|
||||
return Node
|
||||
}())
|
||||
|
||||
// Returns length
|
||||
LinkedList.prototype.size = function () {
|
||||
return this.length
|
||||
}
|
||||
|
||||
// Returns the head
|
||||
LinkedList.prototype.head = function () {
|
||||
return this.head
|
||||
}
|
||||
|
||||
// Creates a node and adds it to linklist
|
||||
LinkedList.prototype.add = function (element) {
|
||||
var node = new Node(element)
|
||||
// Check if its the first element
|
||||
if (this.head === null) {
|
||||
this.head = node
|
||||
} else {
|
||||
var currentNode = this.head
|
||||
|
||||
// Loop till there is node present in the list
|
||||
while (currentNode.next) {
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
|
||||
// Adding node to the end of the list
|
||||
currentNode.next = node
|
||||
}
|
||||
// Increment the length
|
||||
this.length++
|
||||
}
|
||||
|
||||
// Removes the node with the value as param
|
||||
LinkedList.prototype.remove = function (element) {
|
||||
var currentNode = this.head
|
||||
var previousNode
|
||||
|
||||
// Check if the head node is the element to remove
|
||||
if (currentNode.element === element) {
|
||||
this.head = currentNode.next
|
||||
} else {
|
||||
// Check which node is the node to remove
|
||||
while (currentNode.element !== element) {
|
||||
previousNode = currentNode
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
|
||||
// Removing the currentNode
|
||||
previousNode.next = currentNode.next
|
||||
}
|
||||
|
||||
// Decrementing the length
|
||||
this.length--
|
||||
}
|
||||
|
||||
// Return if the list is empty
|
||||
LinkedList.prototype.isEmpty = function () {
|
||||
return this.length === 0
|
||||
}
|
||||
|
||||
// Returns the index of the element passed as param otherwise -1
|
||||
LinkedList.prototype.indexOf = function (element) {
|
||||
var currentNode = this.head
|
||||
var index = -1
|
||||
|
||||
while (currentNode) {
|
||||
index++
|
||||
|
||||
// Checking if the node is the element we are searching for
|
||||
if (currentNode.element === element) {
|
||||
return index + 1
|
||||
}
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
// Returns the element at an index
|
||||
LinkedList.prototype.elementAt = function (index) {
|
||||
var currentNode = this.head
|
||||
var count = 0
|
||||
while (count < index) {
|
||||
count++
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
return currentNode.element
|
||||
}
|
||||
|
||||
// Adds the element at specified index
|
||||
LinkedList.prototype.addAt = function (index, element) {
|
||||
index--
|
||||
var node = new Node(element)
|
||||
|
||||
var currentNode = this.head
|
||||
var previousNode
|
||||
var currentIndex = 0
|
||||
|
||||
// Check if index is out of bounds of list
|
||||
if (index > this.length) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if index is the start of list
|
||||
if (index === 0) {
|
||||
node.next = currentNode
|
||||
this.head = node
|
||||
} else {
|
||||
while (currentIndex < index) {
|
||||
currentIndex++
|
||||
previousNode = currentNode
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
|
||||
// Adding the node at specified index
|
||||
node.next = currentNode
|
||||
previousNode.next = node
|
||||
}
|
||||
|
||||
// Incrementing the length
|
||||
this.length++
|
||||
return true
|
||||
}
|
||||
|
||||
// Removes the node at specified index
|
||||
LinkedList.prototype.removeAt = function (index) {
|
||||
index--
|
||||
var currentNode = this.head
|
||||
var previousNode
|
||||
var currentIndex = 0
|
||||
|
||||
// Check if index is present in list
|
||||
if (index < 0 || index >= this.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Check if element is the first element
|
||||
if (index === 0) {
|
||||
this.head = currentNode.next
|
||||
} else {
|
||||
while (currentIndex < index) {
|
||||
currentIndex++
|
||||
previousNode = currentNode
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
previousNode.next = currentNode.next
|
||||
}
|
||||
|
||||
// Decrementing the length
|
||||
this.length--
|
||||
return currentNode.element
|
||||
}
|
||||
|
||||
// Function to view the LinkedList
|
||||
LinkedList.prototype.view = function () {
|
||||
var currentNode = this.head
|
||||
var count = 0
|
||||
while (count < this.length) {
|
||||
count++
|
||||
console.log(currentNode.element)
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
}
|
||||
|
||||
// returns the constructor
|
||||
return LinkedList
|
||||
}())
|
||||
|
||||
// Implementation of LinkedList
|
||||
var linklist = new LinkedList()
|
||||
linklist.add(2)
|
||||
linklist.add(5)
|
||||
linklist.add(8)
|
||||
linklist.add(12)
|
||||
linklist.add(17)
|
||||
console.log(linklist.size())
|
||||
console.log(linklist.removeAt(4))
|
||||
linklist.addAt(4, 15)
|
||||
console.log(linklist.indexOf(8))
|
||||
console.log(linklist.size())
|
||||
linklist.view()
|
80
Data-Structures/Queue/Queue.js
Normal file
80
Data-Structures/Queue/Queue.js
Normal file
@ -0,0 +1,80 @@
|
||||
/* Queue
|
||||
* A Queue is a data structure that allows you to add an element to the end of
|
||||
* a list and remove the item at the front. A queue follows a "First In First Out"
|
||||
* system, where the first item to enter the queue is the first to be removed. This
|
||||
* implementation uses an array to store the queue.
|
||||
*/
|
||||
|
||||
// Functions: enqueue, dequeue, peek, view, length
|
||||
|
||||
var Queue = (function () {
|
||||
// constructor
|
||||
function Queue () {
|
||||
// This is the array representation of the queue
|
||||
this.queue = []
|
||||
}
|
||||
|
||||
// methods
|
||||
// Add a value to the end of the queue
|
||||
Queue.prototype.enqueue = function (item) {
|
||||
this.queue[this.queue.length] = item
|
||||
}
|
||||
|
||||
// Removes the value at the front of the queue
|
||||
Queue.prototype.dequeue = function () {
|
||||
if (this.queue.length === 0) {
|
||||
throw new Error('Queue is Empty')
|
||||
}
|
||||
|
||||
var result = this.queue[0]
|
||||
this.queue.splice(0, 1) // remove the item at position 0 from the array
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Return the length of the queue
|
||||
Queue.prototype.length = function () {
|
||||
return this.queue.length
|
||||
}
|
||||
|
||||
// Return the item at the front of the queue
|
||||
Queue.prototype.peek = function () {
|
||||
return this.queue[0]
|
||||
}
|
||||
|
||||
// List all the items in the queue
|
||||
Queue.prototype.view = function () {
|
||||
console.log(this.queue)
|
||||
}
|
||||
|
||||
return Queue
|
||||
}())
|
||||
|
||||
// Implementation
|
||||
var myQueue = new Queue()
|
||||
|
||||
myQueue.enqueue(1)
|
||||
myQueue.enqueue(5)
|
||||
myQueue.enqueue(76)
|
||||
myQueue.enqueue(69)
|
||||
myQueue.enqueue(32)
|
||||
myQueue.enqueue(54)
|
||||
|
||||
myQueue.view()
|
||||
|
||||
console.log('Length: ' + myQueue.length())
|
||||
console.log('Front item: ' + myQueue.peek())
|
||||
console.log('Removed ' + myQueue.dequeue() + ' from front.')
|
||||
console.log('New front item: ' + myQueue.peek())
|
||||
console.log('Removed ' + myQueue.dequeue() + ' from front.')
|
||||
console.log('New front item: ' + myQueue.peek())
|
||||
myQueue.enqueue(55)
|
||||
console.log('Inserted 55')
|
||||
console.log('New front item: ' + myQueue.peek())
|
||||
|
||||
for (var i = 0; i < 5; i++) {
|
||||
myQueue.dequeue()
|
||||
myQueue.view()
|
||||
}
|
||||
|
||||
// console.log(myQueue.dequeue()); // throws exception!
|
64
Data-Structures/Queue/QueueUsing2Stacks.js
Normal file
64
Data-Structures/Queue/QueueUsing2Stacks.js
Normal file
@ -0,0 +1,64 @@
|
||||
// implementation of Queue using 2 stacks
|
||||
// contribution made by hamza chabchoub for a university project
|
||||
|
||||
class Queue {
|
||||
constructor () {
|
||||
this.inputStack = []
|
||||
this.outputStack = []
|
||||
}
|
||||
|
||||
// Push item into the inputstack
|
||||
enqueue (item) {
|
||||
this.inputStack.push(item)
|
||||
}
|
||||
|
||||
dequeue (item) {
|
||||
// push all items to outputstack
|
||||
this.outputStack = []
|
||||
if (this.inputStack.length > 0) {
|
||||
while (this.inputStack.length > 0) {
|
||||
this.outputStack.push(this.inputStack.pop())
|
||||
}
|
||||
}
|
||||
// display the top element of the outputstack
|
||||
if (this.outputStack.length > 0) {
|
||||
console.log(this.outputStack.pop())
|
||||
// repush all the items to the inputstack
|
||||
this.inputStack = []
|
||||
while (this.outputStack.length > 0) {
|
||||
this.inputStack.push(this.outputStack.pop())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// display elements of the inputstack
|
||||
listIn () {
|
||||
let i = 0
|
||||
while (i < this.inputStack.length) {
|
||||
console.log(this.inputStack[i])
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
// display element of the outputstack
|
||||
listOut () {
|
||||
let i = 0
|
||||
while (i < this.outputStack.length) {
|
||||
console.log(this.outputStack[i])
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// testing
|
||||
|
||||
const queue = new Queue()
|
||||
queue.enqueue(1)
|
||||
queue.enqueue(2)
|
||||
queue.enqueue(8)
|
||||
queue.enqueue(9)
|
||||
|
||||
console.log(queue.dequeue())
|
||||
// ans = 1
|
||||
console.log(queue.dequeue())
|
||||
// ans = 2
|
72
Data-Structures/Stack/Stack.js
Normal file
72
Data-Structures/Stack/Stack.js
Normal file
@ -0,0 +1,72 @@
|
||||
/* Stack!!
|
||||
* A stack is exactly what it sounds like. An element gets added to the top of
|
||||
* the stack and only the element on the top may be removed. This is an example
|
||||
* of an array implementation of a Stack. So an element can only be added/removed
|
||||
* from the end of the array.
|
||||
*/
|
||||
|
||||
// Functions: push, pop, peek, view, length
|
||||
|
||||
// Creates a stack constructor
|
||||
var Stack = (function () {
|
||||
function Stack () {
|
||||
// The top of the Stack
|
||||
this.top = 0
|
||||
// The array representation of the stack
|
||||
this.stack = []
|
||||
}
|
||||
|
||||
// Adds a value onto the end of the stack
|
||||
Stack.prototype.push = function (value) {
|
||||
this.stack[this.top] = value
|
||||
this.top++
|
||||
}
|
||||
|
||||
// Removes and returns the value at the end of the stack
|
||||
Stack.prototype.pop = function () {
|
||||
if (this.top === 0) {
|
||||
return 'Stack is Empty'
|
||||
}
|
||||
|
||||
this.top--
|
||||
var result = this.stack[this.top]
|
||||
delete this.stack[this.top]
|
||||
return result
|
||||
}
|
||||
|
||||
// Returns the size of the stack
|
||||
Stack.prototype.size = function () {
|
||||
return this.top
|
||||
}
|
||||
|
||||
// Returns the value at the end of the stack
|
||||
Stack.prototype.peek = function () {
|
||||
return this.stack[this.top - 1]
|
||||
}
|
||||
|
||||
// To see all the elements in the stack
|
||||
Stack.prototype.view = function () {
|
||||
for (var i = 0; i < this.top; i++) { console.log(this.stack[i]) }
|
||||
}
|
||||
|
||||
return Stack
|
||||
}())
|
||||
|
||||
// Implementation
|
||||
var myStack = new Stack()
|
||||
|
||||
myStack.push(1)
|
||||
myStack.push(5)
|
||||
myStack.push(76)
|
||||
myStack.push(69)
|
||||
myStack.push(32)
|
||||
myStack.push(54)
|
||||
console.log(myStack.size())
|
||||
console.log(myStack.peek())
|
||||
console.log(myStack.pop())
|
||||
console.log(myStack.peek())
|
||||
console.log(myStack.pop())
|
||||
console.log(myStack.peek())
|
||||
myStack.push(55)
|
||||
console.log(myStack.peek())
|
||||
myStack.view()
|
114
Data-Structures/Tree/Binary Search Tree.js
Normal file
114
Data-Structures/Tree/Binary Search Tree.js
Normal file
@ -0,0 +1,114 @@
|
||||
/* Binary Search Tree!!
|
||||
*
|
||||
* Nodes that will go on the Binary Tree.
|
||||
* They consist of the data in them, the node to the left, the node
|
||||
* to the right, and the parent from which they came from.
|
||||
*
|
||||
* A binary tree is a data structure in which an element
|
||||
* has two successors(children). The left child is usually
|
||||
* smaller than the parent, and the right child is usually
|
||||
* bigger.
|
||||
*/
|
||||
|
||||
// class Node
|
||||
var Node = (function () {
|
||||
// Node in the tree
|
||||
function Node (val) {
|
||||
this.value = val
|
||||
this.left = null
|
||||
this.right = null
|
||||
}
|
||||
|
||||
// Search the tree for a value
|
||||
Node.prototype.search = function (val) {
|
||||
if (this.value === val) {
|
||||
return this
|
||||
} else if (val < this.value && this.left != null) {
|
||||
return this.left.search(val)
|
||||
} else if (val > this.value && this.right != null) {
|
||||
return this.right.search(val)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// Visit a node
|
||||
Node.prototype.visit = function () {
|
||||
// Recursively go left
|
||||
if (this.left != null) {
|
||||
this.left.visit()
|
||||
}
|
||||
// Print out value
|
||||
console.log(this.value)
|
||||
// Recursively go right
|
||||
if (this.right != null) {
|
||||
this.right.visit()
|
||||
}
|
||||
}
|
||||
|
||||
// Add a node
|
||||
Node.prototype.addNode = function (n) {
|
||||
if (n.value < this.value) {
|
||||
if (this.left == null) {
|
||||
this.left = n
|
||||
} else {
|
||||
this.left.addNode(n)
|
||||
}
|
||||
} else if (n.value > this.value) {
|
||||
if (this.right == null) {
|
||||
this.right = n
|
||||
} else {
|
||||
this.right.addNode(n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// returns the constructor
|
||||
return Node
|
||||
}())
|
||||
|
||||
// class Tree
|
||||
var Tree = (function () {
|
||||
function Tree () {
|
||||
// Just store the root
|
||||
this.root = null
|
||||
};
|
||||
|
||||
// Inorder traversal
|
||||
Tree.prototype.traverse = function () {
|
||||
this.root.visit()
|
||||
}
|
||||
|
||||
// Start by searching the root
|
||||
Tree.prototype.search = function (val) {
|
||||
const found = this.root.search(val)
|
||||
if (found === null) {
|
||||
console.log(val + ' not found')
|
||||
} else {
|
||||
console.log('Found:' + found.value)
|
||||
}
|
||||
}
|
||||
|
||||
// Add a new value to the tree
|
||||
Tree.prototype.addValue = function (val) {
|
||||
const n = new Node(val)
|
||||
if (this.root == null) {
|
||||
this.root = n
|
||||
} else {
|
||||
this.root.addNode(n)
|
||||
}
|
||||
}
|
||||
|
||||
// returns the constructor
|
||||
return Tree
|
||||
}())
|
||||
|
||||
// Implementation of BST
|
||||
var bst = new Tree()
|
||||
bst.addValue(6)
|
||||
bst.addValue(3)
|
||||
bst.addValue(9)
|
||||
bst.addValue(2)
|
||||
bst.addValue(8)
|
||||
bst.addValue(4)
|
||||
bst.traverse()
|
||||
bst.search(8)
|
Reference in New Issue
Block a user