From 1dc95554358a5c5e6896acb95164d5416768712e Mon Sep 17 00:00:00 2001 From: Mohit Sharma Date: Fri, 4 Aug 2017 18:04:20 +0530 Subject: [PATCH 1/3] Added Implementation of BST --- Data Structures/Tree/bst.js | 98 +++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 Data Structures/Tree/bst.js diff --git a/Data Structures/Tree/bst.js b/Data Structures/Tree/bst.js new file mode 100644 index 000000000..ed96234bd --- /dev/null +++ b/Data Structures/Tree/bst.js @@ -0,0 +1,98 @@ +/*Binary Search Tree!! +* +* Nodes that will go on the Binary Tree. +* They consist of the data in them, the node to the left, the node +* to the right, and the parent from which they came from. +* +* A binary tree is a data structure in which an element +* has two successors(children). The left child is usually +* smaller than the parent, and the right child is usually +* bigger. +*/ + +// Node in the tree +function Node(val) { + this.value = val; + this.left = null; + this.right = null; +} + +// Search the tree for a value +Node.prototype.search = function(val) { + if (this.value == val) { + return this; + } else if (val < this.value && this.left != null) { + return this.left.search(val); + } else if (val > this.value && this.right != null) { + return this.right.search(val); + } + return null; +} + +// Visit a node +Node.prototype.visit = function() { + // Recursively go left + if (this.left != null) { + this.left.visit(); + } + // Print out value + console.log(this.value); + // Recursively go right + if (this.right != null) { + this.right.visit(); + } +} + +// Add a node +Node.prototype.addNode = function(n) { + if (n.value < this.value) { + if (this.left == null) { + this.left = n; + } else { + this.left.addNode(n) + } + } else if (n.value > this.value) { + if (this.right == null) { + this.right = n; + } else { + this.right.addNode(n); + } + } +} + +function Tree() { + // Just store the root + this.root = null; +} + +// Inorder traversal +Tree.prototype.traverse = function() { + this.root.visit(); +} + +// Start by searching the root +Tree.prototype.search = function(val) { + var found = this.root.search(val); + console.log("Found:"+found.value); +} + +// Add a new value to the tree +Tree.prototype.addValue = function(val) { + var n = new Node(val); + if (this.root == null) { + this.root = n; + } else { + this.root.addNode(n); + } +} + +//Implementation of BST +var bst = new Tree(); +bst.addValue(6); +bst.addValue(3); +bst.addValue(9); +bst.addValue(2); +bst.addValue(8); +bst.addValue(4); +bst.traverse(); +bst.search(8); From fa5d148d90ed7e7b7a138cbec74d7f732ca7cdc0 Mon Sep 17 00:00:00 2001 From: Mohit Sharma Date: Sat, 5 Aug 2017 23:42:21 +0530 Subject: [PATCH 2/3] Delete bst.js --- Data Structures/Tree/bst.js | 98 ------------------------------------- 1 file changed, 98 deletions(-) delete mode 100644 Data Structures/Tree/bst.js diff --git a/Data Structures/Tree/bst.js b/Data Structures/Tree/bst.js deleted file mode 100644 index ed96234bd..000000000 --- a/Data Structures/Tree/bst.js +++ /dev/null @@ -1,98 +0,0 @@ -/*Binary Search Tree!! -* -* Nodes that will go on the Binary Tree. -* They consist of the data in them, the node to the left, the node -* to the right, and the parent from which they came from. -* -* A binary tree is a data structure in which an element -* has two successors(children). The left child is usually -* smaller than the parent, and the right child is usually -* bigger. -*/ - -// Node in the tree -function Node(val) { - this.value = val; - this.left = null; - this.right = null; -} - -// Search the tree for a value -Node.prototype.search = function(val) { - if (this.value == val) { - return this; - } else if (val < this.value && this.left != null) { - return this.left.search(val); - } else if (val > this.value && this.right != null) { - return this.right.search(val); - } - return null; -} - -// Visit a node -Node.prototype.visit = function() { - // Recursively go left - if (this.left != null) { - this.left.visit(); - } - // Print out value - console.log(this.value); - // Recursively go right - if (this.right != null) { - this.right.visit(); - } -} - -// Add a node -Node.prototype.addNode = function(n) { - if (n.value < this.value) { - if (this.left == null) { - this.left = n; - } else { - this.left.addNode(n) - } - } else if (n.value > this.value) { - if (this.right == null) { - this.right = n; - } else { - this.right.addNode(n); - } - } -} - -function Tree() { - // Just store the root - this.root = null; -} - -// Inorder traversal -Tree.prototype.traverse = function() { - this.root.visit(); -} - -// Start by searching the root -Tree.prototype.search = function(val) { - var found = this.root.search(val); - console.log("Found:"+found.value); -} - -// Add a new value to the tree -Tree.prototype.addValue = function(val) { - var n = new Node(val); - if (this.root == null) { - this.root = n; - } else { - this.root.addNode(n); - } -} - -//Implementation of BST -var bst = new Tree(); -bst.addValue(6); -bst.addValue(3); -bst.addValue(9); -bst.addValue(2); -bst.addValue(8); -bst.addValue(4); -bst.traverse(); -bst.search(8); From ff7464dbc1ecbdf56b57e8fc2eac30d86f2bf98f Mon Sep 17 00:00:00 2001 From: Mohit Sharma Date: Sat, 5 Aug 2017 23:53:36 +0530 Subject: [PATCH 3/3] Added Implementation of BST --- Data Structures/Tree/Binary Search Tree.js | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Data Structures/Tree/Binary Search Tree.js diff --git a/Data Structures/Tree/Binary Search Tree.js b/Data Structures/Tree/Binary Search Tree.js new file mode 100644 index 000000000..e9d7ed53a --- /dev/null +++ b/Data Structures/Tree/Binary Search Tree.js @@ -0,0 +1,104 @@ +/*Binary Search Tree!! +* +* Nodes that will go on the Binary Tree. +* They consist of the data in them, the node to the left, the node +* to the right, and the parent from which they came from. +* +* A binary tree is a data structure in which an element +* has two successors(children). The left child is usually +* smaller than the parent, and the right child is usually +* bigger. +*/ + +// Node in the tree +function Node(val) { + this.value = val; + this.left = null; + this.right = null; +} + +// Search the tree for a value +Node.prototype.search = function(val) { + if (this.value == val) { + return this; + } else if (val < this.value && this.left != null) { + return this.left.search(val); + } else if (val > this.value && this.right != null) { + return this.right.search(val); + } + return null; +} + +// Visit a node +Node.prototype.visit = function() { + // Recursively go left + if (this.left != null) { + this.left.visit(); + } + // Print out value + console.log(this.value); + // Recursively go right + if (this.right != null) { + this.right.visit(); + } +} + +// Add a node +Node.prototype.addNode = function(n) { + if (n.value < this.value) { + if (this.left == null) { + this.left = n; + } else { + this.left.addNode(n) + } + } else if (n.value > this.value) { + if (this.right == null) { + this.right = n; + } else { + this.right.addNode(n); + } + } +} + +function Tree() { + // Just store the root + this.root = null; +} + +// Inorder traversal +Tree.prototype.traverse = function() { + this.root.visit(); +} + +// Start by searching the root +Tree.prototype.search = function(val) { + var found = this.root.search(val); + if(found === null) + { + console.log(val + " not found"); + } + else{ + console.log("Found:"+found.value); + } +} + +// Add a new value to the tree +Tree.prototype.addValue = function(val) { + var n = new Node(val); + if (this.root == null) { + this.root = n; + } else { + this.root.addNode(n); + } +} + +//Implementation of BST +var bst = new Tree(); +bst.addValue(6); +bst.addValue(3); +bst.addValue(9); +bst.addValue(2); +bst.addValue(8); +bst.addValue(4); +bst.traverse(); +bst.search(8);