data-sts31 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AVL/AvlNode.d.ts +8 -0
- package/dist/AVL/AvlNode.js +12 -0
- package/dist/AVL/AvlTreeOperation.d.ts +9 -0
- package/dist/AVL/AvlTreeOperation.js +98 -0
- package/dist/Heap/maxHeap.d.ts +14 -0
- package/dist/Heap/maxHeap.js +63 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +11 -0
- package/package.json +15 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import avlNode from "./AvlNode";
|
|
2
|
+
export default class AvlTreeOperation {
|
|
3
|
+
insert(root: avlNode | null, key: number): avlNode | null;
|
|
4
|
+
rightRotation(root: avlNode | null): avlNode | null;
|
|
5
|
+
leftRotation(root: avlNode | null): avlNode | null;
|
|
6
|
+
balancedFactor(root: avlNode | null): number;
|
|
7
|
+
getHeight(root: avlNode | null): number;
|
|
8
|
+
preorder(root: avlNode | null): void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const AvlNode_1 = require("./AvlNode");
|
|
4
|
+
class AvlTreeOperation {
|
|
5
|
+
insert(root, key) {
|
|
6
|
+
var _a;
|
|
7
|
+
// if root does not exist
|
|
8
|
+
if (!root) {
|
|
9
|
+
return new AvlNode_1.default(key);
|
|
10
|
+
}
|
|
11
|
+
// if key is less than root value
|
|
12
|
+
else if (key < root.val) {
|
|
13
|
+
root.left = this.insert(root.left, key);
|
|
14
|
+
}
|
|
15
|
+
// if key is greater than root value
|
|
16
|
+
else if (key > root.val) {
|
|
17
|
+
root.right = this.insert(root.right, key);
|
|
18
|
+
}
|
|
19
|
+
// if key is equal to root value
|
|
20
|
+
else {
|
|
21
|
+
return root;
|
|
22
|
+
}
|
|
23
|
+
// update height of node
|
|
24
|
+
root.height = 1 + Math.max(this.getHeight(root.left), this.getHeight(root.right));
|
|
25
|
+
// check balance factor
|
|
26
|
+
let balance = this.balancedFactor(root);
|
|
27
|
+
// LL rotation
|
|
28
|
+
if (balance > 1 && root.left && key < root.left.val) {
|
|
29
|
+
return this.rightRotation(root);
|
|
30
|
+
}
|
|
31
|
+
// RR rotation
|
|
32
|
+
else if (balance < -1 && root.right && key > root.right.val) {
|
|
33
|
+
return this.leftRotation(root);
|
|
34
|
+
}
|
|
35
|
+
// LR rotation
|
|
36
|
+
else if (balance > 1 && root.left && key > root.left.val) {
|
|
37
|
+
console.log("LR");
|
|
38
|
+
root.left = this.leftRotation(root.left);
|
|
39
|
+
return this.rightRotation(root);
|
|
40
|
+
}
|
|
41
|
+
// RL rotation
|
|
42
|
+
else if (balance < -1 && root.right && key < ((_a = root.right) === null || _a === void 0 ? void 0 : _a.val)) {
|
|
43
|
+
root.right = this.rightRotation(root.right);
|
|
44
|
+
return this.leftRotation(root);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
return root;
|
|
48
|
+
}
|
|
49
|
+
return root;
|
|
50
|
+
// not unbalanced
|
|
51
|
+
}
|
|
52
|
+
rightRotation(root) {
|
|
53
|
+
if (!root || !root.left) {
|
|
54
|
+
return root;
|
|
55
|
+
}
|
|
56
|
+
let newRoot = root.left;
|
|
57
|
+
let leftRootChild = newRoot.right;
|
|
58
|
+
newRoot.right = root;
|
|
59
|
+
root.left = leftRootChild;
|
|
60
|
+
// Update heights
|
|
61
|
+
root.height = 1 + Math.max(this.getHeight(root.left), this.getHeight(root.right));
|
|
62
|
+
newRoot.height = 1 + Math.max(this.getHeight(newRoot.left), this.getHeight(newRoot.right));
|
|
63
|
+
return newRoot;
|
|
64
|
+
}
|
|
65
|
+
leftRotation(root) {
|
|
66
|
+
if (!root || !root.right) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
let newNode = root.right;
|
|
70
|
+
let newNodeRightChild = newNode.left;
|
|
71
|
+
newNode.left = root;
|
|
72
|
+
root.right = newNodeRightChild;
|
|
73
|
+
root.height = 1 + Math.max(this.getHeight(root.left), this.getHeight(root.right));
|
|
74
|
+
newNode.height = 1 + Math.max(this.getHeight(root.left), this.getHeight(root.right));
|
|
75
|
+
return newNode;
|
|
76
|
+
}
|
|
77
|
+
balancedFactor(root) {
|
|
78
|
+
if (!root) {
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
return this.getHeight(root.left) - this.getHeight(root.right);
|
|
82
|
+
}
|
|
83
|
+
getHeight(root) {
|
|
84
|
+
if (!root) {
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
return root.height;
|
|
88
|
+
}
|
|
89
|
+
preorder(root) {
|
|
90
|
+
if (!root) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
console.log(root.val);
|
|
94
|
+
this.preorder(root.left);
|
|
95
|
+
this.preorder(root.right);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.default = AvlTreeOperation;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default class MaxHeap {
|
|
2
|
+
private size;
|
|
3
|
+
private ArraySize;
|
|
4
|
+
private heap;
|
|
5
|
+
constructor(n: number);
|
|
6
|
+
insert(val: number): void;
|
|
7
|
+
delete(): void;
|
|
8
|
+
private heapify;
|
|
9
|
+
private swap;
|
|
10
|
+
private leftChild;
|
|
11
|
+
private rightChild;
|
|
12
|
+
private getParent;
|
|
13
|
+
print(): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class MaxHeap {
|
|
4
|
+
constructor(n) {
|
|
5
|
+
this.heap = new Array(n);
|
|
6
|
+
this.ArraySize = n;
|
|
7
|
+
this.size = 0;
|
|
8
|
+
}
|
|
9
|
+
insert(val) {
|
|
10
|
+
if (this.size === this.ArraySize) {
|
|
11
|
+
console.log("Heap Overflow");
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
this.heap[this.size] = val;
|
|
15
|
+
let index = this.size;
|
|
16
|
+
this.size++;
|
|
17
|
+
while (index > 0 &&
|
|
18
|
+
this.heap[this.getParent(index)] < this.heap[index]) {
|
|
19
|
+
this.swap(index, this.getParent(index));
|
|
20
|
+
index = this.getParent(index);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
delete() {
|
|
24
|
+
if (this.size === 0) {
|
|
25
|
+
console.log("Heap Underflow");
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
this.heap[0] = this.heap[this.size - 1];
|
|
29
|
+
this.size--;
|
|
30
|
+
this.heapify(0);
|
|
31
|
+
}
|
|
32
|
+
heapify(index) {
|
|
33
|
+
let largest = index;
|
|
34
|
+
let left = this.leftChild(index);
|
|
35
|
+
let right = this.rightChild(index);
|
|
36
|
+
if (left < this.size && this.heap[left] > this.heap[largest]) {
|
|
37
|
+
largest = left;
|
|
38
|
+
}
|
|
39
|
+
if (right < this.size && this.heap[right] > this.heap[largest]) {
|
|
40
|
+
largest = right;
|
|
41
|
+
}
|
|
42
|
+
if (largest !== index) {
|
|
43
|
+
this.swap(index, largest);
|
|
44
|
+
this.heapify(largest);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
swap(i, j) {
|
|
48
|
+
[this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]];
|
|
49
|
+
}
|
|
50
|
+
leftChild(i) {
|
|
51
|
+
return 2 * i + 1;
|
|
52
|
+
}
|
|
53
|
+
rightChild(i) {
|
|
54
|
+
return 2 * i + 2;
|
|
55
|
+
}
|
|
56
|
+
getParent(i) {
|
|
57
|
+
return Math.floor((i - 1) / 2);
|
|
58
|
+
}
|
|
59
|
+
print() {
|
|
60
|
+
console.log(this.heap.slice(0, this.size));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.default = MaxHeap;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AvlTree = exports.MaxHeap = void 0;
|
|
4
|
+
var MaxHeap_1 = require("./Heap/MaxHeap");
|
|
5
|
+
Object.defineProperty(exports, "MaxHeap", { enumerable: true, get: function () { return MaxHeap_1.default; } });
|
|
6
|
+
var AvlTreeOperation_1 = require("./AVL/AvlTreeOperation");
|
|
7
|
+
Object.defineProperty(exports, "AvlTree", { enumerable: true, get: function () { return AvlTreeOperation_1.default; } });
|
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/test.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const index_1 = require("./index");
|
|
4
|
+
const heap = new index_1.MaxHeap(5);
|
|
5
|
+
heap.insert(10);
|
|
6
|
+
heap.insert(40);
|
|
7
|
+
heap.insert(30);
|
|
8
|
+
heap.insert(50);
|
|
9
|
+
heap.print(); // [50, 40, 30, 10]
|
|
10
|
+
heap.delete();
|
|
11
|
+
heap.print(); // [40, 10, 30]
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "data-sts31",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Max Heap implementation in TypeScript",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "node dist/test.js"
|
|
10
|
+
},
|
|
11
|
+
"files": ["dist"],
|
|
12
|
+
"keywords": ["max-heap", "heap", "dsa", "typescript"],
|
|
13
|
+
"author": "Shubham Shrivastwa",
|
|
14
|
+
"license": "MIT"
|
|
15
|
+
}
|