From c4c5b77420a06798ad0dc85ffc194cf0f4a395b6 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Fri, 30 Mar 2018 21:15:05 +0200 Subject: [PATCH] wrote the tree more object oriented --- Data Structures/Tree/Binary Search Tree.js | 158 +++++++++++---------- 1 file changed, 85 insertions(+), 73 deletions(-) diff --git a/Data Structures/Tree/Binary Search Tree.js b/Data Structures/Tree/Binary Search Tree.js index e9d7ed53a..2cfa73c8f 100644 --- a/Data Structures/Tree/Binary Search Tree.js +++ b/Data Structures/Tree/Binary Search Tree.js @@ -10,87 +10,99 @@ * 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); +// class Node +var Node = (function () { + // Node in the tree + function Node(val) { + this.value = val; + this.left = null; + this.right = null; } - 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) + // 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); } - } else if (n.value > this.value) { - if (this.right == null) { - this.right = n; - } else { - this.right.addNode(n); + 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(); + } + }; -function Tree() { - // Just store the root - this.root = null; -} + // 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); + } + } + }; -// Inorder traversal -Tree.prototype.traverse = function() { - this.root.visit(); -} + // returns the constructor + return Node; +}()); -// 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); - } -} +// class Tree +var Tree = (function () { + 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); + } + }; + + // returns the constructor + return Tree; +}()); //Implementation of BST var bst = new Tree();