From 05184c42686e0e9d56e23706a39cc9a881a0fadc Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 16:16:41 +0530 Subject: [PATCH 01/17] update: find k-th max in BST --- .../Trees/BST/find-kth-max/index.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/_DataStructures_/Trees/BST/find-kth-max/index.js diff --git a/src/_DataStructures_/Trees/BST/find-kth-max/index.js b/src/_DataStructures_/Trees/BST/find-kth-max/index.js new file mode 100644 index 00000000..67f7aea2 --- /dev/null +++ b/src/_DataStructures_/Trees/BST/find-kth-max/index.js @@ -0,0 +1,35 @@ +const BST = require('../index'); + +// Inorder traversal returns a sorted array +function inOrderTraversal(root) { + if (root === null) return []; + let arr = []; + // traverse left + const left = inOrderTraversal(root.leftChild); + arr = [...left, root.value]; + const right = inOrderTraversal(root.rightChild); + return [...arr, ...right]; +} + +function findKthMax(rootNode, k) { + const arr = inOrderTraversal(rootNode); + return arr[arr.length - k]; +} + +// // create a BST +// const myBST = new BST(6); + +// myBST.add(2); +// myBST.add(19); +// myBST.add(14); +// myBST.add(8); +// myBST.add(5); +// myBST.add(12); +// myBST.add(33); +// myBST.add(52); +// myBST.add(1); + +// // find 3rd max +// console.log(findKthMax(myBST.root, 3)); + +module.exports = findKthMax; From 4a751475a28607f04cd03c57b95efe71ad644e34 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 16:17:28 +0530 Subject: [PATCH 02/17] update: entry in readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 665bc09c..bd35ee53 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Trees](src/_DataStructures_/Trees) - [Binary Search Tree](src/_DataStructures_/Trees/BST) + - [Find kth maximin in a BST](src/_DataStructures_/Trees/BST/find-kth-max) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From f3761352bd4dadaad45302391083ca5b8aa60579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B1=E2=88=82?= Date: 2019年10月10日 16:24:35 +0530 Subject: [PATCH 03/17] fix: closing of `` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f8389dab..d0120523 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Trees](src/_DataStructures_/Trees) - [Binary Search Tree](src/_DataStructures_/Trees/BST) - - [Find kth maximin in a BST](src/_DataStructures_/Trees/BST/find-kth-max) + - [Find kth maximin in a BST](src/_DataStructures_/Trees/BST/find-kth-max) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From 0ff8d471dbb0a02dc0f857775929a930a09a49cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B1=E2=88=82?= Date: 2019年10月10日 16:31:29 +0530 Subject: [PATCH 04/17] fix: typo error --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d0120523..0bdad6f2 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Trees](src/_DataStructures_/Trees) - [Binary Search Tree](src/_DataStructures_/Trees/BST) - - [Find kth maximin in a BST](src/_DataStructures_/Trees/BST/find-kth-max) + - [Find kth maximum in a BST](src/_DataStructures_/Trees/BST/find-kth-max) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From 0bf9860d52a0883ac7782bdda28ac31083862a4f Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 16:36:06 +0530 Subject: [PATCH 05/17] update: find kth min & throw error for invalid K --- .../Trees/BST/find-kth-max/index.js | 3 ++ .../Trees/BST/find-kth-minimum/index.js | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/_DataStructures_/Trees/BST/find-kth-minimum/index.js diff --git a/src/_DataStructures_/Trees/BST/find-kth-max/index.js b/src/_DataStructures_/Trees/BST/find-kth-max/index.js index 67f7aea2..fd798fcd 100644 --- a/src/_DataStructures_/Trees/BST/find-kth-max/index.js +++ b/src/_DataStructures_/Trees/BST/find-kth-max/index.js @@ -13,6 +13,9 @@ function inOrderTraversal(root) { function findKthMax(rootNode, k) { const arr = inOrderTraversal(rootNode); + if (k < 0 || k> arr.lenth) { + throw new Error('Invalid value for K'); + } return arr[arr.length - k]; } diff --git a/src/_DataStructures_/Trees/BST/find-kth-minimum/index.js b/src/_DataStructures_/Trees/BST/find-kth-minimum/index.js new file mode 100644 index 00000000..a28115ad --- /dev/null +++ b/src/_DataStructures_/Trees/BST/find-kth-minimum/index.js @@ -0,0 +1,40 @@ +// eslint-disable-next-line no-unused-vars +const BST = require('../index'); + +// Inorder traversal returns a sorted array +function inOrderTraversal(root) { + if (root === null) return []; + let arr = []; + // traverse left + const left = inOrderTraversal(root.leftChild); + arr = [...left, root.value]; + const right = inOrderTraversal(root.rightChild); + return [...arr, ...right]; +} + +function findKthMin(rootNode, k) { + const arr = inOrderTraversal(rootNode); + if (k < 0 || k> arr.lenth) { + throw new Error('Invalid value for K'); + } + return arr[k - 1]; +} + +// // create a BST +// const myBST = new BST(6); + +// myBST.add(2); +// myBST.add(19); +// myBST.add(14); +// myBST.add(8); +// myBST.add(5); +// myBST.add(12); +// myBST.add(33); +// myBST.add(52); +// myBST.add(1); +// myBST.add(0); + +// // find 3rd max +// console.log(findKthMin(myBST.root, 3)); + +module.exports = findKthMin; From ca93db32f78a85c295fa95c405cdac516cd79a85 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 16:37:26 +0530 Subject: [PATCH 06/17] update: entry in README & folder rename --- README.md | 1 + .../Trees/BST/{find-kth-minimum => find-kth-min}/index.js | 0 2 files changed, 1 insertion(+) rename src/_DataStructures_/Trees/BST/{find-kth-minimum => find-kth-min}/index.js (100%) diff --git a/README.md b/README.md index 0bdad6f2..83f38022 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Trees](src/_DataStructures_/Trees) - [Binary Search Tree](src/_DataStructures_/Trees/BST) - [Find kth maximum in a BST](src/_DataStructures_/Trees/BST/find-kth-max) + - [Find kth minimum in a BST](src/_DataStructures_/Trees/BST/find-kth-min) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems diff --git a/src/_DataStructures_/Trees/BST/find-kth-minimum/index.js b/src/_DataStructures_/Trees/BST/find-kth-min/index.js similarity index 100% rename from src/_DataStructures_/Trees/BST/find-kth-minimum/index.js rename to src/_DataStructures_/Trees/BST/find-kth-min/index.js From b4507f155a1ac9effe467d2b4eb9a389363ec6ee Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 16:46:06 +0530 Subject: [PATCH 07/17] update: rename folder and fix condition for K --- src/_DataStructures_/Trees/{BST => BinarySearchTree}/Node.js | 0 .../Trees/{BST => BinarySearchTree}/find-kth-max/index.js | 2 +- .../Trees/{BST => BinarySearchTree}/find-kth-min/index.js | 2 +- src/_DataStructures_/Trees/{BST => BinarySearchTree}/index.js | 0 4 files changed, 2 insertions(+), 2 deletions(-) rename src/_DataStructures_/Trees/{BST => BinarySearchTree}/Node.js (100%) rename src/_DataStructures_/Trees/{BST => BinarySearchTree}/find-kth-max/index.js (96%) rename src/_DataStructures_/Trees/{BST => BinarySearchTree}/find-kth-min/index.js (96%) rename src/_DataStructures_/Trees/{BST => BinarySearchTree}/index.js (100%) diff --git a/src/_DataStructures_/Trees/BST/Node.js b/src/_DataStructures_/Trees/BinarySearchTree/Node.js similarity index 100% rename from src/_DataStructures_/Trees/BST/Node.js rename to src/_DataStructures_/Trees/BinarySearchTree/Node.js diff --git a/src/_DataStructures_/Trees/BST/find-kth-max/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js similarity index 96% rename from src/_DataStructures_/Trees/BST/find-kth-max/index.js rename to src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js index fd798fcd..fb306ca1 100644 --- a/src/_DataStructures_/Trees/BST/find-kth-max/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js @@ -13,7 +13,7 @@ function inOrderTraversal(root) { function findKthMax(rootNode, k) { const arr = inOrderTraversal(rootNode); - if (k < 0 || k> arr.lenth) { + if (k <= 0 || k> arr.lenth) { throw new Error('Invalid value for K'); } return arr[arr.length - k]; diff --git a/src/_DataStructures_/Trees/BST/find-kth-min/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js similarity index 96% rename from src/_DataStructures_/Trees/BST/find-kth-min/index.js rename to src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js index a28115ad..e62468fc 100644 --- a/src/_DataStructures_/Trees/BST/find-kth-min/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js @@ -14,7 +14,7 @@ function inOrderTraversal(root) { function findKthMin(rootNode, k) { const arr = inOrderTraversal(rootNode); - if (k < 0 || k> arr.lenth) { + if (k <= 0 || k> arr.lenth) { throw new Error('Invalid value for K'); } return arr[k - 1]; diff --git a/src/_DataStructures_/Trees/BST/index.js b/src/_DataStructures_/Trees/BinarySearchTree/index.js similarity index 100% rename from src/_DataStructures_/Trees/BST/index.js rename to src/_DataStructures_/Trees/BinarySearchTree/index.js From ab8b0389a4e6448aa266a4d20a10ae79057aaa01 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 16:46:36 +0530 Subject: [PATCH 08/17] update: fix entries in readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 83f38022..545981ac 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,9 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) - [Trees](src/_DataStructures_/Trees) - - [Binary Search Tree](src/_DataStructures_/Trees/BST) - - [Find kth maximum in a BST](src/_DataStructures_/Trees/BST/find-kth-max) - - [Find kth minimum in a BST](src/_DataStructures_/Trees/BST/find-kth-min) + - [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree) + - [Find kth maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max) + - [Find kth minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From 81c88b86256862202501f41716b1aea5859cd5ce Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 19:06:51 +0530 Subject: [PATCH 09/17] update: find all ancestors of a node --- .../BinarySearchTree/find-ancestors/index.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js new file mode 100644 index 00000000..9ae63c08 --- /dev/null +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js @@ -0,0 +1,43 @@ +// eslint-disable-next-line no-unused-vars +const BST = require('../index'); + +function findAncestors(root, value) { + /** + * search the given node and meanwhile + * keep pushing the visited nodes + */ + let arr = []; + if (root === null) return []; + if (value> root.value) { + // traverse right + const left = findAncestors(root.rightChild, value); + arr = [...arr, ...left]; + } + if (value < root.value) { + // traverse left + const right = findAncestors(root.leftChild, value); + arr = [...arr, ...right]; + } + if (root.value === value) return arr; + arr = [root.value, ...arr]; + return arr; +} + +// create a BST +// const myBST = new BST(6); +// myBST.add(4); +// myBST.add(9); +// myBST.add(2); +// myBST.add(5); +// myBST.add(14); +// myBST.add(8); +// myBST.add(12); +// myBST.add(10); + +// // find 3rd max +// // console.log(myBST.root); +// console.log(myBST.traversePreorder()); +// // console.log(myBST.root.rightChild); +// console.log(findAncestors(myBST.root, 10)); + +module.exports = findAncestors; From 252e912b853995b3d28a5982cb4ac8955ea2e039 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 19:07:38 +0530 Subject: [PATCH 10/17] update: entry in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 545981ac..0bb877c0 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree) - [Find kth maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max) - [Find kth minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min) + - [Find Ancestors of a Node](src/_DataStructures_/Trees/BinarySearchTree/find-ancestors) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From c9e3a6a05d2139a425d787b4d7f3def99be6dfd3 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 19:43:45 +0530 Subject: [PATCH 11/17] update: reverse order of pushing nodes --- .../Trees/BinarySearchTree/find-ancestors/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js index 9ae63c08..f316400d 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js @@ -11,15 +11,15 @@ function findAncestors(root, value) { if (value> root.value) { // traverse right const left = findAncestors(root.rightChild, value); - arr = [...arr, ...left]; + arr = [...left, ...arr]; } if (value < root.value) { // traverse left const right = findAncestors(root.leftChild, value); - arr = [...arr, ...right]; + arr = [...right, ...arr]; } if (root.value === value) return arr; - arr = [root.value, ...arr]; + arr = [...arr, root.value]; return arr; } From c4a733a5198816e922bf6845df8b2ede8a52d00f Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 19:59:57 +0530 Subject: [PATCH 12/17] update: height of BST --- .../BinarySearchTree/height-of-bst/index.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js diff --git a/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js b/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js new file mode 100644 index 00000000..88a5d6c5 --- /dev/null +++ b/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js @@ -0,0 +1,35 @@ +// eslint-disable-next-line no-unused-vars +const BST = require('../index'); + +function findHeightOfBST(root) { + let leftHeight = 0; + let rightHeight = 0; + + if (root === null) return 0; + leftHeight = findHeightOfBST(root.leftChild); + rightHeight = findHeightOfBST(root.rightChild); + + if (leftHeight> rightHeight) { + return leftHeight + 1; + } + return rightHeight + 1; +} + +// create a BST +// const myBST = new BST(6); +// myBST.add(4); +// myBST.add(9); +// myBST.add(2); +// myBST.add(5); +// myBST.add(14); +// myBST.add(8); +// myBST.add(12); +// myBST.add(10); + +// // find 3rd max +// // console.log(myBST.root); +// console.log(myBST.traversePreorder()); +// // console.log(myBST.root.rightChild); +// console.log(findHeightOfBST(myBST.root)); + +module.exports = findHeightOfBST; From c95821c55534f929a73116d95cb0d280f36ecdd9 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 20:00:38 +0530 Subject: [PATCH 13/17] update: entry in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0bb877c0..257bf17b 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Find kth maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max) - [Find kth minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min) - [Find Ancestors of a Node](src/_DataStructures_/Trees/BinarySearchTree/find-ancestors) + - [Find Height of BST](src/_DataStructures_/Trees/BinarySearchTree/height-of-bst) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems From a75e9506663a3e9e35ab90488c152cd34936a09c Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 21:38:30 +0530 Subject: [PATCH 14/17] update: Find k nodes from the root --- .../find-k-nodes-from-root/index.js | 31 +++++++++++++++++++ .../BinarySearchTree/find-kth-max/index.js | 1 + .../BinarySearchTree/find-kth-min/index.js | 1 - .../BinarySearchTree/height-of-bst/index.js | 2 -- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js new file mode 100644 index 00000000..79259a00 --- /dev/null +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js @@ -0,0 +1,31 @@ +// eslint-disable-next-line no-unused-vars +const BST = require('../index'); + +function findKNodes(root, k) { + let arr = []; + + if (root === null) return []; + if (k === 0) return [...arr, root.value]; + + const left = findKNodes(root.leftChild, k - 1); + arr = [...arr, ...left]; + + const right = findKNodes(root.rightChild, k - 1); + arr = [...arr, ...right]; + return arr; +} + +// create a BST +// const myBST = new BST(6); + +// myBST.add(2); +// myBST.add(19); +// myBST.add(14); +// myBST.add(8); +// myBST.add(5); +// myBST.add(12); +// myBST.add(33); +// myBST.add(52); +// myBST.add(1); + +// console.log(findKNodes(myBST.root, 2)); diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js index fb306ca1..21ba18f7 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-max/index.js @@ -1,3 +1,4 @@ +// eslint-disable-next-line no-unused-vars const BST = require('../index'); // Inorder traversal returns a sorted array diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js index e62468fc..ad18cdea 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-kth-min/index.js @@ -34,7 +34,6 @@ function findKthMin(rootNode, k) { // myBST.add(1); // myBST.add(0); -// // find 3rd max // console.log(findKthMin(myBST.root, 3)); module.exports = findKthMin; diff --git a/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js b/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js index 88a5d6c5..ad4f1ee7 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js @@ -26,10 +26,8 @@ function findHeightOfBST(root) { // myBST.add(12); // myBST.add(10); -// // find 3rd max // // console.log(myBST.root); // console.log(myBST.traversePreorder()); -// // console.log(myBST.root.rightChild); // console.log(findHeightOfBST(myBST.root)); module.exports = findHeightOfBST; From 6416cfcb7c9a3b94e307432795d4cc3769c21787 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月10日 21:39:29 +0530 Subject: [PATCH 15/17] update: entry in README --- README.md | 1 + .../Trees/BinarySearchTree/find-k-nodes-from-root/index.js | 2 ++ 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index 257bf17b..37708c9e 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Find kth minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min) - [Find Ancestors of a Node](src/_DataStructures_/Trees/BinarySearchTree/find-ancestors) - [Find Height of BST](src/_DataStructures_/Trees/BinarySearchTree/height-of-bst) + - [Find k Nodes from Root of BST](src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root) - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js index 79259a00..25aa70d1 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root/index.js @@ -29,3 +29,5 @@ function findKNodes(root, k) { // myBST.add(1); // console.log(findKNodes(myBST.root, 2)); + +module.exports = findKNodes; From 97f8f9489a0c1a964038168f1b0ad4297c6724e9 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月11日 11:32:28 +0530 Subject: [PATCH 16/17] fix: change in code, return null when node not found --- .../BinarySearchTree/find-ancestors/index.js | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js index f316400d..e45cec7b 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js @@ -1,26 +1,31 @@ // eslint-disable-next-line no-unused-vars const BST = require('../index'); -function findAncestors(root, value) { +function searchAndPush(root, value, result) { /** * search the given node and meanwhile * keep pushing the visited nodes */ - let arr = []; - if (root === null) return []; - if (value> root.value) { - // traverse right - const left = findAncestors(root.rightChild, value); - arr = [...left, ...arr]; + if (root == null) { + return false; + } + if (root.value === value) { + return true; } - if (value < root.value) { - // traverse left - const right = findAncestors(root.leftChild, value); - arr = [...right, ...arr]; + if ( + searchAndPush(root.leftChild, value, result) + || searchAndPush(root.rightChild, value, result) + ) { + result.push(root.value); + return true; } - if (root.value === value) return arr; - arr = [...arr, root.value]; - return arr; + return false; +} + +function findAncestors(root, value) { + const result = []; + searchAndPush(root, value, result); + return result; } // create a BST @@ -34,10 +39,7 @@ function findAncestors(root, value) { // myBST.add(12); // myBST.add(10); -// // find 3rd max -// // console.log(myBST.root); -// console.log(myBST.traversePreorder()); -// // console.log(myBST.root.rightChild); // console.log(findAncestors(myBST.root, 10)); +// console.log(findAncestors(myBST.root, 101)); module.exports = findAncestors; From 28e73b5ae692a7344a8d1978a28589a76be29473 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月11日 12:12:32 +0530 Subject: [PATCH 17/17] update: fix the edge case using array approach --- .../BinarySearchTree/find-ancestors/index.js | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js index e45cec7b..4ccea5f9 100644 --- a/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js +++ b/src/_DataStructures_/Trees/BinarySearchTree/find-ancestors/index.js @@ -1,31 +1,35 @@ // eslint-disable-next-line no-unused-vars const BST = require('../index'); -function searchAndPush(root, value, result) { +/** You should go through this conversation here: + * https://github.com/knaxus/problem-solving-javascript/pull/63 + */ + +function findAncestors(root, value) { /** * search the given node and meanwhile * keep pushing the visited nodes */ - if (root == null) { + if (root === null) return false; + + if (value < root.value) { + const left = findAncestors(root.leftChild, value); + if (left) { + return [...left, root.value]; + } return false; } - if (root.value === value) { - return true; - } - if ( - searchAndPush(root.leftChild, value, result) - || searchAndPush(root.rightChild, value, result) - ) { - result.push(root.value); - return true; + + if (value> root.value) { + const right = findAncestors(root.rightChild, value); + if (right) { + return [...right, root.value]; + } + return false; } - return false; -} -function findAncestors(root, value) { - const result = []; - searchAndPush(root, value, result); - return result; + if (value === root.value) return []; + return false; } // create a BST

AltStyle によって変換されたページ (->オリジナル) /