From 16281be72f7f55df63e95266451165cd1b6c92ba Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月17日 17:17:46 +0530 Subject: [PATCH 1/3] update: Binary Tree (!BST) added --- src/_DataStructures_/Trees/BinaryTree/Node.js | 7 +++ .../Trees/BinaryTree/index.js | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/_DataStructures_/Trees/BinaryTree/Node.js create mode 100644 src/_DataStructures_/Trees/BinaryTree/index.js diff --git a/src/_DataStructures_/Trees/BinaryTree/Node.js b/src/_DataStructures_/Trees/BinaryTree/Node.js new file mode 100644 index 00000000..2b515b2a --- /dev/null +++ b/src/_DataStructures_/Trees/BinaryTree/Node.js @@ -0,0 +1,7 @@ +module.exports = class Node { + constructor(value) { + this.value = value; + this.leftChild = null; // will be a node + this.rightChild = null; // will be a node + } +}; diff --git a/src/_DataStructures_/Trees/BinaryTree/index.js b/src/_DataStructures_/Trees/BinaryTree/index.js new file mode 100644 index 00000000..0172899a --- /dev/null +++ b/src/_DataStructures_/Trees/BinaryTree/index.js @@ -0,0 +1,51 @@ +const Node = require('./Node'); + +class BinaryTree { + constructor(arr) { + if (!Array.isArray(arr)) { + throw new Error('Invalid argument to create a Binary Tre'); + } + this.root = this.createBinaryTree(this.root, arr, 0); + } + + // eslint-disable-next-line class-methods-use-this + createBinaryTree(root, arr, i) { + if (i < arr.length) { + // eslint-disable-next-line no-param-reassign + root = new Node(arr[i]); + // eslint-disable-next-line no-param-reassign + root.leftChild = this.createBinaryTree(root.leftChild, arr, 2 * i + 1); + // eslint-disable-next-line no-param-reassign + root.rightChild = this.createBinaryTree(root.rightChild, arr, 2 * i + 2); + } + return root; + } + + traversePreorder(root) { + let arr = []; + + if (root === null) return arr; + // push node to arr + arr.push(root.value); + + // push left node + const left = this.traversePreorder(root.leftChild); + arr = [...arr, ...left]; + + // push right node + const right = this.traversePreorder(root.rightChild); + arr = [...arr, ...right]; + + return arr; + } + + preOrder() { + return this.traversePreorder(this.root); + } +} + +// const bt = new BinaryTree([1, 2, 3, 4, 5, 6]); +// console.log(bt.root); +// console.log(bt.preOrder()); + +module.exports = BinaryTree; From 2f39c2687f85a87a399b2e0d816dd5f9bcaa8858 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月17日 17:18:36 +0530 Subject: [PATCH 2/3] update: entry added in Readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dc772666..cbe89b09 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) - [Trees](src/_DataStructures_/Trees) + - [Binary Tree (creation using level order)](src/_DataStructures_/Trees/BinaryTree) - [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) From 580e3ba89d9789bec861af4f2887959db4427040 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月17日 21:38:10 +0530 Subject: [PATCH 3/3] fix: empty array throw error --- src/_DataStructures_/Trees/BinaryTree/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_DataStructures_/Trees/BinaryTree/index.js b/src/_DataStructures_/Trees/BinaryTree/index.js index 0172899a..f30999ad 100644 --- a/src/_DataStructures_/Trees/BinaryTree/index.js +++ b/src/_DataStructures_/Trees/BinaryTree/index.js @@ -2,10 +2,10 @@ const Node = require('./Node'); class BinaryTree { constructor(arr) { - if (!Array.isArray(arr)) { + if (!Array.isArray(arr) || !arr.length) { throw new Error('Invalid argument to create a Binary Tre'); } - this.root = this.createBinaryTree(this.root, arr, 0); + this.root = this.createBinaryTree((this.root = null), arr, 0); } // eslint-disable-next-line class-methods-use-this

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