|
| 1 | +// Create node |
| 2 | +const Node = function (item) { |
| 3 | + this.item = item; |
| 4 | + this.height = 1; |
| 5 | + this.left = null; |
| 6 | + this.right = null; |
| 7 | +}; |
| 8 | + |
| 9 | +//AVL Tree |
| 10 | +const AVLTree = function () { |
| 11 | + let root = null; |
| 12 | + |
| 13 | + //return height of the node |
| 14 | + this.height = (N) => { |
| 15 | + if (N === null) { |
| 16 | + return 0; |
| 17 | + } |
| 18 | + |
| 19 | + return N.height; |
| 20 | + }; |
| 21 | + |
| 22 | + //right rotate |
| 23 | + this.rightRotate = (y) => { |
| 24 | + let x = y.left; |
| 25 | + let T2 = x.right; |
| 26 | + x.right = y; |
| 27 | + y.left = T2; |
| 28 | + y.height = Math.max(this.height(y.left), this.height(y.right)) + 1; |
| 29 | + x.height = Math.max(this.height(x.left), this.height(x.right)) + 1; |
| 30 | + return x; |
| 31 | + }; |
| 32 | + |
| 33 | + //left rotate |
| 34 | + this.leftRotate = (x) => { |
| 35 | + let y = x.right; |
| 36 | + let T2 = y.left; |
| 37 | + y.left = x; |
| 38 | + x.right = T2; |
| 39 | + x.height = Math.max(this.height(x.left), this.height(x.right)) + 1; |
| 40 | + y.height = Math.max(this.height(y.left), this.height(y.right)) + 1; |
| 41 | + return y; |
| 42 | + }; |
| 43 | + |
| 44 | + // get balance factor of a node |
| 45 | + this.getBalanceFactor = (N) => { |
| 46 | + if (N == null) { |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | + return this.height(N.left) - this.height(N.right); |
| 51 | + }; |
| 52 | + |
| 53 | + // helper function to insert a node |
| 54 | + const insertNodeHelper = (node, item) => { |
| 55 | + // find the position and insert the node |
| 56 | + if (node === null) { |
| 57 | + return new Node(item); |
| 58 | + } |
| 59 | + |
| 60 | + if (item < node.item) { |
| 61 | + node.left = insertNodeHelper(node.left, item); |
| 62 | + } else if (item > node.item) { |
| 63 | + node.right = insertNodeHelper(node.right, item); |
| 64 | + } else { |
| 65 | + return node; |
| 66 | + } |
| 67 | + |
| 68 | + // update the balance factor of each node |
| 69 | + // and, balance the tree |
| 70 | + node.height = 1 + Math.max(this.height(node.left), this.height(node.right)); |
| 71 | + |
| 72 | + let balanceFactor = this.getBalanceFactor(node); |
| 73 | + |
| 74 | + if (balanceFactor > 1) { |
| 75 | + if (item < node.left.item) { |
| 76 | + return this.rightRotate(node); |
| 77 | + } else if (item > node.left.item) { |
| 78 | + node.left = this.leftRotate(node.left); |
| 79 | + return this.rightRotate(node); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (balanceFactor < -1) { |
| 84 | + if (item > node.right.item) { |
| 85 | + return this.leftRotate(node); |
| 86 | + } else if (item < node.right.item) { |
| 87 | + node.right = this.rightRotate(node.right); |
| 88 | + return this.leftRotate(node); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return node; |
| 93 | + }; |
| 94 | + |
| 95 | + // insert a node |
| 96 | + this.insertNode = (item) => { |
| 97 | + // console.log(root); |
| 98 | + root = insertNodeHelper(root, item); |
| 99 | + }; |
| 100 | + |
| 101 | + //get node with minimum value |
| 102 | + this.nodeWithMimumValue = (node) => { |
| 103 | + let current = node; |
| 104 | + while (current.left !== null) { |
| 105 | + current = current.left; |
| 106 | + } |
| 107 | + return current; |
| 108 | + }; |
| 109 | + |
| 110 | + // delete helper |
| 111 | + const deleteNodeHelper = (root, item) => { |
| 112 | + // find the node to be deleted and remove it |
| 113 | + if (root == null) { |
| 114 | + return root; |
| 115 | + } |
| 116 | + if (item < root.item) { |
| 117 | + root.left = deleteNodeHelper(root.left, item); |
| 118 | + } else if (item > root.item) { |
| 119 | + root.right = deleteNodeHelper(root.right, item); |
| 120 | + } else { |
| 121 | + if (root.left === null || root.right === null) { |
| 122 | + let temp = null; |
| 123 | + if (temp == root.left) { |
| 124 | + temp = root.right; |
| 125 | + } else { |
| 126 | + temp = root.left; |
| 127 | + } |
| 128 | + |
| 129 | + if (temp == null) { |
| 130 | + temp = root; |
| 131 | + root = null; |
| 132 | + } else { |
| 133 | + root = temp; |
| 134 | + } |
| 135 | + } else { |
| 136 | + let temp = this.nodeWithMimumValue(root.right); |
| 137 | + root.item = temp.item; |
| 138 | + root.right = deleteNodeHelper(root.right, temp.item); |
| 139 | + } |
| 140 | + } |
| 141 | + if (root == null) { |
| 142 | + return root; |
| 143 | + } |
| 144 | + |
| 145 | + // Update the balance factor of each node and balance the tree |
| 146 | + root.height = Math.max(this.height(root.left), this.height(root.right)) + 1; |
| 147 | + |
| 148 | + let balanceFactor = this.getBalanceFactor(root); |
| 149 | + if (balanceFactor > 1) { |
| 150 | + if (this.getBalanceFactor(root.left) >= 0) { |
| 151 | + return this.rightRotate(root); |
| 152 | + } else { |
| 153 | + root.left = this.leftRotate(root.left); |
| 154 | + return this.rightRotate(root); |
| 155 | + } |
| 156 | + } |
| 157 | + if (balanceFactor < -1) { |
| 158 | + if (this.getBalanceFactor(root.right) <= 0) { |
| 159 | + return this.leftRotate(root); |
| 160 | + } else { |
| 161 | + root.right = this.rightRotate(root.right); |
| 162 | + return this.leftRotate(root); |
| 163 | + } |
| 164 | + } |
| 165 | + return root; |
| 166 | + }; |
| 167 | + |
| 168 | + //delete a node |
| 169 | + this.deleteNode = (item) => { |
| 170 | + root = deleteNodeHelper(root, item); |
| 171 | + }; |
| 172 | + |
| 173 | + // print the tree in pre - order |
| 174 | + this.preOrder = () => { |
| 175 | + preOrderHelper(root); |
| 176 | + }; |
| 177 | + |
| 178 | + const preOrderHelper = (node) => { |
| 179 | + if (node) { |
| 180 | + console.log(node.item); |
| 181 | + preOrderHelper(node.left); |
| 182 | + preOrderHelper(node.right); |
| 183 | + } |
| 184 | + }; |
| 185 | +}; |
| 186 | + |
| 187 | +// Insert: |
| 188 | +var tree = new AVLTree(); |
| 189 | +tree.insertNode(33); |
| 190 | +tree.insertNode(13); |
| 191 | +tree.insertNode(53); |
| 192 | +tree.insertNode(9); |
| 193 | +tree.insertNode(21); |
| 194 | +tree.insertNode(61); |
| 195 | +tree.insertNode(8); |
| 196 | +tree.insertNode(11); |
| 197 | +tree.preOrder(); |
| 198 | +tree.deleteNode(13); |
| 199 | +console.log("After Deletion: "); |
| 200 | +tree.preOrder(); |
0 commit comments