├── Designs ├── V1.xd ├── Landing Page.png ├── Loading Page.png └── colour_palette.jpg ├── js ├── tailwindConfig.js ├── utils.js ├── animation │ ├── animationScript.js │ └── anime.min.js ├── datastructureControllers │ ├── stack.js │ ├── queue │ │ ├── circularqueue.js │ │ ├── priorityqueue │ │ │ ├── UnsortedPriorityQueue.js │ │ │ └── SortedPriorityQueue.js │ │ └── queue.js │ ├── binaryTree.js │ ├── singleLinkedList.js │ ├── doubleLinkedList.js │ └── array.js └── algorithmsControllers │ ├── searchAlgorithms.js │ └── sortAlgorithms.js ├── README.md ├── LICENSE ├── index.html ├── stack.html ├── binary.html ├── doublylinklist.html ├── array.html ├── search.html ├── singlylinklist.html ├── sort.html ├── priorityqueue.html └── queue.html /Designs/V1.xd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aashray446/DSA_MADE_EASY/HEAD/Designs/V1.xd -------------------------------------------------------------------------------- /Designs/Landing Page.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aashray446/DSA_MADE_EASY/HEAD/Designs/Landing Page.png -------------------------------------------------------------------------------- /Designs/Loading Page.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aashray446/DSA_MADE_EASY/HEAD/Designs/Loading Page.png -------------------------------------------------------------------------------- /Designs/colour_palette.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aashray446/DSA_MADE_EASY/HEAD/Designs/colour_palette.jpg -------------------------------------------------------------------------------- /js/tailwindConfig.js: -------------------------------------------------------------------------------- 1 | tailwind.config = { 2 | theme: { 3 | colors : { 4 | 'primary' : '#166D8A', 5 | 'secondary' : '#FDD741', 6 | 'light' : '#F4F3ED', 7 | 'primary10' : '#24B4A5', 8 | 'primary20' : '#8FEDC2', 9 | 'pink' : '#c17c74' 10 | } , 11 | extend: { 12 | fontFamily : { 13 | serif : ['Abel', 'sans'], 14 | sansSerif : ['Abel', 'sans-serif'] 15 | } 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DSA_MADE_EASY 2 | A fun way to learn about DSA concepts 3 | 4 | Checkout the site at https://aashray446.github.io/DSA_MADE_EASY/index.html 5 | 6 | The project is based on plain javascript, HTML, and CSS with no framework purposely, As to help beginners understand the code without getting overwhelmed by any framework code. 7 | 8 | ### How to run the Code ?? 9 | - Just open Index.html (Easy right?? ) 10 | 11 |  12 | 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Aashray Katiyar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /js/utils.js: -------------------------------------------------------------------------------- 1 | // ACCORDION 2 | export default (() => { 3 | let acc = document.getElementsByClassName("accordion"); 4 | for (let i = 0; i < acc.length; i++) { 5 | acc[i].addEventListener("click", function() { 6 | this.classList.toggle("active"); 7 | let panel = this.nextElementSibling; 8 | panel.style.maxHeight ? panel.style.maxHeight = null : panel.style.maxHeight = panel.scrollHeight + "px"; 9 | for (i = 0; i < acc.length; i++) { 10 | if (acc[i] != this && acc[i].classList.contains("active")) { 11 | acc[i].classList.toggle("active"); 12 | acc[i].nextElementSibling.style.maxHeight = null; 13 | } 14 | } 15 | }); 16 | } 17 | })() 18 | 19 | const getRandomInt = (min, max) => { 20 | return Math.floor(Math.random() * (max - min + 1)) + min; 21 | } 22 | 23 | const createElement = (data) => { 24 | const element = document.createElement('div'); 25 | element.classList.add(this.structure) 26 | element.style.backgroundColor = data?.color 27 | element.innerHTML = `
${data?.value}
` 28 | element.classList.add('circlerow') 29 | return element 30 | } 31 | 32 | const getRandomColor = () => { 33 | var trans = '0.3'; 34 | var color = 'rgba('; 35 | for (var i = 0; i < 3; i++) { 36 | color += Math.floor(Math.random() * 255) + ','; 37 | } 38 | color += trans + ')'; 39 | return color; 40 | } 41 | 42 | const wrapFR = (value, isfront) => { 43 | if(isfront) return `Front is ${value}
` 44 | return `Rear is ${value}
` 45 | } 46 | 47 | 48 | 49 | function sleep(ms) { 50 | return new Promise( 51 | resolve => setTimeout(resolve, ms) 52 | ); 53 | } 54 | 55 | 56 | export {getRandomInt, createElement, getRandomColor, wrapFR, sleep} -------------------------------------------------------------------------------- /js/animation/animationScript.js: -------------------------------------------------------------------------------- 1 | function fitElementToParent(el, padding) { 2 | var timeout = null; 3 | function resize() { 4 | if (timeout) clearTimeout(timeout); 5 | anime.set(el, {scale: 1}); 6 | var pad = padding || 0; 7 | var parentEl = el.parentNode; 8 | var elOffsetWidth = el.offsetWidth - pad; 9 | var parentOffsetWidth = parentEl.offsetWidth; 10 | var ratio = parentOffsetWidth / elOffsetWidth; 11 | timeout = setTimeout(anime.set(el, {scale: ratio}), 10); 12 | } 13 | resize(); 14 | window.addEventListener('resize', resize); 15 | } 16 | 17 | var advancedStaggeringAnimation = (function() { 18 | 19 | var staggerVisualizerEl = document.querySelector('.stagger-visualizer'); 20 | var dotsWrapperEl = staggerVisualizerEl.querySelector('.dots-wrapper'); 21 | var dotsFragment = document.createDocumentFragment(); 22 | var grid = [10, 10]; 23 | var cell = 50; 24 | var numberOfElements = grid[0] * grid[1]; 25 | var animation; 26 | var paused = true; 27 | 28 | fitElementToParent(staggerVisualizerEl, 0); 29 | 30 | for (var i = 0; i < numberOfElements; i++) { 31 | var dotEl = document.createElement('div'); 32 | dotEl.classList.add('dot'); 33 | dotEl.innerText = Math.floor(Math.random() * 9) + 1; 34 | dotsFragment.appendChild(dotEl); 35 | } 36 | 37 | dotsWrapperEl.appendChild(dotsFragment); 38 | 39 | var index = anime.random(0, numberOfElements-1); 40 | var nextIndex = 0; 41 | 42 | anime.set('.stagger-visualizer .cursor', { 43 | translateX: anime.stagger(-cell, {grid: grid, from: index, axis: 'x'}), 44 | translateY: anime.stagger(-cell, {grid: grid, from: index, axis: 'y'}), 45 | translateZ: 0, 46 | scale: 1.5, 47 | }); 48 | 49 | function play() { 50 | 51 | paused = false; 52 | if (animation) animation.pause(); 53 | 54 | nextIndex = anime.random(0, numberOfElements-1); 55 | 56 | animation = anime.timeline({ 57 | easing: 'easeInOutQuad', 58 | complete: play 59 | }) 60 | .add({ 61 | targets: '.stagger-visualizer .cursor', 62 | keyframes: [ 63 | { scale: .75, duration: 120}, 64 | { scale: 2.5, duration: 220}, 65 | { scale: 1.5, duration: 450}, 66 | ], 67 | duration: 300 68 | }) 69 | .add({ 70 | targets: '.stagger-visualizer .dot', 71 | keyframes: [ 72 | { 73 | translateX: anime.stagger('-2px', {grid: grid, from: index, axis: 'x'}), 74 | translateY: anime.stagger('-2px', {grid: grid, from: index, axis: 'y'}), 75 | duration: 100 76 | }, { 77 | translateX: anime.stagger('4px', {grid: grid, from: index, axis: 'x'}), 78 | translateY: anime.stagger('4px', {grid: grid, from: index, axis: 'y'}), 79 | scale: anime.stagger([2.6, 1], {grid: grid, from: index}), 80 | duration: 225 81 | }, { 82 | translateX: 0, 83 | translateY: 0, 84 | scale: 1, 85 | duration: 1200, 86 | } 87 | ], 88 | delay: anime.stagger(80, {grid: grid, from: index}) 89 | }, 30) 90 | .add({ 91 | targets: '.stagger-visualizer .cursor', 92 | translateX: { value: anime.stagger(-cell, {grid: grid, from: nextIndex, axis: 'x'}) }, 93 | translateY: { value: anime.stagger(-cell, {grid: grid, from: nextIndex, axis: 'y'}) }, 94 | scale: 1.5, 95 | easing: 'cubicBezier(.075, .2, .165, 1)' 96 | }, '-=800') 97 | 98 | index = nextIndex; 99 | 100 | } 101 | 102 | play(); 103 | 104 | })(); -------------------------------------------------------------------------------- /js/datastructureControllers/stack.js: -------------------------------------------------------------------------------- 1 | //Container Variable Definition 2 | const container = document.getElementById("animation-container"); 3 | const structure = "stack"; 4 | const errorBox = document.getElementById("error"); 5 | 6 | const textFieldStack = document.getElementById("push"); 7 | const pushingData = document.getElementById("pushingData"); 8 | const popingData = document.getElementById("popingData"); 9 | 10 | const createElement = function (value) { 11 | const element = document.createElement("div"); 12 | element.classList.add("hidden"); 13 | element.classList.add(structure); 14 | if (value == null) { 15 | element.innerHTML = "-"; 16 | } else { 17 | element.innerHTML = value; 18 | } 19 | container.prepend(element); 20 | }; 21 | 22 | // ------------------------------ ANIMATIONS FUNCTIONS ------------------------------------------------- 23 | 24 | const movePushingElement = function () { 25 | let target = document.getElementById("pushingData"); 26 | let arrayElements = document.getElementsByClassName("stack"); 27 | 28 | // Some Unkown Error BruteForcely Solved 29 | let destination = arrayElements[1]; 30 | if (arrayElements.length == 1) { 31 | destination = arrayElements[0]; 32 | } 33 | 34 | let x = destination.offsetLeft - target.offsetLeft; 35 | let y = destination.offsetTop - target.offsetTop; 36 | 37 | anime({ 38 | targets: target, 39 | translateY: [{ value: y, easing: "linear" }], 40 | translateX: { 41 | value: x, 42 | }, 43 | complete: function () { 44 | document.getElementsByClassName("stack")[0].classList.remove("hidden"); 45 | anime.set(target, { 46 | translateX: 0, 47 | translateY: 0, 48 | }); 49 | }, 50 | }); 51 | }; 52 | 53 | const movePopingData = function () { 54 | let destination = document.getElementById("popingData"); 55 | target = document.getElementsByClassName("stack")[0]; 56 | 57 | let x = destination.offsetLeft - target.offsetLeft; 58 | let y = destination.offsetTop - target.offsetTop; 59 | 60 | anime({ 61 | targets: "stack", 62 | translateY: [{ value: y, easing: "linear" }], 63 | translateX: { 64 | value: x, 65 | }, 66 | complete: function () { 67 | var stackElementHolder = document.getElementsByClassName("stack")[0]; 68 | if (stackElementHolder) { 69 | document.getElementsByClassName("stack")[0].classList.remove("hidden"); 70 | anime.set(target, { 71 | translateX: 0, 72 | translateY: 0, 73 | }); 74 | } 75 | }, 76 | }); 77 | }; 78 | 79 | // ------------------ Implementation of stack with animation ------------------------- 80 | // Stack Implementation with array 81 | class Stack { 82 | 83 | //construtor 84 | constructor() { 85 | this.dataArray = new Array(); 86 | } 87 | push(value) { 88 | 89 | // Check if the value is null or not 90 | if (value == "") { 91 | errorBox.style.display = "block"; 92 | return; 93 | } else { 94 | errorBox.style.display = "none"; 95 | } 96 | 97 | if (this.dataArray.length > 12) { 98 | errorBox.style.display = "block"; 99 | errorBox.innerText = "Stack is Full"; 100 | return; 101 | } 102 | // THIS IS CREATING THE UI PART 103 | document.getElementById("pushingData").innerHTML = value; 104 | createElement(value); 105 | // ACUTAL ARRAY PUTTING 106 | this.dataArray.push(value); 107 | movePushingElement(); 108 | textFieldStack.value = ""; 109 | } 110 | 111 | pop() { 112 | if (this.dataArray.length == 0) { 113 | errorBox.style.display = "block"; 114 | errorBox.innerText = "Stack is Empty"; 115 | return; 116 | } 117 | movePopingData(); 118 | const list = document.getElementsByClassName(structure); 119 | list[0].remove(); 120 | document.getElementById("popingData").innerHTML = this.dataArray.pop(); 121 | } 122 | 123 | reset() { 124 | container.innerHTML = ""; 125 | this.dataArray = new Array(); 126 | errorBox.style.display = "none"; 127 | textFieldStack.value = ""; 128 | popingData.innerHTML = ""; 129 | pushingData.innerHTML = ""; 130 | }; 131 | 132 | peek() { 133 | if (this.dataArray.length == 0) { 134 | errorBox.style.display = "block"; 135 | errorBox.innerText = "Stack is Empty"; 136 | return; 137 | } 138 | const list = document.getElementsByClassName(structure); 139 | document.getElementById("popingData").innerHTML = 140 | this.dataArray[this.dataArray.length - 1]; 141 | } 142 | } 143 | 144 | // Function that will be used by the browser 145 | const stack = new Stack(); -------------------------------------------------------------------------------- /js/datastructureControllers/queue/circularqueue.js: -------------------------------------------------------------------------------- 1 | 2 | class Circularqueue{ 3 | container = document.getElementById('animation-container') 4 | structure = 'queue' 5 | 6 | qf = queue.qf 7 | qr = queue.qr 8 | inf = queue.inf 9 | 10 | createElement = (data) => { 11 | const element = document.createElement('div'); 12 | element.classList.add(this.structure) 13 | element.style.backgroundColor=data?.color 14 | element.innerHTML = `${data?.value}
` 15 | element.classList.add('circlerow') 16 | return element 17 | } 18 | 19 | wrapFR = (value, isfront) => { 20 | if(isfront) { 21 | return `Front is ${value}
` 22 | } 23 | return `Rear is ${value}
` 24 | } 25 | 26 | array = new Array(); 27 | front = -1; 28 | rear = -1; 29 | length = 19; 30 | isactive=false; 31 | 32 | enqueue=(value)=>{ 33 | console.log("circular enqueuing") 34 | if(this.isFull()){ 35 | this.inf.getElementsByTagName('p')[0].innerHTML = "Queue Overflow! Queue is full!" 36 | this.inf.style.display = 'block' 37 | return false; 38 | } 39 | 40 | let enqinf = document.getElementById('enqueueinfo') 41 | if(value=="" || value<-99 || value>99) { 42 | enqinf.getElementsByTagName('p')[0].innerHTML = "Random number is taken for Enqueue!" 43 | enqinf.style.display = 'block' 44 | value = Math.floor((Math.random() * 99) + 1); 45 | } else { 46 | enqinf.style.display = 'none' 47 | } 48 | 49 | const bg_color = getRandomColor() 50 | let element = this.createElement({'value': value, 'color': bg_color}) 51 | if(this.isEmpty()){ 52 | this.rear=this.front=0; 53 | this.array[this.rear]=value; 54 | this.container.replaceChild(element,this.container.children[this.rear]) 55 | } else{ 56 | this.rear=(this.rear+1)%this.length; 57 | this.array[this.rear]=value; 58 | this.container[this.rear]=value; 59 | this.container.replaceChild(element, this.container.children[this.rear]) 60 | } 61 | this.inf.style.display = 'none' 62 | this.qr.innerHTML = this.wrapFR(this.array[this.rear]||-1, false) 63 | this.qf.innerHTML = this.wrapFR(this.array[this.front]||-1, true) 64 | this.qf.style.display = 'flex' 65 | this.qr.style.display = 'flex' 66 | return true; 67 | } 68 | 69 | dequeue=()=>{ 70 | console.log("circular dequeuing") 71 | if(this.isEmpty()){ 72 | this.inf.getElementsByTagName('p')[0].innerHTML = "Queue Underflow! Queue is empty!" 73 | this.inf.style.display = 'block' 74 | return false; 75 | } 76 | this.container.insertBefore(this.createElement({'value': '-', 'color': 'rgba(255,255,255,0)'}), this.container.children[this.front]) 77 | this.container.removeChild(this.container.children[this.front+1]) 78 | if(this.front==this.rear){ 79 | this.front=-1; 80 | this.rear=-1; 81 | } else{ 82 | this.front=(this.front+1)%this.length; 83 | } 84 | this.inf.style.display = 'none' 85 | this.qr.innerHTML = this.wrapFR(this.array[this.rear]||-1, false) 86 | this.qf.innerHTML = this.wrapFR(this.array[this.front]||-1, true) 87 | this.qf.style.display = 'flex' 88 | this.qr.style.display = 'flex' 89 | return true; 90 | } 91 | 92 | isFull=()=>{ 93 | console.log(this.front, this.rear) 94 | if((this.rear+1)%this.length==this.front){ 95 | return true; 96 | } 97 | return false; 98 | } 99 | 100 | isEmpty=()=>{ 101 | console.log(this.front, this.rear) 102 | if(this.rear==this.front && this.front==-1){ 103 | return true; 104 | } 105 | return false; 106 | } 107 | 108 | reset=()=>{ 109 | this.array = new Array(); 110 | this.front = -1; 111 | this.rear = -1; 112 | this.container.innerHTML = ''; 113 | if(this.isactive) { 114 | this.container.classList.add('circlecontainer') 115 | for (let i = 0; i < this.length; i++) { 116 | this.container.append(this.createElement({'value': '-', 'color': 'rgba(255,255,255,0)'})); 117 | } 118 | }else { 119 | for (let i = 0; i < this.length; i++) { 120 | if(this.container.firstChild) { 121 | this.container.firstChild.remove(); 122 | } 123 | } 124 | } 125 | 126 | } 127 | 128 | handlengthchange=()=>{ 129 | for(let i=0; i${value}
` 30 | return element 31 | } 32 | 33 | array = new Array(); 34 | length; 35 | searchtype = 'linear'; 36 | 37 | render(){ 38 | this.display.style.visibility = 'hidden'; 39 | this.container.innerHTML = ''; 40 | for (let i = 0; i < this.array.length; i++) { 41 | this.container.append(this.createElement(this.array[i])); 42 | } 43 | } 44 | 45 | add(value) { 46 | let enqinf = document.getElementById('enqueueinfo') 47 | if(value=="" || value<-99 || value>99) { 48 | value = Math.floor((Math.random() * 99) + 1); 49 | enqinf.getElementsByTagName('p')[0].innerHTML = "Random number is added!" 50 | enqinf.style.display = 'block' 51 | } else{ 52 | enqinf.style.display = 'none' 53 | } 54 | this.array.push(value); 55 | this.render(); 56 | } 57 | 58 | generateRandomArray(){ 59 | length=20; 60 | this.array = new Array(); 61 | for(let i=0;iElement not found
` 101 | } 102 | return index; 103 | } 104 | 105 | async linearSearch(array, target) { 106 | this.render() 107 | for (let i = 0; i < array.length; i++) { 108 | this.display.style.visibility = 'visible'; 109 | this.display.innerHTML = `Checking element at index ${i}.
` 110 | this.container.children[i].style.backgroundColor = 'orange'; 111 | if (array[i] == target) { 112 | return i; 113 | } 114 | await sleep(this.speed/2); 115 | this.container.children[i].style.backgroundColor = '#24b4a6df'; 116 | } 117 | 118 | return -1; 119 | } 120 | 121 | async binarySearch(array,target) { 122 | array = bblSort(this.array); 123 | this.array=array; 124 | this.render() 125 | let left = 0; 126 | let right = array.length - 1; 127 | let mid = Math.floor((left + right) / 2); 128 | while (left <= right) { 129 | this.display.style.visibility = 'visible'; 130 | this.display.innerHTML = `Mid is ${mid}, start is ${left}, end is ${right}
` 131 | this.container.children[mid].style.backgroundColor = 'orange'; 132 | if (array[mid] == target) { 133 | return mid; 134 | } else if (array[mid] > target) { 135 | right = mid - 1; 136 | } else { 137 | left = mid + 1; 138 | } 139 | await sleep(this.speed); 140 | this.container.children[mid].style.backgroundColor = '#24b4a6df'; 141 | mid = Math.floor((left + right) / 2); 142 | } 143 | return -1; 144 | } 145 | 146 | reset() { 147 | this.array = new Array(); 148 | this.container.innerHTML = ''; 149 | } 150 | } 151 | 152 | let searchAlgorithms = new SearchAlgorithms(); -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |DSA VISUALIZER BY GROUP 14
91 |>-<
` 62 | } 63 | await sleep(this.speed); 64 | if (container.children[count + 1]) { 65 | container.children[count + 1].innerHTML = `<->
`; 66 | } 67 | await sleep(this.speed); 68 | if (parseInt(node.key) < min) { 69 | if (container.children[count]) { 70 | container.children[count].style.backgroundColor = 'lightgreen'; 71 | await sleep(this.speed); 72 | container.children[count].style.backgroundColor = node.color; 73 | } 74 | document.getElementById('peekindex').innerHTML = node.address ? node.address : '-'; 75 | document.getElementById('peekkey').innerHTML = node.key ? node.key : '-'; 76 | document.getElementById('peekindex').style.backgroundColor = node.color ? node.color : '-'; 77 | min = node.key; 78 | minNode = node; 79 | count2 = count; 80 | } 81 | count += 2; 82 | node = node.next; 83 | } 84 | await sleep(this.speed); 85 | if (container.children[count2]) { 86 | container.children[count2].style.backgroundColor = 'red'; 87 | } 88 | await sleep(this.speed); 89 | if (container.children[count2]) { 90 | container.children[count2].style.backgroundColor = minNode.color; 91 | } 92 | if(minNode.previous != null) {minNode.previous.next = minNode.next;} 93 | if(minNode.next != null){minNode.next.previous = minNode.previous;} 94 | if(minNode == this.head){this.head = minNode.next;} 95 | if(minNode == this.tail){this.tail = minNode.previous;} 96 | this.generate(); 97 | return node; 98 | } 99 | 100 | min = () => { 101 | if(this.isEmpty()){ 102 | info.getElementsByTagName('p')[0].innerHTML = "Queue Underflow! Queue is empty!" 103 | info.style.display = 'block' 104 | } 105 | let min = Number.MAX_VALUE; 106 | let minNode = new Node(null, null); 107 | let node = this.head; 108 | while(node !== null){ 109 | if(parseInt(node.key) < min){ 110 | min = node.key; 111 | minNode = node; 112 | } 113 | node = node.next; 114 | } 115 | return minNode; 116 | } 117 | 118 | size = () => { 119 | let count = 0; 120 | let node = this.head; 121 | while(node !== null){ 122 | count+=1; 123 | node = node.next; 124 | } 125 | return count; 126 | } 127 | 128 | createelement = (data) => { 129 | const element = document.createElement('div'); 130 | element.classList.add('sudo-pqueue') 131 | element.style.backgroundColor = data.color; 132 | element.innerHTML = ` 133 |<->
` 145 | return element 146 | } 147 | 148 | generate = () => { 149 | info.style.display = 'none' 150 | document.getElementById('peekindex').innerHTML = this?.min()?.address ? this.min().address : '-'; 151 | document.getElementById('peekkey').innerHTML = this?.min()?.key ? this.min().key : '-'; 152 | document.getElementById('peekindex').style.backgroundColor = this?.min()?.color ? this.min().color : '-'; 153 | document.getElementById('sizearea').innerHTML = this.size(); 154 | container.innerHTML = ''; 155 | let node = this.head; 156 | while(node !== null){ 157 | let element = this.createelement({ 158 | key : node?.key ? node.key : '-', 159 | value : node?.value ? node.value : '-', 160 | next : node?.next, 161 | previous : node?.previous, 162 | color : node?.color 163 | }) 164 | container.append(element) 165 | if(node.next){ 166 | container.append(this.inb()) 167 | } 168 | node = node.next; 169 | } 170 | } 171 | 172 | reset = () => { 173 | this.head = null; 174 | this.tail = null; 175 | container.innerHTML = ''; 176 | } 177 | 178 | randomize = () => { 179 | let value = Math.floor((Math.random() * 99) + 1); 180 | let key = Math.floor((Math.random() * 99) + 1); 181 | this.enqueue(key, value); 182 | } 183 | } 184 | 185 | let unsortedpriorityqueue = new UnsortedPriorityQueue(); -------------------------------------------------------------------------------- /js/algorithmsControllers/sortAlgorithms.js: -------------------------------------------------------------------------------- 1 | function sleep(ms) { 2 | return new Promise(resolve => setTimeout(resolve, ms)); 3 | } 4 | 5 | class SortAlgorithms{ 6 | container = document.getElementById('animation-container') 7 | display = document.getElementById('sort-box') 8 | inf = document.getElementById('queueinfo') 9 | isRepresentation=false; 10 | speed = 600; 11 | length = 40; 12 | 13 | structure = 'queue' 14 | 15 | isFull = () => { 16 | return this.array.length >= this.length; 17 | } 18 | 19 | createElement = (value) => { 20 | if(this.isRepresentation) { 21 | const element = document.createElement('div'); 22 | element.classList.add('queue-representation'); 23 | element.style.height = `${value*6}px`; 24 | return element 25 | } 26 | else { 27 | const element = document.createElement('div'); 28 | element.classList.add(this.structure) 29 | element.innerHTML = `${value}
` 30 | return element 31 | } 32 | } 33 | 34 | array = new Array(); 35 | sorttype = 'bubble'; 36 | 37 | render(){ 38 | this.display.style.visibility = 'hidden'; 39 | this.container.innerHTML = ''; 40 | for (let i = 0; i < this.array.length; i++) { 41 | this.container.append(this.createElement(this.array[i])); 42 | } 43 | } 44 | 45 | add=(value)=>{ 46 | if(this.isFull()) { 47 | this.inf.getElementsByTagName('p')[0].innerHTML = "Array is full !"; 48 | this.inf.style.display = 'block'; 49 | return; 50 | } 51 | let enqinf = document.getElementById('enqueueinfo') 52 | if(value=="" || value<-99 || value>99) { 53 | value = Math.floor((Math.random() * 99) + 1); 54 | enqinf.getElementsByTagName('p')[0].innerHTML = "Random number is added!" 55 | enqinf.style.display = 'block' 56 | } else{ 57 | enqinf.style.display = 'none' 58 | } 59 | this.array.push(value); 60 | this.render(); 61 | } 62 | 63 | generateRandomArray() { 64 | this.array = new Array(); 65 | for (let i = 0; i < this.length; i++) { 66 | this.add(Math.floor((Math.random() * 99) + 1)); 67 | } 68 | this.render(); 69 | } 70 | 71 | remove(index) { 72 | if (index !== -1) { 73 | this.array.pop(index); 74 | this.container.lastChild.remove(); 75 | } 76 | this.render(); 77 | 78 | } 79 | 80 | async sort() { 81 | if(this.array.length==0) { 82 | this.inf.getElementsByTagName('p')[0].innerHTML = "Please add some elements to the Array !"; 83 | this.inf.style.display = 'block'; 84 | return; 85 | } 86 | switch (this.sorttype) { 87 | case 'bubble': 88 | await this.bubbleSort(); 89 | break; 90 | case 'selection': 91 | await this.selectionSort(); 92 | break; 93 | case 'insertion': 94 | await this.insertionSort(); 95 | break; 96 | } 97 | this.render() 98 | this.display.style.visibility = 'visible'; 99 | this.display.innerHTML = `Sorted
` 100 | } 101 | 102 | async bubbleSort() { 103 | let i, j, temp; 104 | for (i = 0; i < this.array.length; i++) { 105 | for (j = 0; j < this.array.length - i - 1; j++) { 106 | if (this.array[j] > this.array[j + 1]) { 107 | this.container.children[j].style.backgroundColor = 'orange'; 108 | this.container.children[j+1].style.backgroundColor = 'lightgreen'; 109 | await sleep(this.speed/2); 110 | temp = this.array[j]; 111 | this.array[j] = this.array[j + 1]; 112 | this.array[j + 1] = temp; 113 | this.render(); 114 | this.container.children[j].style.backgroundColor = 'lightgreen'; 115 | this.container.children[j+1].style.backgroundColor = 'orange'; 116 | await sleep(this.speed/2); 117 | this.container.children[j].style.backgroundColor = '#24b4a6df'; 118 | this.container.children[j+1].style.backgroundColor = '#24b4a6df'; 119 | 120 | } 121 | } 122 | } 123 | } 124 | 125 | async selectionSort() { 126 | let i, j, min, temp; 127 | for (i = 0; i < this.array.length; i++) { 128 | min = i; 129 | for (j = i + 1; j < this.array.length; j++) { 130 | if (this.array[j] < this.array[min]) { 131 | min = j; 132 | } 133 | } 134 | await sleep(this.speed/3); 135 | this.render(); 136 | this.container.children[min].style.backgroundColor = 'orange'; 137 | this.container.children[i].style.backgroundColor = 'lightgreen'; 138 | await sleep(this.speed/3); 139 | temp = this.array[i]; 140 | this.array[i] = this.array[min]; 141 | this.array[min] = temp; 142 | this.render(); 143 | this.container.children[min].style.backgroundColor = 'lightgreen'; 144 | this.container.children[i].style.backgroundColor = 'orange'; 145 | await sleep(this.speed/3); 146 | } 147 | } 148 | 149 | async insertionSort() { 150 | let i, j, temp; 151 | for (i = 1; i < this.array.length; i++) { 152 | this.container.children[i].style.backgroundColor = 'orange'; 153 | temp = this.array[i]; 154 | this.display.style.visibility = 'visible'; 155 | this.display.innerHTML = `Temp-> ${temp}
` 156 | j = i - 1; 157 | while (j >= 0 && this.array[j] > temp) { 158 | this.display.innerHTML = `Temp-> ${temp}
` 159 | this.container.children[i].style.backgroundColor = 'orange'; 160 | if(i>=j+2) { 161 | this.container.children[j + 1].style.backgroundColor = 'lightblue'; 162 | if(!this.isRepresentation) { 163 | this.container.children[j + 2].text = `${temp}
`; 164 | } 165 | } 166 | this.display.style.visibility = 'visible'; 167 | this.array[j + 1] = this.array[j]; 168 | this.container.children[j + 1].style.backgroundColor = 'lightblue'; 169 | if(!this.isRepresentation) { 170 | this.container.children[j + 1].innerHTML = `${temp}
`; 171 | } 172 | j = j - 1; 173 | await sleep(this.speed/3); 174 | this.render(); 175 | } 176 | this.array[j + 1] = temp; 177 | this.container.children[i].style.backgroundColor = 'orange'; 178 | if(!this.isRepresentation) { 179 | this.container.children[j + 1].innerHTML = `${temp}
`; 180 | } 181 | this.container.children[j + 1].style.backgroundColor = 'lightblue'; 182 | await sleep(this.speed/3); 183 | this.container.children[j + 1].style.backgroundColor = 'green'; 184 | await sleep(this.speed/3); 185 | this.render() 186 | } 187 | } 188 | 189 | reset() { 190 | this.array = new Array(); 191 | this.container.innerHTML = ''; 192 | } 193 | } 194 | 195 | let sortAlgorithms = new SortAlgorithms(); -------------------------------------------------------------------------------- /js/datastructureControllers/queue/priorityqueue/SortedPriorityQueue.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(key, value, color = getRandomColor(), address = generateRandomTwoLetterWord()) { 3 | this.key = key; 4 | this.value = value; 5 | this.next = null; 6 | this.previous = null; 7 | this.address = address; 8 | this.color=color; 9 | } 10 | } 11 | 12 | function sleep(ms) { 13 | return new Promise(resolve => setTimeout(resolve, ms)); 14 | } 15 | 16 | function getRandomColor() { 17 | let color = "#"; 18 | for (let i = 0; i < 3; i++) 19 | color += ("0" + Math.floor(Math.random() * Math.pow(16, 2) / 2).toString(16)).slice(-2); 20 | return color; 21 | } 22 | 23 | const addresses = []; 24 | 25 | function generateRandomTwoLetterWord(){ 26 | const result = Math.random().toString(36).substring(2,5); 27 | for (let i = 0; i < addresses.length; i++) { 28 | if(addresses[i] == result) { 29 | return generateRandomTwoLetterWord(); 30 | } 31 | } 32 | console.log(addresses) 33 | addresses.push(result); 34 | return result; 35 | } 36 | 37 | const container = document.getElementById('animation-container') 38 | const info = document.getElementById('info') 39 | 40 | class SortedPriorityQueue { 41 | isSorted = false; 42 | head = null; 43 | tail = null; 44 | speed = 300; 45 | 46 | isEmpty = () => { 47 | if(this.tail == null && this.head == null){ 48 | return true; 49 | } 50 | return false; 51 | } 52 | 53 | enqueue = async (key, value) => { 54 | if (parseInt(key) < 1 || parseInt(key) > 99) { 55 | info.getElementsByTagName('p')[0].innerHTML = "Key must be from 1 to 99!" 56 | info.style.display = 'block' 57 | return; 58 | } 59 | if (parseInt(value) < -99 || parseInt(value) > 99) { 60 | info.getElementsByTagName('p')[0].innerHTML = "Value must be from -99 to 99!" 61 | info.style.display = 'block' 62 | return; 63 | } 64 | if (key == '' || value == '') { 65 | info.getElementsByTagName('p')[0].innerHTML = "Key and value must not be empty!" 66 | info.style.display = 'block' 67 | return; 68 | } 69 | let start = this.head; 70 | let temp = new Node(key, value); 71 | if (this.isEmpty()) { 72 | this.head = temp; 73 | this.tail = temp; 74 | } else { 75 | let count = 0; 76 | if (this.head.key > key) { 77 | temp.next = this.head; 78 | this.head.previous = temp; 79 | this.head = temp; 80 | } else { 81 | while (start != null) { 82 | await sleep(this.speed); 83 | if(container.children[count]) { 84 | container.children[count].style.backgroundColor = 'lightgreen'; 85 | } 86 | await sleep(this.speed); 87 | if(container.children[count]) { 88 | container.children[count].style.backgroundColor = start.color; 89 | } 90 | if(container.children[count+1]) { 91 | container.children[count + 1].innerHTML = `>-<
` 92 | } 93 | await sleep(this.speed); 94 | if(container.children[count+1]) { 95 | container.children[count+1].innerHTML = `<->
`; 96 | } 97 | await sleep(this.speed); 98 | if (Number.parseInt(start.key) > Number.parseInt(temp.key)) { 99 | temp.next = start; 100 | temp.previous = start.previous; 101 | start.previous.next = temp; 102 | start.previous = temp; 103 | this.generate(); 104 | return; 105 | } 106 | count+=2; 107 | start = start.next; 108 | } 109 | this.tail.next = temp; 110 | temp.previous = this.tail; 111 | this.tail = temp; 112 | } 113 | } 114 | this.generate(); 115 | return; 116 | } 117 | 118 | dequeue = async () => { 119 | console.log("sorted dequeue") 120 | if (this.isEmpty()) { 121 | info.getElementsByTagName('p')[0].innerHTML = "Queue Underflow! Queue is empty!" 122 | info.style.display = 'block' 123 | return null; 124 | } 125 | container.children[0].style.backgroundColor = 'red'; 126 | await sleep(this.speed); 127 | let temp = this.head 128 | if (temp !== null) { 129 | this.head = temp.next; 130 | if (this.head !== null) { 131 | this.head.previous = null; 132 | } else { 133 | this.tail = null; 134 | this.head = null; 135 | } 136 | } 137 | this.size(); 138 | this.generate(); 139 | return temp; 140 | } 141 | 142 | min = () => { 143 | if(this.isEmpty()){ 144 | info.getElementsByTagName('p')[0].innerHTML = "Queue Underflow! Queue is empty!" 145 | info.style.display = 'block' 146 | return null; 147 | } 148 | return this.head; 149 | } 150 | 151 | size = () => { 152 | let count = 0; 153 | let node = this.head; 154 | while(node !== null){ 155 | count+=1; 156 | node = node.next; 157 | } 158 | return count; 159 | } 160 | 161 | createelement = (data) => { 162 | const element = document.createElement('div'); 163 | element.classList.add('sudo-pqueue') 164 | element.style.backgroundColor = data.color; 165 | element.innerHTML = ` 166 |<->
` 178 | return element 179 | } 180 | 181 | 182 | generate = () => { 183 | info.style.display = 'none' 184 | document.getElementById('peekindex').innerHTML = this?.head?.address ? this.head.address : '-'; 185 | document.getElementById('peekkey').innerHTML = this?.head?.key ? this.head.key : '-'; 186 | document.getElementById('peekindex').style.backgroundColor = this?.head?.color ? this.head.color : ''; 187 | document.getElementById('sizearea').innerHTML = this.size(); 188 | container.innerHTML = ''; 189 | let node = this.head; 190 | while(node !== null){ 191 | let element = this.createelement({ 192 | key : node?.key ? node.key : '-', 193 | value : node?.value ? node.value : '-', 194 | next : node?.next, 195 | previous : node?.previous, 196 | color : node?.color 197 | }) 198 | container.append(element) 199 | if(node.next){ 200 | container.append(this.inb()) 201 | } 202 | node = node.next; 203 | } 204 | } 205 | 206 | 207 | reset = () => { 208 | this.head = null; 209 | this.tail = null; 210 | this.generate(); 211 | } 212 | 213 | randomize = () => { 214 | let value = Math.floor((Math.random() * 99) + 1); 215 | let key = Math.floor((Math.random() * 99) + 1); 216 | this.enqueue(key, value); 217 | } 218 | 219 | } 220 | 221 | let sortedPriorityQueue = new SortedPriorityQueue(); -------------------------------------------------------------------------------- /binary.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |Current Process : -
None
122 |Current Node Memory : -
None
123 |Current Node Data : -
None
124 |Current Head Memory : -
None
125 |Current Tail Memory: -
None
126 | 127 | 128 | 129 |${data?.value}
` 9 | return element 10 | } 11 | 12 | 13 | const getRandomColor = () => { 14 | var trans = '0.3'; 15 | var color = 'rgba('; 16 | for (var i = 0; i < 3; i++) { 17 | color += Math.floor(Math.random() * 255) + ','; 18 | } 19 | color += trans + ')'; 20 | return color; 21 | } 22 | 23 | const wrapFR = (value, isfront) => { 24 | if(isfront) { 25 | return `Front is ${value}
` 26 | } 27 | return `Rear is ${value}
` 28 | } 29 | 30 | 31 | class Queue { 32 | dataArray = new Array(); 33 | front=-1; 34 | rear=-1; 35 | length=19; 36 | detailed=false; 37 | isdeque=false; 38 | 39 | qf = document.getElementById('queueFront') 40 | qr = document.getElementById('queueRear') 41 | inf = document.getElementById('queueinfo') 42 | 43 | enqueue=(value)=>{ 44 | 45 | if(this.isFull()) { 46 | this.inf.getElementsByTagName('p')[0].innerHTML = "Queue Overflow! Queue is full!" 47 | this.inf.style.display = 'block' 48 | } 49 | else { 50 | let enqinf = document.getElementById('enqueueinfo') 51 | if(value=="" || value<-99 || value>99) { 52 | enqinf.getElementsByTagName('p')[0].innerHTML = "Random number is taken for Enqueue!" 53 | enqinf.style.display = 'block' 54 | value = Math.floor((Math.random() * 99) + 1); 55 | } else { 56 | enqinf.style.display = 'none' 57 | } 58 | this.inf.style.display = 'none' 59 | const bg_color = getRandomColor() 60 | this.qr.innerHTML = wrapFR(value, false) 61 | this.qr.style.backgroundColor = bg_color 62 | container.append(createElement({'value': value, 'color': bg_color})) 63 | this.dataArray.push(value) 64 | if (this.detailed){ 65 | this.qf.innerHTML = wrapFR(this.dataArray[this.front],true) 66 | } 67 | if (this.rear == -1) { 68 | this.front = 0; 69 | this.qf.innerHTML = wrapFR(this.dataArray[this.front],true) 70 | this.rear = 0; 71 | this.qf.style.display = 'flex' 72 | this.qr.style.display = 'flex' 73 | } else { 74 | this.rear++; 75 | } 76 | } 77 | 78 | } 79 | 80 | enqueueFront=(value)=>{ 81 | if(this.isFull()) { 82 | this.inf.getElementsByTagName('p')[0].innerHTML = "Queue Overflow! Queue is full!" 83 | this.inf.style.display = 'block' 84 | } 85 | else { 86 | let enqinf = document.getElementById('enqueueinfo') 87 | if(value=="" || value<-99 || value>99) { 88 | enqinf.getElementsByTagName('p')[0].innerHTML = "Random number is taken for Enqueue!" 89 | enqinf.style.display = 'block' 90 | value = Math.floor((Math.random() * 99) + 1); 91 | } else { 92 | enqinf.style.display = 'none' 93 | } 94 | this.inf.style.display = 'none' 95 | const bg_color = getRandomColor() 96 | this.qf.innerHTML = wrapFR(value, true) 97 | this.qf.style.backgroundColor = bg_color 98 | container.prepend(createElement({'value': value, 'color': bg_color})) 99 | this.dataArray.unshift(value) 100 | if (this.rear == -1) { 101 | this.rear = 0; 102 | this.qf.innerHTML = wrapFR(this.dataArray[this.rear],true) 103 | this.front = 0; 104 | this.qf.style.display = 'flex' 105 | this.qr.style.display = 'flex' 106 | } else { 107 | this.rear++; 108 | } 109 | } 110 | } 111 | 112 | dequeue=()=>{ 113 | if(this.isEmpty()) { 114 | this.inf.getElementsByTagName('p')[0].innerHTML = "Queue Underflow! Queue is empty!" 115 | this.inf.style.display = 'block' 116 | } else { 117 | this.inf.style.display = 'none' 118 | const list = document.getElementsByClassName(structure) 119 | if(this.detailed){ 120 | if(list[this.front]) { 121 | list[this.front].style.opacity = '0.1' 122 | } 123 | if (this.dataArray[this.front+1]) { 124 | this.qf.innerHTML = wrapFR(this.dataArray[this.front+1],true) 125 | } else { 126 | this.qf.innerHTML = wrapFR(-1,true) 127 | this.qr.innerHTML = wrapFR(-1,false) 128 | } 129 | this.front++; 130 | }else{ 131 | list[0].remove() 132 | this.dataArray?.pop() 133 | this.qf.innerHTML = wrapFR(list[0]?.getElementsByTagName('p')[0]?.innerHTML || -1,true) 134 | this.qf.style.backgroundColor = list[0]?.style?.backgroundColor 135 | this.front++; 136 | } 137 | } 138 | if(this.dataArray.length == 0 || this.front>this.rear+1) { 139 | if(!this.detailed) { 140 | this.front = -1; 141 | this.rear = -1; 142 | } else{ 143 | this.front--; 144 | this.inf.getElementsByTagName('p')[0].innerHTML = "Queue Underflow! Queue is empty!" 145 | this.inf.style.display = 'block' 146 | } 147 | console.log(this.dataArray.length,this.length,"this") 148 | this.qf.innerHTML = wrapFR(-1,true) 149 | this.qr.innerHTML = wrapFR(-1,false) 150 | } 151 | } 152 | 153 | dequeueRear=()=> { 154 | if (this.isEmpty()) { 155 | this.inf.getElementsByTagName('p')[0].innerHTML = "Queue Underflow! Queue is empty!" 156 | this.inf.style.display = 'block' 157 | } else { 158 | this.inf.style.display = 'none' 159 | const list = document.getElementsByClassName(structure) 160 | if (this.detailed) { 161 | if (list[this.rear]) { 162 | list[this.rear].style.opacity = '0.1' 163 | } 164 | if (this.dataArray[this.rear - 1]) { 165 | this.qr.innerHTML = wrapFR(this.dataArray[this.rear - 1], false) 166 | } else { 167 | this.qf.innerHTML = wrapFR(-1, true) 168 | this.qr.innerHTML = wrapFR(-1, false) 169 | } 170 | this.rear--; 171 | } else { 172 | list[list.length - 1].remove() 173 | this.dataArray?.pop() 174 | this.qr.innerHTML = wrapFR(list[list.length - 1]?.getElementsByTagName('p')[0]?.innerHTML || -1, false) 175 | this.qr.style.backgroundColor = list[list.length - 1]?.style?.backgroundColor 176 | this.rear--; 177 | } 178 | } 179 | if (this.dataArray.length == 0 || this.front > this.rear + 1) { 180 | if (!this.detailed) { 181 | this.front = -1; 182 | this.rear = -1; 183 | } else { 184 | this.rear--; 185 | this.inf.getElementsByTagName('p')[0].innerHTML = "Queue Underflow! Queue is empty!" 186 | this.inf.style.display = 'block' 187 | } 188 | this.qf.innerHTML = wrapFR(-1, true) 189 | this.qr.innerHTML = wrapFR(-1, false) 190 | } 191 | } 192 | 193 | isFull=()=>{ 194 | if(this.dataArray.length > this.length) { 195 | return true; 196 | } 197 | return false; 198 | } 199 | 200 | isEmpty=()=>{ 201 | if(this.front == -1) { 202 | this.qf.innerHTML = wrapFR(-1,true) 203 | this.qr.innerHTML = wrapFR(-1,false) 204 | return true; 205 | } 206 | return false; 207 | } 208 | 209 | reset = () => { 210 | this.inf.style.display = 'none' 211 | this.qf.innerHTML = wrapFR(-1,true) 212 | this.qr.innerHTML = wrapFR(-1,false) 213 | container.innerHTML = '' 214 | this.dataArray = new Array(); 215 | this.front=-1; 216 | this.rear=-1; 217 | } 218 | 219 | toggledeque = () => { 220 | if(this.isdeque) { 221 | this.qf.style.display = 'flex' 222 | this.qr.style.display = 'flex' 223 | document.getElementById('dequeuerearbutton').style.display = 'block' 224 | document.getElementById('enqueuefrontbutton').style.display = 'block' 225 | document.getElementById('enqueuebuttongroup').classList.add('grid-cols-2') 226 | document.getElementById('dequeuebuttongroup').classList.add('grid-cols-2') 227 | } else { 228 | this.qf.style.display = 'none' 229 | this.qr.style.display = 'none' 230 | document.getElementById('dequeuerearbutton').style.display = 'none' 231 | document.getElementById('enqueuefrontbutton').style.display = 'none' 232 | document.getElementById('enqueuebuttongroup').classList.remove('grid-cols-2') 233 | document.getElementById('dequeuebuttongroup').classList.remove('grid-cols-2') 234 | } 235 | } 236 | } 237 | 238 | let queue = new Queue() 239 | 240 | 241 | 242 | 243 | 244 | -------------------------------------------------------------------------------- /search.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |Current Process : -
None
129 |Current Node Memory : -
None
130 |Current Node Data : -
None
131 |Current Head Memory : -
None
132 | 133 | 134 | 135 |Front is -1
214 |Rear is -1
217 |