├── ds.js ├── qwer.txt ├── remove.trie.js ├── package.json ├── select.js ├── insert.js ├── quick.js ├── mzeequick.js ├── mergsort.js ├── heap.js ├── mzeemerge.js ├── stackArr.js ├── bst-validate.js ├── mindlt.js ├── maxdelete.js ├── minheap.js ├── queArr.js ├── dfs.js ├── qlink.js ├── bfs.js ├── arrToheapSort.js ├── Hchain.js ├── bst.pre.js ├── bst-dept.js ├── trie.js ├── sample.js ├── linked.js ├── bfs └── bst.insrt.js /ds.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /qwer.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /remove.trie.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "body-parser": "^1.20.2", 4 | "express": "^4.18.2" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /select.js: -------------------------------------------------------------------------------- 1 | const a=[7,4,10,8,3,1] 2 | const n=a.length 3 | 4 | for(i=0;i=0 && arr[j]>temp) { 6 | arr[j+1]=arr[j] 7 | j-- 8 | } 9 | arr[j+1]=temp 10 | } 11 | return arr 12 | } 13 | 14 | const arr=[3,5,1,5,9,10] 15 | const b=insert(arr) 16 | console.log(b); -------------------------------------------------------------------------------- /quick.js: -------------------------------------------------------------------------------- 1 | function quick(arr) { 2 | if(arr.length<2){ 3 | return arr 4 | } 5 | let pivot=arr[arr.length-1] 6 | let left =[] 7 | let right=[] 8 | for(i=0;ipivot){ 10 | right.push(arr[i]) 11 | }else{ 12 | left.push(arr[i]) 13 | } 14 | } 15 | return [...quicksort(left),pivot,...quicksort(right)] 16 | } 17 | console.log(quicksort([1,2,4,5,6,8,3])) -------------------------------------------------------------------------------- /mergsort.js: -------------------------------------------------------------------------------- 1 | function mergeSort(arr) { 2 | if (arr.length <= 1) { 3 | return arr; 4 | } 5 | 6 | const middle = Math.floor(arr.length / 2); 7 | const left = arr.slice(0, middle); 8 | const right = arr.slice(middle); 9 | 10 | return merge(mergeSort(left), mergeSort(right)); 11 | } 12 | 13 | function merge(left, right) { 14 | const result = []; 15 | 16 | while (left.length && right.length) { 17 | if (left[0] <= right[0]) { 18 | result.push(left.shift()); 19 | } else { 20 | result.push(right.shift()); 21 | } 22 | } 23 | 24 | return result.concat(left, right); 25 | } 26 | 27 | 28 | const arr=[3,5,8,1,2,8,0,5,3] 29 | const b=mergeSort(arr) 30 | console.log(b); -------------------------------------------------------------------------------- /heap.js: -------------------------------------------------------------------------------- 1 | function insertMaxHeap(heap, value) { 2 | // Add the new element to the end of the heap 3 | heap.push(value); 4 | let currentIndex = heap.length - 1; 5 | let parentIndex = Math.floor((currentIndex - 1) / 2); 6 | 7 | // Compare the value of the new element with its parent node and swap if necessary 8 | while (currentIndex > 0 && heap[currentIndex] > heap[parentIndex]) { 9 | [heap[currentIndex], heap[parentIndex]] = [heap[parentIndex], heap[currentIndex]]; 10 | currentIndex = parentIndex; 11 | parentIndex = Math.floor((currentIndex - 1) / 2); // update the current index 12 | } 13 | return heap; 14 | } 15 | let heap = [10, 8,9,7, 6, 3, 4]; 16 | console.log(insertMaxHeap(heap,9)); // Output: [10, 9, 7, 6, 8, 4, 3] 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /mzeemerge.js: -------------------------------------------------------------------------------- 1 | function mergeSort(arr){ 2 | if(arr.length<=1){ 3 | return arr 4 | } 5 | let mid=Math.floor((arr.length)/2) 6 | let left=arr.slice(0,mid) 7 | let right=arr.slice(mid) 8 | return hello(mergeSort(left),mergeSort(right)) 9 | } 10 | function hello(first,second) 11 | { 12 | let i=0 13 | let j=0 14 | let k=0 15 | let temp=[] 16 | while(isecond[j]) { 18 | temp[k]=second[j] 19 | j++ 20 | }else{ 21 | temp[k]=first[i] 22 | i++ 23 | } 24 | k++ 25 | 26 | } 27 | while(i= max) { 17 | return false; 18 | } 19 | 20 | return ( 21 | isBSTHelper(node.left, min, node.value) && 22 | isBSTHelper(node.right, node.value, max) 23 | ); 24 | } 25 | 26 | return isBSTHelper(root, -Infinity, Infinity); 27 | } 28 | 29 | // Example usage: 30 | const root = new Node(4); 31 | root.left = new Node(2); 32 | root.right = new Node(5); 33 | root.left.left = new Node(1); 34 | root.left.right = new Node(3); 35 | 36 | console.log(isBST(root)); // Output: true 37 | 38 | // Add an invalid node to make the tree invalid 39 | root.right.left = new Node(3); 40 | console.log(isBST(root)); // Output: false 41 | -------------------------------------------------------------------------------- /mindlt.js: -------------------------------------------------------------------------------- 1 | function minHeap(a) { 2 | var size = a.length; 3 | for (var i = Math.floor(size / 2) - 1; i >= 0; i--) 4 | heapify(a, size, i); 5 | } 6 | function heapify(a, size, i) { 7 | var smallest = i; 8 | var l = 2 * i + 1; 9 | var r = 2 * i + 2; 10 | if (l < size && a[l] < a[smallest]) 11 | smallest = l; 12 | if (r < size && a[r] < a[smallest]) 13 | smallest = r; 14 | if (smallest != i) { 15 | var swap = a[i]; 16 | a[i] = a[smallest]; 17 | a[smallest] = swap; 18 | heapify(a, size, smallest); 19 | } 20 | } 21 | function printArray(a) { 22 | var size = a.length; 23 | for (var i = 0; i < size; i++) 24 | console.log(a[i]) 25 | } 26 | function deleteMin(a) { 27 | if (a.length < 1) return null; // min-heap is empty 28 | var min = a[0]; 29 | a[0] = a[a.length - 1]; 30 | a.pop(); 31 | heapify(a, a.length, 0); 32 | return min; 33 | } 34 | var a = [12, 11, 81, 5]; minHeap(a); deleteMin(a); 35 | printArray(a); //-RESULT-- prints deletion: 11 12 81 36 | -------------------------------------------------------------------------------- /maxdelete.js: -------------------------------------------------------------------------------- 1 | function deleteMaxHeap(heap) { 2 | // Remove the root element from the heap 3 | heap[0] = heap[heap.length - 1]; 4 | heap.pop(); 5 | let currentIndex = 0; 6 | 7 | // Compare the value of the root element with its child nodes and swap if necessary 8 | while (true) { 9 | let leftChildIndex = 2 * currentIndex + 1; 10 | let rightChildIndex = 2 * currentIndex + 2; 11 | let maxChildIndex = currentIndex; 12 | if (leftChildIndex < heap.length && heap[leftChildIndex] > heap[maxChildIndex]) { 13 | maxChildIndex = leftChildIndex; 14 | } 15 | if (rightChildIndex < heap.length && heap[rightChildIndex] > heap[maxChildIndex]) { 16 | maxChildIndex = rightChildIndex; 17 | } 18 | if (maxChildIndex !== currentIndex) { 19 | [heap[currentIndex], heap[maxChildIndex]] = [heap[maxChildIndex], heap[currentIndex]]; 20 | currentIndex = maxChildIndex; 21 | } else { 22 | break; 23 | } 24 | } 25 | return heap; 26 | } 27 | let heap = [10, 9, 7, 6, 8, 4, 3]; 28 | console.log(deleteMaxHeap(heap)); // Output: [9, 8, 7, 6, 3, 4] 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /minheap.js: -------------------------------------------------------------------------------- 1 | function minHeap(a) { 2 | var size = a.length; 3 | for (var i = Math.floor(size / 2) - 1; i >= 0; i--) 4 | heapify(a, size, i) 5 | } 6 | function heapify(a, size, i) { 7 | var smallest = i; 8 | var l = 2 * i + 1; 9 | var r = 2 * i + 2; 10 | if (l < size && a[l] < a[smallest]) 11 | smallest = l; 12 | if (r < size && a[r] < a[smallest]) 13 | smallest = r; 14 | if (smallest != i) { 15 | var swap = a[i]; 16 | a[i] = a[smallest]; 17 | a[smallest] = swap; 18 | heapify(a, size, smallest); 19 | } 20 | } 21 | function printArray(a) { 22 | var size = a.length; 23 | for (var i = 0; i < size; i++) 24 | console.log(a[i]) 25 | } 26 | function insertToMinHeap(value) { 27 | a.push(value); 28 | let currentIndex = a.length - 1; 29 | let parentIndex = Math.floor((currentIndex - 1) / 2); 30 | while (currentIndex > 0 && a[currentIndex] < a[parentIndex]) { 31 | var temp = a[currentIndex]; 32 | a[currentIndex] = a[parentIndex] 33 | a[parentIndex] = temp; 34 | currentIndex = parentIndex; 35 | parentIndex = Math.floor((currentIndex - 1) / 2); 36 | } 37 | } 38 | var a = [12, 11, 81, 5]; 39 | minHeap(a); 40 | insertToMinHeap(10) 41 | printArray(a); -------------------------------------------------------------------------------- /queArr.js: -------------------------------------------------------------------------------- 1 | class Queue { 2 | constructor(maxSize) { 3 | this.queue = new Array(maxSize); 4 | this.front = -1; 5 | this.rear = -1; 6 | } 7 | 8 | enqueue(item) { 9 | if (this.rear === this.queue.length - 1) { 10 | console.log("Queue is full"); 11 | return; 12 | } 13 | 14 | this.rear++; 15 | this.queue[this.rear] = item; 16 | } 17 | 18 | dequeue() { 19 | if (this.front === this.rear) { 20 | console.log("Queue is empty"); 21 | return; 22 | } 23 | 24 | this.front++; 25 | return this.queue[this.front]; 26 | } 27 | 28 | isEmpty() { 29 | return this.front === this.rear; 30 | } 31 | 32 | isFull() { 33 | return this.rear === this.queue.length - 1; 34 | } 35 | } 36 | 37 | 38 | 39 | const myQueue = new Queue(5); 40 | 41 | myQueue.enqueue(10); 42 | myQueue.enqueue(20); 43 | myQueue.enqueue(30); 44 | 45 | console.log(myQueue.dequeue()); // Output: 10 46 | console.log(myQueue.dequeue()); // Output: 20 47 | console.log(myQueue.isEmpty()); // Output: false 48 | console.log(myQueue.isFull()); // Output: false 49 | 50 | myQueue.enqueue(40); 51 | myQueue.enqueue(50); 52 | myQueue.enqueue(60); // Output: Queue is full 53 | 54 | console.log(myQueue.dequeue()); // Output: 30 55 | console.log(myQueue.dequeue()); // Output: 40 56 | console.log(myQueue.dequeue()); // Output: 50 57 | console.log(myQueue.dequeue()); // Output: Queue is empty 58 | -------------------------------------------------------------------------------- /dfs.js: -------------------------------------------------------------------------------- 1 | class Graph { 2 | constructor() { 3 | this.vertices = new Map(); 4 | } 5 | addVertex(vertex) { 6 | this.vertices.set(vertex, new Set()); 7 | } 8 | addEdge(vertex1, vertex2) { 9 | this.vertices.get(vertex1).add(vertex2); 10 | this.vertices.get(vertex2).add(vertex1); 11 | } 12 | getEdges(vertex) { 13 | return this.vertices.get(vertex); 14 | } 15 | getVertices() { 16 | return [...this.vertices.keys()]; 17 | } 18 | 19 | dfs(startVertex){ 20 | let visited = new Set() 21 | this.dfshelper(startVertex,visited) 22 | } 23 | dfshelper(currentvisited,visited){ 24 | visited.add(currentvisited) 25 | console.log(currentvisited) 26 | for(let adjVertex of this.getEdges(currentvisited)){ 27 | if(!visited.has(adjVertex)){ 28 | this.dfshelper(adjVertex,visited) 29 | } 30 | } 31 | } 32 | } 33 | const myGraph = new Graph(); 34 | myGraph.addVertex('A'); 35 | myGraph.addVertex('B'); 36 | myGraph.addVertex('C'); 37 | myGraph.addVertex('D'); 38 | myGraph.addVertex('E'); 39 | myGraph.addEdge('A', 'B'); 40 | myGraph.addEdge('A', 'D'); 41 | myGraph.addEdge('B', 'C'); 42 | myGraph.addEdge('C', 'E'); 43 | console.log('Vertices:', myGraph.getVertices()); 44 | for (const vertex of myGraph.getVertices()) { 45 | console.log(`Edges for ${vertex}:`, [...myGraph.getEdges(vertex)]); 46 | } 47 | console.log('_____DFS_______') 48 | myGraph.dfs('A') -------------------------------------------------------------------------------- /qlink.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(value) { 3 | this.value = value; 4 | this.next = null; 5 | } 6 | } 7 | 8 | class Queue { 9 | constructor() { 10 | this.front = null; 11 | this.rear = null; 12 | } 13 | 14 | enqueue(item) { 15 | const newNode = new Node(item); 16 | 17 | if (this.rear === null) { 18 | this.front = newNode; 19 | this.rear = newNode; 20 | } else { 21 | this.rear.next = newNode; 22 | this.rear = newNode; 23 | } 24 | } 25 | 26 | dequeue() { 27 | if (this.front === null) { 28 | console.log("Queue is empty"); 29 | return; 30 | } 31 | 32 | const value = this.front.value; 33 | this.front = this.front.next; 34 | 35 | if (this.front === null) { 36 | this.rear = null; 37 | } 38 | 39 | return value; 40 | } 41 | 42 | isEmpty() { 43 | return this.front === null; 44 | } 45 | } 46 | 47 | 48 | 49 | const myQueue = new Queue(); 50 | 51 | myQueue.enqueue(10); 52 | myQueue.enqueue(20); 53 | myQueue.enqueue(30); 54 | 55 | console.log(myQueue.dequeue()); // Output: 10 56 | console.log(myQueue.dequeue()); // Output: 20 57 | console.log(myQueue.isEmpty()); // Output: false 58 | 59 | myQueue.enqueue(40); 60 | myQueue.enqueue(50); 61 | 62 | console.log(myQueue.dequeue()); // Output: 30 63 | console.log(myQueue.dequeue()); // Output: 40 64 | console.log(myQueue.dequeue()); // Output: 50 65 | console.log(myQueue.isEmpty()); // Output: true 66 | 67 | console.log(myQueue.dequeue()); // Output: Queue is empty 68 | -------------------------------------------------------------------------------- /bfs.js: -------------------------------------------------------------------------------- 1 | class Graph { 2 | constructor() { 3 | this.vertices = new Map(); 4 | } 5 | addVertex(vertex) { 6 | this.vertices.set(vertex, new Set()); 7 | } 8 | addEdge(vertex1, vertex2) { 9 | this.vertices.get(vertex1).add(vertex2); 10 | this.vertices.get(vertex2).add(vertex1); 11 | } 12 | getEdges(vertex) { 13 | return this.vertices.get(vertex); 14 | } 15 | getVertices() { 16 | return [...this.vertices.keys()]; 17 | } 18 | connection() { 19 | console.log("yeay"+[...this.vertices.keys()]) 20 | return [...this.vertices.keys()]; 21 | } 22 | 23 | bfs(startindex){ 24 | let visited = new Set() 25 | visited.add(startindex) 26 | let queue = [startindex] 27 | while(queue.length > 0){ 28 | let currentvisited = queue.shift() 29 | console.log(currentvisited) 30 | for(let adjVertex of this.getEdges(currentvisited)){ 31 | if(!visited.has(adjVertex)){ 32 | visited.add(adjVertex) 33 | queue.push(adjVertex) 34 | } } } } } 35 | const myGraph = new Graph(); 36 | myGraph.addVertex('A'); 37 | myGraph.addVertex('B'); 38 | myGraph.addVertex('D'); 39 | myGraph.addEdge('A', 'B'); 40 | myGraph.addEdge('A', 'D'); 41 | myGraph.addEdge('B', 'C'); 42 | myGraph.connection() 43 | console.log(myGraph) 44 | console.log('Vertices:', myGraph.getVertices()); 45 | for (const vertex of myGraph.getVertices()) { 46 | console.log(`Edges for ${vertex}:`, [...myGraph.getEdges(vertex)]); 47 | } 48 | console.log("___BSF____") 49 | myGraph.bfs('B') 50 | 51 | 52 | -------------------------------------------------------------------------------- /arrToheapSort.js: -------------------------------------------------------------------------------- 1 | function sort(array) { 2 | var size = array.length; 3 | 4 | // Build heap (rearrange array) 5 | for (var parent = Math.floor(size / 2) - 1; parent >= 0; parent--) 6 | heapify(array, size, parent); 7 | console.log(array) 8 | // One by one extract an element from heap 9 | for (var i = size - 1; i > 0; i--) { 10 | // Move current root to end 11 | var temp = a[0]; 12 | a[0] = a[i]; 13 | a[i] = temp; 14 | // call max heapify on the reduced heap 15 | heapify(a, i, 0); 16 | } 17 | } 18 | // To heapify a subtree rooted with node i which is 19 | // an index in arr[]. n is size of heap 20 | function heapify(a, size, i) { 21 | var largest = i; // Initialize largest as root 22 | var l = 2 * i + 1; // left = 2*i + 1 23 | var r = 2 * i + 2; // right = 2*i + 2 24 | 25 | // If left child is larger than root 26 | if (l < size && a[l] > a[largest]) 27 | largest = l; 28 | // If right child is larger than largest so far 29 | if (r < size && a[r] > a[largest]) 30 | largest = r; 31 | // If largest is not root 32 | if (largest != i) { 33 | var swap = a[i]; 34 | a[i] = a[largest]; 35 | a[largest] = swap; 36 | // Recursively heapify the affected sub-tree 37 | heapify(a, size, largest); 38 | } 39 | } 40 | function printArray(a) { 41 | var size = a.length; 42 | for (var i = 0; i < size; i++) 43 | console.log(a[i]) 44 | } 45 | //--RESULT--- 46 | var a = [12, 11, 81, 5, 56, 7,91]; 47 | var size = a.length; 48 | sort(a); 49 | console.log("Sorted array is"); 50 | printArray(a, size); 51 | 52 | -------------------------------------------------------------------------------- /Hchain.js: -------------------------------------------------------------------------------- 1 | class HashTable { 2 | constructor(size) { 3 | this.size = size; 4 | this.table = new Array(size); 5 | } 6 | 7 | // hash function using division method 8 | hash(key) { 9 | let total = 0 10 | for (let i = 0; i < key.length; i++) { 11 | total += key.charCodeAt(i) 12 | } 13 | return total % this.size 14 | } 15 | // set a key-value pair into the hash table 16 | set(key, value) { 17 | const index = this.hash(key); 18 | const bucket = this.table[index] 19 | if (!bucket) { 20 | this.table[index] = [[key, value]] 21 | } else { 22 | const sameKeyItem = bucket.find(item => item[0] === key) 23 | if (sameKeyItem) { 24 | sameKeyItem[1] = value 25 | } else { 26 | bucket.push([key, value]) 27 | 28 | } 29 | } 30 | } 31 | 32 | // retrieve the value associated with a key 33 | get(key) { 34 | const index = this.hash(key); 35 | const bucket = this.table[index] 36 | if (bucket) { 37 | const sameKeyItem = bucket.find(item => item[0] === key) 38 | if (sameKeyItem) { 39 | return sameKeyItem[1] 40 | 41 | } 42 | 43 | } 44 | return undefined; 45 | } 46 | 47 | // remove a key-value pair from the hash table 48 | remove(key) { 49 | const index = this.hash(key); 50 | const bucket = this.table[index] 51 | if (bucket) { 52 | const sameKeyItem = bucket.find(item => item[0] === key) 53 | if (sameKeyItem) { 54 | bucket.splice(bucket.indexof(sameKeyItem), 1) 55 | } 56 | } 57 | } 58 | 59 | display() { 60 | for (let i = 0; i < this.table.length; i++) { 61 | if (this.table[i]) { 62 | console.log(i, this.table[i]); 63 | 64 | } 65 | } 66 | } 67 | } 68 | const table = new HashTable(50) 69 | table.set("name", "yatheesh") 70 | table.set("age", 23) 71 | table.display() 72 | console.log(table.get("name")); 73 | 74 | table.set("mane", "clark") 75 | table.set("name", "yathu") 76 | table.remove() 77 | table.display() -------------------------------------------------------------------------------- /bst.pre.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(data) { 3 | this.data = data; 4 | this.left = null; 5 | this.right = null; 6 | } 7 | } 8 | 9 | class BinarySearchTree { 10 | constructor() { 11 | this.root = null; 12 | } 13 | 14 | // Insert a new node into the BST 15 | insert(data) { 16 | const newNode = new Node(data); 17 | 18 | if (this.root === null) { 19 | this.root = newNode; 20 | } else { 21 | this.insertNode(this.root, newNode); 22 | } 23 | } 24 | 25 | insertNode(node, newNode) { 26 | if (newNode.data < node.data) { 27 | if (node.left === null) { 28 | node.left = newNode; 29 | } else { 30 | this.insertNode(node.left, newNode); 31 | } 32 | } else { 33 | if (node.right === null) { 34 | node.right = newNode; 35 | } else { 36 | this.insertNode(node.right, newNode); 37 | } 38 | } 39 | } 40 | 41 | // Traverse the BST in pre-order 42 | preOrderTraversal(node = this.root) { 43 | if (node !== null) { 44 | console.log(node.data); 45 | this.preOrderTraversal(node.left); 46 | this.preOrderTraversal(node.right); 47 | } 48 | } 49 | 50 | // Traverse the BST in in-order 51 | inOrderTraversal(node = this.root) { 52 | if (node !== null) { 53 | this.inOrderTraversal(node.left); 54 | console.log(node.data); 55 | this.inOrderTraversal(node.right); 56 | } 57 | } 58 | 59 | // Traverse the BST in post-order 60 | postOrderTraversal(node = this.root) { 61 | if (node !== null) { 62 | this.postOrderTraversal(node.left); 63 | this.postOrderTraversal(node.right); 64 | console.log(node.data); 65 | } 66 | } 67 | } 68 | 69 | // Example usage: 70 | const bst = new BinarySearchTree(); 71 | bst.insert(4); 72 | bst.insert(2); 73 | bst.insert(6); 74 | bst.insert(1); 75 | bst.insert(3); 76 | bst.insert(5); 77 | bst.insert(7); 78 | 79 | console.log('Pre-order traversal:'); 80 | bst.preOrderTraversal(); 81 | 82 | console.log('In-order traversal:'); 83 | bst.inOrderTraversal(); 84 | 85 | console.log('Post-order traversal:'); 86 | bst.postOrderTraversal(); 87 | -------------------------------------------------------------------------------- /bst-dept.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(data) { 3 | this.data = data; 4 | this.left = null; 5 | this.right = null; 6 | } 7 | } 8 | 9 | class BinarySearchTree { 10 | constructor() { 11 | this.root = null; 12 | } 13 | 14 | insert(data) { 15 | const newNode = new Node(data); 16 | if (this.root === null) { 17 | this.root = newNode; 18 | } else { 19 | this.insertNode(this.root, newNode); 20 | } 21 | } 22 | 23 | insertNode(node, newNode) { 24 | if (newNode.data < node.data) { 25 | if (node.left === null) { 26 | node.left = newNode; 27 | } else { 28 | this.insertNode(node.left, newNode); 29 | } 30 | } else { 31 | if (node.right === null) { 32 | node.right = newNode; 33 | } else { 34 | this.insertNode(node.right, newNode); 35 | } 36 | } 37 | } 38 | 39 | getHeight() { 40 | return this.calculateHeight(this.root); 41 | } 42 | 43 | calculateHeight(node) { 44 | if (node === null) { 45 | return 0; 46 | } else { 47 | const leftHeight = this.calculateHeight(node.left); 48 | const rightHeight = this.calculateHeight(node.right); 49 | return Math.max(leftHeight, rightHeight) + 1; 50 | } 51 | } 52 | 53 | getDepth(node) { 54 | return this.calculateDepth(this.root, node); 55 | } 56 | 57 | calculateDepth(root, node) { 58 | if (root === null) { 59 | return 0; 60 | } else if (root === node) { 61 | return 1; 62 | } else { 63 | const leftDepth = this.calculateDepth(root.left, node); 64 | const rightDepth = this.calculateDepth(root.right, node); 65 | if (leftDepth !== 0) { 66 | return leftDepth + 1; 67 | } else if (rightDepth !== 0) { 68 | return rightDepth + 1; 69 | } else { 70 | return 0; 71 | } 72 | } 73 | } 74 | } 75 | 76 | const bst = new BinarySearchTree(); 77 | bst.insert(50); 78 | bst.insert(30); 79 | bst.insert(20); 80 | bst.insert(40); 81 | bst.insert(70); 82 | bst.insert(60); 83 | bst.insert(80); 84 | 85 | console.log("Height of BST:", bst.getHeight()); // Output: 3 86 | console.log("Depth of node with value 40:", bst.getDepth(bst.root.left.right)); // Output: 2 87 | -------------------------------------------------------------------------------- /trie.js: -------------------------------------------------------------------------------- 1 | class TrieNode { 2 | constructor() { 3 | this.children = {}; 4 | this.endOfNode = false; 5 | } 6 | } 7 | class Trie { 8 | constructor() { 9 | this.root = new TrieNode(); 10 | } 11 | insert(word) { 12 | let node = this.root; 13 | for (let i = 0; i < word.length; i++) { 14 | const char = word[i]; 15 | if (!node.children[char]) { 16 | node.children[char] = new TrieNode(); 17 | } 18 | node = node.children[char]; 19 | } 20 | node.endOfNode = true; 21 | } 22 | search(word) { 23 | let node = this.root; 24 | for (let i = 0; i < word.length; i++) { 25 | const char = word[i]; 26 | if (!node.children[char]) { 27 | return false; 28 | } 29 | node = node.children[char]; 30 | } 31 | return node.endOfNode; 32 | } 33 | 34 | delete(word) { 35 | let node = this.root; 36 | const stack = []; 37 | // Find the node corresponding to the last character in the word 38 | for (let i = 0; i < word.length; i++) { 39 | const char = word[i]; 40 | if (!node.children[char]) { 41 | return false; // Word not found in trie 42 | } 43 | stack.push({ node, char }); 44 | node = node.children[char]; 45 | } 46 | if (!node.endOfNode) { 47 | return false; // Word not found in trie 48 | } 49 | // Mark the node as non-terminal 50 | node.endOfNode = false; 51 | // Delete nodes that are no longer needed 52 | while (stack.length > 0) { 53 | const { node, char } = stack.pop(); 54 | if (Object.keys(node.children).length === 0 && !node.endOfNode) { 55 | delete node.children[char]; 56 | } else { 57 | break; 58 | } 59 | } 60 | return true; 61 | } 62 | 63 | display() { 64 | let words = []; 65 | function traverse(node, word) { 66 | if (node.endOfNode) { 67 | words.push(word); 68 | } 69 | for (let char in node.children) { 70 | traverse(node.children[char], word + char); 71 | } 72 | } 73 | traverse(this.root, ""); 74 | return words; 75 | } 76 | print() { 77 | console.log(this.root); 78 | } 79 | } 80 | let tri = new Trie(); 81 | tri.insert("yathish"); 82 | tri.insert("world"); 83 | 84 | console.log(tri.search("yathish")); // true 85 | 86 | console.log(tri.delete("yathish")); // true 87 | 88 | console.log(tri.display()); 89 | tri.print(); 90 | -------------------------------------------------------------------------------- /sample.js: -------------------------------------------------------------------------------- 1 | // function bubble(arr) { 2 | // for(i=0;iarr[j]) { 5 | // let temp=arr[i] 6 | // arr[i]=arr[j] 7 | // arr[j]=temp 8 | // } 9 | // } 10 | // } 11 | // return arr 12 | // } 13 | 14 | // const arr=[3,5,7,4,9,-2] 15 | // const b=bubble(arr) 16 | // console.log(b); 17 | 18 | 19 | 20 | 21 | // function insert(arr) { 22 | // for(i=1;i=0 && arr[j]>temp) { 26 | // arr[j+1]=arr[j] 27 | // j-- 28 | // } 29 | // arr[j+1]=temp 30 | // } 31 | // return arr 32 | // } 33 | 34 | // const arr=[3,5,7,4,9,-2] 35 | // const b=insert(arr) 36 | // console.log(b); 37 | 38 | 39 | 40 | 41 | // const a=[3,5,8,1,3,-5] 42 | 43 | // for(i=0;i{ 50 | // console.log(element) 51 | // }) 52 | // } 53 | // } 54 | // const hh=new hashtable() 55 | // hh.set("name","sreerag") 56 | // hh.set("name","toure") 57 | // //hh.get("leo") 58 | // hh.display() 59 | // console.log(hh.table) 60 | 61 | 62 | 63 | 64 | 65 | 66 | //quadratic probing in hashtable 67 | 68 | // class hashtable{ 69 | // constructor(){ 70 | // this.table=new Array(126) 71 | // this.size=0 72 | // } 73 | // _hash(key){ 74 | // var hash=0 75 | // for(var i=0;i{ 111 | // console.log(element) 112 | // }) 113 | // } 114 | // } 115 | // const hh=new hashtable() 116 | // hh.set("name","sreerag") 117 | // hh.set("name","toure") 118 | // // hh.get("leo") 119 | // hh.display() 120 | // console.log(hh.table) 121 | 122 | 123 | 124 | 125 | 126 | 127 | //double hashing in hash table 128 | 129 | // class hashtable{ 130 | // constructor(){ 131 | // this.table=new Array(100) 132 | // this.size=0 133 | // } 134 | // _hash(key){ 135 | // var hash=0 136 | // for(var i=0;i{ 178 | // console.log(element) 179 | // }) 180 | // } 181 | // } 182 | // const hh=new hashtable() 183 | // hh.set("name","sreerag") 184 | // hh.set("name","toure") 185 | // // hh.get("leo") 186 | // hh.display() 187 | // console.log(hh.table) -------------------------------------------------------------------------------- /bfs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | // function encryption(str,n){ 5 | // let newString ="" 6 | // for (let i=0;i= 65 && asciiCode <=90){ 9 | // newString += String.fromCharCode( ( ( asciiCode - 65 + n ) % 26 ) + 65 ) 10 | // }else if(asciiCode >= 97 && asciiCode <= 122 ){ 11 | // newString += String.fromCharCode(((asciiCode - 97 +n )% 26 ) + 97) 12 | // } 13 | // } 14 | // return newString 15 | // } 16 | // let he = "yatheesh" 17 | // let d = encryption(he , 2) 18 | // console.log(d) 19 | 20 | 21 | 22 | // function encryption(str, n) { 23 | // let newString = "" 24 | 25 | // for (i = 0; i < str.length; i++) { 26 | // let asciiCode = str.charCodeAt(i) 27 | // if (asciiCode => 65 && asciiCode <= 90) { 28 | // newString += String.fromCharCode(((asciiCode - 65 + n) % 26) + 65) 29 | // } else if (asciiCode => 97 && asciiCode <= 122) { 30 | // newString += String.fromCharCode(((asciiCode - 97 + n) % 26) + 97) 31 | // } 32 | // } 33 | // return newString 34 | 35 | // } 36 | 37 | // let he = "yathu" 38 | // let ff = encryption(he, 2) 39 | // console.log(ff); 40 | 41 | 42 | 43 | 44 | 45 | 46 | // function emcryption(str,n) { 47 | // let newstr="" 48 | 49 | // for(i=0;i65 && asciiCode<=90) { 52 | // newstr+=String.fromCharCode(((asciiCode-65+n)%26)+65) 53 | // }else if(asciiCode=>97 && asciiCode<=122){ 54 | // newstr+=String.fromCharCode(((asciiCode-97+n)%26)+97) 55 | // } 56 | // } 57 | // return newstr 58 | // } 59 | 60 | // let he="yatheesh" 61 | // let d=emcryption(he,4) 62 | // console.log(d); 63 | 64 | 65 | 66 | 67 | 68 | // let a="yatheesh" 69 | // let c="" 70 | // let b=c.a.split() 71 | // console.log(b); 72 | 73 | // for(i=0;i0) { 137 | // console.log(a.shift()) 138 | // show(a) 139 | 140 | // } 141 | 142 | // } 143 | // show(a) 144 | 145 | // function show(n) { 146 | // if(n<=0){ 147 | // return 0; 148 | // }else{ 149 | // return n+show(n-1) 150 | // } 151 | // } 152 | // console.log(show(20)); 153 | 154 | 155 | 156 | 157 | 158 | // function fact(n) { 159 | // if (n==0) { 160 | // return 1; 161 | // }else{ 162 | // return n*fact(n-1) 163 | // } 164 | // } 165 | 166 | // let n=3 167 | 168 | // console.log(fact(n)); 169 | 170 | 171 | 172 | // class linkedlist{ 173 | // constructor(data){ 174 | // this.head={ 175 | // value:data, 176 | // next:null 177 | // } 178 | // this.tail=this.head 179 | // this.length=1; 180 | // } 181 | // append(data){ 182 | // const newNode={ 183 | // value:data, 184 | // next:null 185 | // } 186 | // this.tail.next=newNode 187 | // this.tail=newNode 188 | // this.length++ 189 | // } 190 | // } 191 | // const mylist=new linkedlist(10) 192 | // mylist.append(18) 193 | // console.log(mylist); 194 | 195 | 196 | 197 | 198 | 199 | 200 | // class linkedlist{ 201 | // constructor(data){ 202 | // this.head={ 203 | // value:data, 204 | // next:null 205 | // } 206 | // this.tail=this.head 207 | // this.length=1; 208 | // } 209 | // append(data){ 210 | // let newNode={ 211 | // value:data, 212 | // next:null 213 | // } 214 | // this.tail.next=newNode 215 | // this.tail 216 | // } 217 | // } 218 | 219 | 220 | 221 | 222 | // class linkedlist{ 223 | // constructor(data){ 224 | // this.head={ 225 | // value:data, 226 | // next:null 227 | // } 228 | // this.tail=this.head 229 | // this.length=1; 230 | // } 231 | // append(data){ 232 | // let newNode={ 233 | // value:data, 234 | // next:null 235 | // } 236 | // this.tail.next=newNode 237 | // this.tail=newNode 238 | // this.length++ 239 | // } 240 | // prepend(data){ 241 | // const newNode={ 242 | // value:data, 243 | // next:null 244 | // } 245 | // newNode.next=this.head 246 | // this.head=newNode 247 | // } 248 | // } 249 | 250 | // const mylist=new linkedlist(10) 251 | // mylist.append(17) 252 | // mylist.prepend(20) 253 | 254 | // console.log(mylist); 255 | 256 | 257 | 258 | 259 | 260 | // class linkedlist{ 261 | // constructor(data){ 262 | // this.head={ 263 | // value:data, 264 | // next:null 265 | // } 266 | // this.tail=this.head 267 | // this.length=1; 268 | // } 269 | // append(data){ 270 | // const newNode={ 271 | // value:data, 272 | // next:null 273 | // } 274 | // this.tail.next=newNode 275 | // this.tail=newNode 276 | // this.length++ 277 | // } 278 | // prepend(data){ 279 | // const newNode={ 280 | // value:data, 281 | // next:null 282 | // } 283 | // newNode.next=this.head 284 | // this.head=newNode 285 | // } 286 | // treversing(req){ 287 | // let counter:0, 288 | // let currentNode:this.head 289 | 290 | // while(counter !=req){ 291 | // counter++ 292 | // let leaderNode=currentNode.next 293 | // } 294 | // } 295 | 296 | // } 297 | 298 | // const mylist=new linkedlist(10) 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | // // hash function using division method 311 | // hash(key) { 312 | // let total=0 313 | // for(let i=0;iitem[0]===key) 327 | // if (sameKeyItem) { 328 | // sameKeyItem[1]=value 329 | // } else { 330 | // bucket.push([key,value]) 331 | 332 | // } 333 | // } 334 | // } 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | // quadratic probing in hashtable 345 | 346 | class hashtable{ 347 | constructor(){ 348 | this.table=new Array(126) 349 | this.size=0 350 | } 351 | _hash(key){ 352 | var hash=0 353 | for(var i=0;i{ 389 | console.log(element) 390 | }) 391 | } 392 | } 393 | const hh=new hashtable() 394 | hh.set("name","sreerag") 395 | hh.set("name","toure") 396 | // hh.get("leo") 397 | hh.display() 398 | console.log(hh.table) 399 | -------------------------------------------------------------------------------- /bst.insrt.js: -------------------------------------------------------------------------------- 1 | // class Node { 2 | // constructor(data) { 3 | // this.value = data 4 | // this.left = null 5 | // this.right = null 6 | // } 7 | // } 8 | // class BST { 9 | // constructor() { 10 | // this.root = null 11 | // } 12 | // insert(value) { //INSERT NODE----------- 13 | // let newNode = new Node(value) 14 | // if (!this.root) { 15 | // this.root = newNode 16 | // } 17 | // else { 18 | // let node = this.root 19 | // while (true) { 20 | // if (value < node.value) { 21 | // if (!node.left) { 22 | // node.left = newNode 23 | // break; 24 | // } 25 | // node = node.left 26 | // } 27 | // else { 28 | // if (!node.right) { 29 | // node.right = newNode 30 | // break 31 | // } 32 | // node = node.right 33 | // }}}} 34 | 35 | 36 | // search(value) { 37 | // let current = this.root; 38 | // while (current !== null) { 39 | // if (value === current.value) { 40 | // return true; 41 | // } else if (value < current.value) { 42 | // current = current.left; 43 | // } else { 44 | // current = current.right; 45 | // } 46 | // } 47 | // return false; 48 | // } 49 | 50 | 51 | 52 | // //DELETE NODE-------------------------------------- 53 | 54 | // delete(value) { 55 | // const removeNode = (node, value) => { 56 | // if (node === null) { 57 | // return null; 58 | // } 59 | // if (value === node.value) { 60 | // if (node.left === null && node.right === null) { 61 | // return null; 62 | // } 63 | // if (node.left === null) { 64 | // return node.right; 65 | // } 66 | // if (node.right === null) { 67 | // return node.left; 68 | // } 69 | // let temp = node.right; 70 | // while (temp.left !== null) { 71 | // temp = temp.left; 72 | // } 73 | // node.value = temp.value; 74 | // node.right = removeNode(node.right, temp.value); 75 | // return node; 76 | // } else if (value < node.value) { 77 | // node.left = removeNode(node.left, value); 78 | // return node; 79 | // } else { 80 | // node.right = removeNode(node.right, value); 81 | // return node; 82 | // } 83 | // } 84 | // this.root = removeNode(this.root, value); 85 | // } 86 | 87 | 88 | // //BST-FINDclosest-------------------------------- 89 | // findClosest(value) { 90 | // let current = this.root; 91 | // let closest = current.value; 92 | // while (current !== null) { 93 | // if (current.value - value < closest - value) { 94 | // closest = current.value; 95 | // } 96 | // if (value === current.value) { 97 | // break; 98 | // } else if (value < current.value) { 99 | // current = current.left; 100 | // } else { 101 | // current = current.right; 102 | // } 103 | // } 104 | // return closest; 105 | // } 106 | // } 107 | // //-----RESULT------------------------------------------- 108 | // const bst = new BST() 109 | 110 | // bst.insert(12) 111 | // bst.insert(32) 112 | // bst.insert(43) 113 | // bst.insert(10) 114 | 115 | // bst.insert(5) 116 | // console.log(bst.search(5)); 117 | // console.log(bst.findClosest(4)) 118 | 119 | // bst.delete(32) 120 | // console.log(bst.root); 121 | 122 | 123 | 124 | 125 | 126 | 127 | // class Node { 128 | // constructor(data) { 129 | // this.value = data; 130 | // this.left = null; 131 | // this.right = null; 132 | // } 133 | // } 134 | 135 | // class BST { 136 | // constructor() { 137 | // this.root = null; 138 | // } 139 | 140 | // // INSERT NODE 141 | // insert(value) { 142 | // let newNode = new Node(value); 143 | // if (!this.root) { 144 | // this.root = newNode; 145 | // } else { 146 | // let node = this.root; 147 | // while (true) { 148 | // if (value < node.value) { 149 | // if (!node.left) { 150 | // node.left = newNode; 151 | // break; 152 | // } 153 | // node = node.left; 154 | // } else { 155 | // if (!node.right) { 156 | // node.right = newNode; 157 | // break; 158 | // } 159 | // node = node.right; 160 | // } 161 | // } 162 | // } 163 | // } 164 | 165 | // // PREORDER TRAVERSAL 166 | // preorder(node = this.root) { 167 | // if (node) { 168 | // console.log(node.value); 169 | // this.preorder(node.left); 170 | // this.preorder(node.right); 171 | // } } 172 | // // INORDER TRAVERSAL 173 | // inorder(node = this.root) { 174 | // if (node) { 175 | // this.inorder(node.left); 176 | // console.log(node.value); 177 | // this.inorder(node.right); 178 | // } } 179 | // // POSTORDER TRAVERSAL 180 | // postorder(node = this.root) { 181 | // if (node) { 182 | // this.postorder(node.left); 183 | // this.postorder(node.right); 184 | // console.log(node.value); 185 | // } } } 186 | // let bst = new BST(); 187 | // bst.insert(4); 188 | // bst.insert(2); 189 | // bst.insert(6); 190 | // bst.insert(1); 191 | // console.log("Preorder Traversal:"); 192 | // bst.preorder(); 193 | // console.log("Inorder Traversal:"); 194 | // bst.inorder(); 195 | // console.log("Postorder Traversal:"); 196 | // bst.postorder(); 197 | 198 | 199 | 200 | 201 | // class Node { 202 | // constructor(data) { 203 | // this.value = data; 204 | // this.left = null; 205 | // this.right = null; 206 | // } 207 | // } 208 | 209 | // function isBST(node, min = Number.MIN, max = Number.MAX) { 210 | // if (!node) { 211 | // return true; 212 | // } 213 | // if (node.value <= min || node.value >= max) { 214 | // return false; 215 | // } 216 | // return isBST(node.left, min, node.value) && isBST(node.right, node.value, max); 217 | // } 218 | 219 | // // Sample usage: 220 | // const root = new Node(4); 221 | // root.left = new Node(2); 222 | // root.right = new Node(6); 223 | // root.left.left = new Node(1); 224 | // root.left.right = new Node(3); 225 | // root.right.left = new Node(5); 226 | // root.right.right = new Node(7); 227 | // console.log(isBST(root)); // true 228 | 229 | 230 | // class Node { 231 | // constructor(value) { 232 | // this.value = value; 233 | // this.left = null; 234 | // this.right = null; 235 | // } 236 | // } 237 | 238 | // class BST { 239 | // constructor() { 240 | // this.root = null; 241 | // } 242 | 243 | // insert(value) { 244 | // let newNode = new Node(value); 245 | // if (!this.root) { 246 | // this.root = newNode; 247 | // } else { 248 | // let node = this.root; 249 | // while (true) { 250 | // if (value < node.value) { 251 | // if (!node.left) { 252 | // node.left = newNode; 253 | // break; 254 | // } 255 | // node = node.left; 256 | // } else { 257 | // if (!node.right) { 258 | // node.right = newNode; 259 | // break; 260 | // } 261 | // node = node.right; 262 | // } 263 | // } 264 | // } 265 | // } 266 | 267 | // height() { 268 | // if (!this.root) { 269 | // return 0; 270 | // } 271 | // return this._height(this.root); 272 | // } 273 | 274 | // _height(node) { 275 | // if (!node) { 276 | // return 0; 277 | // } 278 | // let leftHeight = this._height(node.left); 279 | // let rightHeight = this._height(node.right); 280 | // return Math.max(leftHeight, rightHeight) + 1; 281 | // } 282 | // } 283 | // let bst = new BST(); 284 | // bst.insert(5); 285 | // bst.insert(3); 286 | // bst.insert(8); 287 | // bst.insert(2); 288 | // bst.insert(4); 289 | // bst.insert(7); 290 | // bst.insert(9); 291 | // console.log(bst.height()); // output: 3 292 | 293 | 294 | 295 | // depth(value) { 296 | // if (!this.root) { 297 | // return 0; 298 | // } 299 | // return this._depth(value, this.root, 1); 300 | // } 301 | // _depth(value, node, level) { 302 | // if (!node) { 303 | // return 0; 304 | // } 305 | // if (node.value === value) { 306 | // return level; 307 | // } 308 | // let leftDepth = this._depth(value, node.left, level + 1); 309 | // let rightDepth = this._depth(value, node.right, level + 1); 310 | // if (leftDepth !== 0 && rightDepth !== 0) { 311 | // return Math.min(leftDepth, rightDepth); 312 | // } 313 | // return leftDepth + rightDepth; 314 | // } 315 | // } 316 | // let bst = new BST(); 317 | // bst.insert(5); 318 | // bst.insert(3); 319 | // bst.insert(8); 320 | // bst.insert(2); 321 | // bst.insert(4); 322 | // bst.insert(7); 323 | // bst.insert(9); 324 | // console.log(bst.depth(7)); // output: 2 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | class Node { 333 | constructor(value) { 334 | this.value = value; 335 | this.left = null; 336 | this.right = null; 337 | } 338 | } 339 | 340 | class BST { 341 | constructor() { 342 | this.root = null; 343 | } 344 | 345 | insert(value) { 346 | let newNode = new Node(value); 347 | if (!this.root) { 348 | this.root = newNode; 349 | } else { 350 | let node = this.root; 351 | while (true) { 352 | if (value < node.value) { 353 | if (!node.left) { 354 | node.left = newNode; 355 | break; 356 | } 357 | node = node.left; 358 | } else { 359 | if (!node.right) { 360 | node.right = newNode; 361 | break; 362 | } 363 | node = node.right; 364 | } 365 | } 366 | } 367 | } 368 | 369 | 370 | maxvalue(){ 371 | let node =this.root 372 | while(node.right !=null){ 373 | node = node.right 374 | } 375 | console.log(node.value) 376 | } 377 | } 378 | 379 | const bst = new BST() 380 | 381 | bst.insert(12) 382 | bst.insert(32) 383 | bst.insert(43) 384 | bst.insert(10) 385 | 386 | console.log(bst.root); 387 | 388 | bst.maxvalue() --------------------------------------------------------------------------------