From f71c10ceef94c376d6fc6f752582d502ef7e5a7f Mon Sep 17 00:00:00 2001 From: "Er. VIKRAM SNEHI" Date: Tue, 20 Oct 2020 05:21:16 +0530 Subject: [PATCH 1/2] Binary Search Tree I am adding One More File, that is BST --- index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000000..b29b23cdda --- /dev/null +++ b/index.js @@ -0,0 +1,49 @@ +// --- Directions +// 1) Implement the Node class to create +// a binary search tree. The constructor +// should initialize values 'data', 'left', +// and 'right'. +// 2) Implement the 'insert' method for the +// Node class. Insert should accept an argument +// 'data', then create an insert a new node +// at the appropriate location in the tree. +// 3) Implement the 'contains' method for the Node +// class. Contains should accept a 'data' argument +// and return the Node in the tree with the same value. +// If the value isn't in the tree return null. + +class Node { + constructor(data) { + this.data = data; + this.left = null; + this.right = null; + } + + insert(data) { + if (data < this.data && this.left) { + this.left.insert(data); + } else if (data < this.data) { + this.left = new Node(data); + } else if (data > this.data && this.right) { + this.right.insert(data); + } else if (data > this.data) { + this.right = new Node(data); + } + } + + contains(data) { + if (this.data === data) { + return this; + } + + if (this.data < data && this.right) { + return this.right.contains(data); + } else if (this.data > data && this.left) { + return this.left.contains(data); + } + + return null; + } +} + +module.exports = Node; From 0bb6ca6a2d7837ff7b4edb2d60dafcacb361ba13 Mon Sep 17 00:00:00 2001 From: "Er. VIKRAM SNEHI" Date: Tue, 20 Oct 2020 05:22:15 +0530 Subject: [PATCH 2/2] Rename index.js to BinarySearchTree.js Updating Binary Search Tree. --- index.js => BinarySearchTree.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename index.js => BinarySearchTree.js (100%) diff --git a/index.js b/BinarySearchTree.js similarity index 100% rename from index.js rename to BinarySearchTree.js