diff --git a/docs/index.html b/docs/index.html index b12131d..f539be5 100644 --- a/docs/index.html +++ b/docs/index.html @@ -19,10 +19,10 @@ - -
-
-
+ +
+
+
-
-
- - - -
-
+ /> +
+
+ + + + + +
+
+
+
-
-
-
-
-

Rotation Counts

-

Left Rotations: 0

-

Right Rotations: 0

-

Left-Right Rotations: 0

-

Right-Left Rotations: 0

-
-
-

Tree Stats

-

Tree Depth: 0

-

Tree Complexity (number of nodes): 0

+
+
+

Rotation Counts

+

Left Rotations: 0

+

Right Rotations: 0

+

Left-Right Rotations: 0

+

Right-Left Rotations: 0

+
+
+

Tree Stats

+

Tree Depth: 0

+

Tree Complexity (number of nodes): 0

+
-
-
- - GitHub - - Made by Vileend -
-
- -
+
+ + GitHub + + Made by Vileend +
+
+ +
- + \ No newline at end of file diff --git a/docs/script.js b/docs/script.js index 62e8a80..640f3e0 100644 --- a/docs/script.js +++ b/docs/script.js @@ -49,6 +49,7 @@ class AVLTree { constructor(uiUpdater) { this.root = null; this.uiUpdater = uiUpdater; + this.history = []; } height(node) { @@ -131,6 +132,76 @@ class AVLTree { addNode(key) { this.root = this.insert(this.root, key); + this.history.push({ type: 'add', key: key }); + } + + deleteNode(key) { + this.root = this.delete(this.root, key); + this.history.push({ type: 'delete', key: key }); + } + + delete(root, key) { + if (!root) return root; + + if (key < root.key) { + root.left = this.delete(root.left, key); + } else if (key > root.key) { + root.right = this.delete(root.right, key); + } else { + if (!root.left || !root.right) { + root = root.left ? root.left : root.right; + } else { + const temp = this.getMinValueNode(root.right); + root.key = temp.key; + root.right = this.delete(root.right, temp.key); + } + } + + if (!root) return root; + + root.height = 1 + Math.max(this.height(root.left), this.height(root.right)); + const balance = this.getBalance(root); + + if (balance > 1 && this.getBalance(root.left) >= 0) { + return this.rightRotate(root); + } + + if (balance > 1 && this.getBalance(root.left) < 0) { + root.left = this.leftRotate(root.left); + return this.rightRotate(root); + } + + if (balance < -1 && this.getBalance(root.right) <= 0) { + return this.leftRotate(root); + } + + if (balance < -1 && this.getBalance(root.right) > 0) { + root.right = this.rightRotate(root.right); + return this.leftRotate(root); + } + + this.updateNodeDepthAndBalance(root); + return root; + } + + getMinValueNode(node) { + let current = node; + while (current.left) current = current.left; + return current; + } + + undoLastOperation() { + const lastOperation = this.history.pop(); + if (!lastOperation) return; + + if (lastOperation.type === 'add') { + this.root = this.delete(this.root, lastOperation.key); + } else if (lastOperation.type === 'delete') { + this.root = this.insert(this.root, lastOperation.key); + } + + renderTree(); + printTraversals(); } printPreOrder(node = this.root, logger = console.log) { @@ -200,6 +271,29 @@ function addValues() { } } +function deleteValue() { + const valueInput = document.getElementById('valueInput'); + const value = parseInt(valueInput.value, 10); + if (!isNaN(value)) { + tree.deleteNode(value); + + let operations = localStorage.getItem('operations'); + operations = operations ? JSON.parse(operations) : []; + operations.push({ type: 'deleteValue', value: value }); + localStorage.setItem('operations', JSON.stringify(operations)); + + valueInput.value = ''; + renderTree(); + printTraversals(); + } else { + alert('Please enter a valid number.'); + } +} + +function undoLastOperation() { + tree.undoLastOperation(); +} + function collapse(d) { if (d.children) { d._children = d.children; @@ -378,7 +472,13 @@ function updateDimensions() { .attr("height", height + margin.top + margin.bottom); } -window.onresize = updateDimensions; +window.onresize = () => { + + updateDimensions(); + + renderTree(); + +}; function updateTreeStats() { const treeDepth = tree.getDepth(); @@ -426,6 +526,8 @@ function executeSavedOperations() { op.values.forEach(value => { tree.addNode(value); }); + } else if (op.type === 'deleteValue') { + tree.deleteNode(op.value); } }); renderTree(); @@ -440,4 +542,4 @@ window.onload = function () { updateDimensions(); printTraversals(); updateTreeStats(); -}; +}; \ No newline at end of file diff --git a/docs/styles.css b/docs/styles.css index 2a0ab6b..4e202d6 100644 --- a/docs/styles.css +++ b/docs/styles.css @@ -1,36 +1,46 @@ body { font-family: Arial, sans-serif; padding: 20px; + overflow-x: hidden; /* Prevent horizontal scrolling on larger screens */ } + #tree-container { min-height: 500px; position: relative; max-width: 100%; /* Ensure the container doesn't stretch beyond its parent */ - overflow-x: auto; /* Enable horizontal scrolling */ + overflow-x: auto; /* Allow horizontal scrolling on smaller screens */ + overflow-y: auto; /* Allow vertical scrolling */ } - +/* Dark mode styles */ .dark .bg-gray-200 { background-color: #3c4256; } + .dark .bg-gray-300 { background-color: #303544; } + .dark .bg-blue-500 { background-color: #3182ce; } + .dark .bg-red-500 { background-color: #e53e3e; } + .dark .text-white { color: #e2e8f0; } + .dark .border-gray-300 { border-color: #4a5568; } + .dark .focus\:ring-blue-500 { box-shadow: 0 0 0 3px rgba(49, 130, 206, 0.5); } + .dark { background-color: #2D3748; } \ No newline at end of file