From bd6bd60be54521256d40eb1519cf148b8cee9317 Mon Sep 17 00:00:00 2001 From: gyim1345 Date: Fri, 1 Jan 2021 02:33:38 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[gyim1345]=20=EC=9D=B4=EC=A7=84=20=ED=83=90?= =?UTF-8?q?=EC=83=89=20=ED=8A=B8=EB=A6=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../heap/BinarySearchTree.test.js" | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 "\354\236\220\353\243\214\352\265\254\354\241\260/heap/BinarySearchTree.test.js" diff --git "a/\354\236\220\353\243\214\352\265\254\354\241\260/heap/BinarySearchTree.test.js" "b/\354\236\220\353\243\214\352\265\254\354\241\260/heap/BinarySearchTree.test.js" new file mode 100644 index 0000000..3ae196e --- /dev/null +++ "b/\354\236\220\353\243\214\352\265\254\354\241\260/heap/BinarySearchTree.test.js" @@ -0,0 +1,217 @@ +class Node { + constructor(value) { + this.value = value; + this.parent = null; + this.left = null; + this.right = null; + } +} + +class BinarySearchTree { + constructor() { + this.rootNode = null; + } + + insert(value) { + const newNode = new Node(value); + + if (this.rootNode === null) { + this.rootNode = newNode; + return; + } + let nextNode = this.rootNode; + let prevNode = null; + + while (nextNode !== null) { + prevNode = nextNode; + if (value < nextNode.value) { + nextNode = nextNode.left; + continue; + } + nextNode = nextNode.right; + } + + newNode.parent = prevNode; + + if (prevNode && prevNode.value > newNode.value) { + prevNode.left = newNode; + return; + } + prevNode.right = newNode; + } + + getHeight() { + const heights = []; + this.getHeights(heights); + + // console.log(heights, 'array'); + const maxHeight = Math.max(...heights); + // console.log(maxHeight, 'maxheight'); + this.height = maxHeight; + return maxHeight; + } + + getHeights(heights, height = 1, currentNode = this.rootNode) { + this.height = 0; + if (!this.rootNode) { + return 0; + } + this.height = Math.max(height, this.height); + if (currentNode.right) { + this.getHeights(heights, height + 1, currentNode.right); + } + if (currentNode.left) { + this.getHeights(heights, height + 1, currentNode.left); + } + heights.push(height); + } + + peakValue() { + return this.rootNode.value; + } + + findMax(node) { + let currentNode = node || this.rootNode; + while (currentNode.right !== null) { + currentNode = currentNode.right; + } + return currentNode; + } + + findMin(node) { + let currentNode = node || this.rootNode; + while (currentNode.left !== null) { + currentNode = currentNode.left; + } + return currentNode; + } + + remove(value) { + const currentNode = this.getNodeByValue(value); + + if (!currentNode.left && !currentNode.right) { + const direction = this.getChildDirection(currentNode); + currentNode.parent[direction] = null; + return; + } + + if (!currentNode.left) { + const minNode = this.findMin(currentNode.right); + this.changeParentDirection(currentNode, minNode); + return; + } + + const maxNode = this.findMax(currentNode.left); + maxNode.left = currentNode.left; + this.changeParentDirection(currentNode, maxNode); + } + + changeParentDirection(node, targetNode) { + targetNode.parent = node.parent; + const direction = this.getChildDirection(node); + node.parent[direction] = targetNode; + } + + getChildDirection(node) { + return node.parent.value > node.value ? 'left' : 'right'; + } + + getNodeByValue(value) { + let currentNode = this.rootNode; + let previousNode = null; + while (currentNode.value !== value && currentNode !== null) { + previousNode = currentNode; + if (value < currentNode.value) { + currentNode = currentNode.left; + continue; + } + currentNode = currentNode.right; + } + return currentNode; + } + + peakValue() { + return this.rootNode.value; + } +} + +test('insert', () => { + const bst = new BinarySearchTree(); + bst.insert(25); + bst.insert(17); + bst.insert(34); + bst.insert(19); + bst.insert(24); + bst.insert(5); + expect(bst.peakValue()).toBe(25); + expect(bst.getHeight()).toBe(4); +}); + +test('findMax', () => { + const bst = new BinarySearchTree(); + bst.insert(25); + bst.insert(17); + bst.insert(34); + bst.insert(19); + bst.insert(24); + bst.insert(5); + expect(bst.findMax(bst.rootNode.left).value).toBe(24); + expect(bst.findMax().value).toBe(34); +}); + +test('findMin', () => { + const bst = new BinarySearchTree(); + bst.insert(25); + bst.insert(17); + bst.insert(34); + bst.insert(19); + bst.insert(24); + bst.insert(5); + expect(bst.findMin(bst.rootNode.right).value).toBe(34); + expect(bst.findMin().value).toBe(5); +}); + +test('remove no child', () => { + const bst = new BinarySearchTree(); + bst.insert(3); + bst.insert(1); + bst.insert(5); + + bst.remove(1); + expect(bst.rootNode.left).toBe(null); +}); + +test('remove 1 child left', () => { + const bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(8); + bst.insert(3); + bst.insert(4); + bst.remove(3); + + expect(bst.rootNode.left.value).toBe(4); +}); + +test('remove 1 child right', () => { + const bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(8); + bst.insert(3); + bst.insert(4); + bst.insert(7); + bst.remove(8); + + expect(bst.rootNode.right.value).toBe(7); +}); + +test('remove 2 child', () => { + const bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(8); + bst.insert(3); + bst.insert(4); + bst.insert(1); + bst.remove(3); + + expect(bst.rootNode.left.value).toBe(1); +}); From 31568b346f4881e95d7fa368b45047fa601cab06 Mon Sep 17 00:00:00 2001 From: gyim1345 Date: Fri, 1 Jan 2021 02:35:47 +0900 Subject: [PATCH 2/2] remove annotation --- .../heap/BinarySearchTree.test.js" | 2 -- 1 file changed, 2 deletions(-) diff --git "a/\354\236\220\353\243\214\352\265\254\354\241\260/heap/BinarySearchTree.test.js" "b/\354\236\220\353\243\214\352\265\254\354\241\260/heap/BinarySearchTree.test.js" index 3ae196e..bdce4f2 100644 --- "a/\354\236\220\353\243\214\352\265\254\354\241\260/heap/BinarySearchTree.test.js" +++ "b/\354\236\220\353\243\214\352\265\254\354\241\260/heap/BinarySearchTree.test.js" @@ -44,9 +44,7 @@ class BinarySearchTree { const heights = []; this.getHeights(heights); - // console.log(heights, 'array'); const maxHeight = Math.max(...heights); - // console.log(maxHeight, 'maxheight'); this.height = maxHeight; return maxHeight; }