From 5ca1350add080e41d6a01d7d35bfa90d28dd5d21 Mon Sep 17 00:00:00 2001 From: apil <078bct017.apil@pcampus.edu.np> Date: Thu, 7 Mar 2024 21:00:26 +0545 Subject: [PATCH 01/10] initial commit --- src/algorithms/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/algorithms/.gitkeep diff --git a/src/algorithms/.gitkeep b/src/algorithms/.gitkeep new file mode 100644 index 0000000..e69de29 From 88541975ca9246657b564b73bd7ea9b3af1c62d9 Mon Sep 17 00:00:00 2001 From: Anoobee Date: Thu, 7 Mar 2024 21:45:16 +0545 Subject: [PATCH 02/10] added doublylinklist --- src/algorithms/dll.js | 113 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/algorithms/dll.js diff --git a/src/algorithms/dll.js b/src/algorithms/dll.js new file mode 100644 index 0000000..1f50184 --- /dev/null +++ b/src/algorithms/dll.js @@ -0,0 +1,113 @@ +class Node { + constructor(data) { + this.data = data; + this.prev = null; + this.next = null; + } +} + +class DoublyLinkedList { + constructor() { + this.head = null; + this.tail = null; + this.size = 0; + } + + isEmpty() { + return this.size === 0; + } + + back() { + if (this.isEmpty()) { + return null; + } + return this.tail.data; + } + + front() { + if (this.isEmpty()) { + return null; + } + return this.head.data; + } + + pushBack(data) { + const newNode = new Node(data); + if (this.isEmpty()) { + this.head = newNode; + this.tail = newNode; + } else { + newNode.prev = this.tail; + this.tail.next = newNode; + this.tail = newNode; + } + this.size++; + } + + pushFront(data) { + const newNode = new Node(data); + if (this.isEmpty()) { + this.head = newNode; + this.tail = newNode; + } else { + newNode.next = this.head; + this.head.prev = newNode; + this.head = newNode; + } + this.size++; + } + + popBack() { + if (this.isEmpty()) { + return null; + } + const removedData = this.tail.data; + if (this.size === 1) { + this.head = null; + this.tail = null; + } else { + this.tail = this.tail.prev; + this.tail.next = null; + } + this.size--; + return removedData; + } + + popFront() { + if (this.isEmpty()) { + return null; + } + const removedData = this.head.data; + if (this.size === 1) { + this.head = null; + this.tail = null; + } else { + this.head = this.head.next; + this.head.prev = null; + } + this.size--; + return removedData; + } + + display() { + let current = this.head; + while (current) { + console.log(current.data); + current = current.next; + } + } +} + +export default DoublyLinkedList; +// // Example usage: +// const dll = new DoublyLinkedList(); +// dll.pushBack(1); +// dll.pushBack("anup"); +// dll.pushBack(3); +// dll.display(); // Output: 1 anup 3 +// console.log("Front:", dll.front()); // Output: Front: 1 +// console.log("Back:", dll.back()); // Output: Back: 3 +// dll.popBack(); +// dll.display(); // Output: 1 anup +// dll.popFront(); +// dll.display(); // Output: anup From fd4c58d7c2fbb0ef824cc821901a34a204cbc35e Mon Sep 17 00:00:00 2001 From: Anoobee Date: Fri, 8 Mar 2024 00:14:13 +0545 Subject: [PATCH 03/10] random --- src/Player/AudioData.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Player/AudioData.js b/src/Player/AudioData.js index 07530d6..e7301a3 100644 --- a/src/Player/AudioData.js +++ b/src/Player/AudioData.js @@ -16,7 +16,7 @@ const songsdata = [ "url": song3 } ] -export { songsdata }; +export { songsdata }; // change hello //Better Data // let savage = { From fd5a650a2c297ce2931e6939cabd7092dbe73c39 Mon Sep 17 00:00:00 2001 From: apil <078bct017.apil@pcampus.edu.np> Date: Fri, 8 Mar 2024 00:55:28 +0545 Subject: [PATCH 04/10] first draft red_black_tree --- src/algorithms/red_black_tree.js | 221 +++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 src/algorithms/red_black_tree.js diff --git a/src/algorithms/red_black_tree.js b/src/algorithms/red_black_tree.js new file mode 100644 index 0000000..2ebce64 --- /dev/null +++ b/src/algorithms/red_black_tree.js @@ -0,0 +1,221 @@ +class RBT_Node { + constructor(key, value) { + this.key = key; + this.value = value; + this.left = null; + this.right = null; + this.parent = null; + this.black = false; + this.isLeftChild = false; + } +} + +//balanced binary search tree uses color labels of node for rebalancing +class RedBlackTree { + constructor() { + this.root = null; + this.height = 0; + this.size = 0; + } + + add(key, value) { + const node = new RBT_Node(key, value); + if (this.root == null) { + this.root = node; + this.root.black = true; + this.size++; + return; + } + this.insert(this.root, node); + this.size++; + } + + insert(parent, newNode) { + if (this.isKeyGreater(newNode, parent)) { + if (parent.right == null) { + parent.right = newNode; + newNode.parent = parent; + newNode.isLeftChild = false; + } else { + this.insert(parent.right, newNode); + } + } else { + if (parent.left == null) { + parent.left = newNode; + newNode.parent = parent; + newNode.isLeftChild = true; + } else { + this.insert(parent.left, newNode); + } + } + this.checkColor(newNode); + return; + } + + //checks for if two consecutive trees are red + checkColor(node) { + if (node == this.root) { + return; + } + if (!node.black && !node.parent.black) { + this.correctTree(node); + } + this.checkColor(node.parent); + } + + //corrects tree + correctTree(node) { + if (node.parent.isLeftChild) { + //aunt is grandparent's irght + if (node.parent.parent.right == null || node.parent.parent.right.black) { + //rotation + this.rotate(node); + return; + } + if (node.parent.parent.right != null) { + node.parent.parent.right.black = true; + } + node.parent.parent.black = false; + node.parent.black = true; + return; + } + + //aunt is grandparent's left i.e. parent is rightChild of node + if (node.parent.parent.left == null || node.parent.parent.left.black) { + //rotation + this.rotate(node); + return; + } + if (node.parent.parent.left != null) { + node.parent.parent.left.black = true; + } + node.parent.parent.black = false; + node.parent.black = true; + return; + } + + //interface for calling which type of rotation + rotate(node) { + if (node.isLeftChild) { + if (node.parent.isLeftChild) { + //actual rotation is done with respect to grandparent + this.rightRotate(node.parent.parent); + node.parent.black = true; + node.black = false; + //set sibling red + if (node.parent.right != null) { + node.parent.right.black = true; + } + return; + } + //is node is left but node's parent is right child + this.rightLeftRotate(node.parent.parent); + node.black = true; + node.right.black = false; + node.left.black = false; + return; + } + //node and node's parent both are right child + if (!node.parent.isLeftChild) { + this.leftRotate(node.parent.parent); + node.black = false; + node.parent.black = true; + if (node.parent.left != null) { + node.parent.left.black = false; + } + return; + } + this.leftRightRotate(node.parent.parent); + node.black = true; + node.right.black = false; + node.left.black = false; + return; + } + + //grandparent node passed of node which disturbed balance + leftRotate(node) { + //RR heavy tree + const temp = node.right; + node.right = temp.left; + if (node.right != null) { + node.right.parent = node; + node.right.isLeftChild = false; + } + if (node.parent == null) { + this.root = temp; + temp.parent = null; + } else { + temp.parent = node.parent; + if (node.isLeftChild) { + temp.isLeftChild = true; + node.parent.left = temp; + } else { + temp.isLeftChild = false; + node.parent.right = temp; + } + temp.left = node; + temp.isLeftChild = true; + node.parent = temp; + } + } + + rightRotate(node) { + const temp = node.left; + node.left = temp.right; + if (node.left != null) { + node.left.parent = node; + node.left.isLeftChild = true; + } + if (node.parent == null) { + this.root = temp; + temp.parent = null; + } + if (node.isLeftChild) { + temp.isLeftChild = true; + node.parent.left = temp; + } else { + temp.isLeftChild = false; + node.parent.right = temp; + } + temp.right = node; + node.isLeftChild = false; + node.parent = temp; + } + + leftRightRotate(node) { + this.leftRotate(node.left); + this.rightRotate(node); + } + + rightLeftRotate(node) { + this.rightRotate(node.right); + this.leftRotate(node); + } + + isKeyGreater(newNode, parent) { + return newNode.key > parent.key; + } + + ascendingOrder() { + this.inOrderTraversal(this.root); + } + + inOrderTraversal(node) { + if (node == null) { + return; + } + this.inOrderTraversal(node.left); + console.log(node.key); + this.inOrderTraversal(node.right); + } +} + +const tree = new RedBlackTree(); +tree.add("apil", 10); +tree.add("bily", 5); +tree.add("anup", 1); +tree.add("kshitiz", 40); +tree.add("aang", 2); +tree.add("zing", 6); +tree.add("zong", 23); +tree.ascendingOrder(); From af6e1393573f03049990460e39812216b9965f9b Mon Sep 17 00:00:00 2001 From: apil <078bct017.apil@pcampus.edu.np> Date: Fri, 8 Mar 2024 01:54:27 +0545 Subject: [PATCH 05/10] working red-black tree --- src/algorithms/red_black_tree.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/algorithms/red_black_tree.js b/src/algorithms/red_black_tree.js index 2ebce64..2b3e4ad 100644 --- a/src/algorithms/red_black_tree.js +++ b/src/algorithms/red_black_tree.js @@ -31,7 +31,7 @@ class RedBlackTree { } insert(parent, newNode) { - if (this.isKeyGreater(newNode, parent)) { + if (this.isNewnodeKeyGreater(newNode, parent)) { if (parent.right == null) { parent.right = newNode; newNode.parent = parent; @@ -55,6 +55,7 @@ class RedBlackTree { //checks for if two consecutive trees are red checkColor(node) { if (node == this.root) { + node.black = true; return; } if (!node.black && !node.parent.black) { @@ -192,13 +193,16 @@ class RedBlackTree { this.leftRotate(node); } - isKeyGreater(newNode, parent) { + isNewnodeKeyGreater(newNode, parent) { return newNode.key > parent.key; } ascendingOrder() { this.inOrderTraversal(this.root); } + descendingOrder() { + this.reverseOrderTraversal(this.root); + } inOrderTraversal(node) { if (node == null) { @@ -208,6 +212,15 @@ class RedBlackTree { console.log(node.key); this.inOrderTraversal(node.right); } + + reverseOrderTraversal(node) { + if (node == null) { + return; + } + this.reverseOrderTraversal(node.right); + console.log(node.key); + this.reverseOrderTraversal(node.left); + } } const tree = new RedBlackTree(); @@ -219,3 +232,5 @@ tree.add("aang", 2); tree.add("zing", 6); tree.add("zong", 23); tree.ascendingOrder(); +console.log(" "); +tree.descendingOrder(); From c457d9841e4f43a5fa12fdbfe2040e8605bf7de1 Mon Sep 17 00:00:00 2001 From: apil <078bct017.apil@pcampus.edu.np> Date: Fri, 8 Mar 2024 22:25:03 +0545 Subject: [PATCH 06/10] redblack-tree export added --- src/algorithms/red_black_tree.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/algorithms/red_black_tree.js b/src/algorithms/red_black_tree.js index 2b3e4ad..997503d 100644 --- a/src/algorithms/red_black_tree.js +++ b/src/algorithms/red_black_tree.js @@ -223,14 +223,18 @@ class RedBlackTree { } } -const tree = new RedBlackTree(); -tree.add("apil", 10); -tree.add("bily", 5); -tree.add("anup", 1); -tree.add("kshitiz", 40); -tree.add("aang", 2); -tree.add("zing", 6); -tree.add("zong", 23); -tree.ascendingOrder(); -console.log(" "); -tree.descendingOrder(); +export default RedBlackTree; + +//Implementation example + +// const tree = new RedBlackTree(); +// tree.add("apil", 10); +// tree.add("bily", 5); +// tree.add("anup", 1); +// tree.add("kshitiz", 40); +// tree.add("aang", 2); +// tree.add("zing", 6); +// tree.add("zong", 23); +// tree.ascendingOrder(); +// console.log(" "); +// tree.descendingOrder(); From 3e53e510c4fc06527f05e56c69ed713e6f5bcd88 Mon Sep 17 00:00:00 2001 From: Anoobee Date: Sat, 9 Mar 2024 01:13:28 +0545 Subject: [PATCH 07/10] integrated DLL --- src/App.jsx | 15 ++++-- src/Player/AudioData.js | 50 ++++++++++++------ src/Player/Player.jsx | 109 ++++++++++++++++++++-------------------- src/algorithms/dll.js | 31 +++++++----- 4 files changed, 116 insertions(+), 89 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 48c315e..881ac67 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,16 +1,17 @@ import Player from "./Player/Player"; import "./Player/player.scss"; -import { songsdata } from "./Player/AudioData.js"; import { useRef, useState, useEffect } from "react"; +import { song_dll } from "./Player/AudioData.js"; +import { BsArrowsExpandVertical } from "react-icons/bs"; const App = () => { - const [songs, setSongs] = useState(songsdata); + const [songs, setSongs] = useState(song_dll); const [isplaying, setisplaying] = useState(false); - const [currentSong, setCurrentSong] = useState(songsdata[1]); + const [currentSong, setCurrentSong] = useState(window.current_song_ptr); const audioElem = useRef(); - useEffect(() => { + useEffect(() => {songs if (isplaying) { audioElem.current.play(); } else { @@ -31,7 +32,11 @@ const App = () => { return (
-