mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
wrote the tree more object oriented
This commit is contained in:
@ -10,15 +10,17 @@
|
||||
* bigger.
|
||||
*/
|
||||
|
||||
// Node in the tree
|
||||
function Node(val) {
|
||||
// class Node
|
||||
var Node = (function () {
|
||||
// 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) {
|
||||
// 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) {
|
||||
@ -27,10 +29,10 @@ Node.prototype.search = function(val) {
|
||||
return this.right.search(val);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// Visit a node
|
||||
Node.prototype.visit = function() {
|
||||
// Visit a node
|
||||
Node.prototype.visit = function () {
|
||||
// Recursively go left
|
||||
if (this.left != null) {
|
||||
this.left.visit();
|
||||
@ -41,10 +43,10 @@ Node.prototype.visit = function() {
|
||||
if (this.right != null) {
|
||||
this.right.visit();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Add a node
|
||||
Node.prototype.addNode = function(n) {
|
||||
// Add a node
|
||||
Node.prototype.addNode = function (n) {
|
||||
if (n.value < this.value) {
|
||||
if (this.left == null) {
|
||||
this.left = n;
|
||||
@ -58,39 +60,49 @@ Node.prototype.addNode = function(n) {
|
||||
this.right.addNode(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function Tree() {
|
||||
// returns the constructor
|
||||
return Node;
|
||||
}());
|
||||
|
||||
|
||||
// class Tree
|
||||
var Tree = (function () {
|
||||
function Tree() {
|
||||
// Just store the root
|
||||
this.root = null;
|
||||
}
|
||||
};
|
||||
|
||||
// Inorder traversal
|
||||
Tree.prototype.traverse = function() {
|
||||
// Inorder traversal
|
||||
Tree.prototype.traverse = function () {
|
||||
this.root.visit();
|
||||
}
|
||||
};
|
||||
|
||||
// Start by searching the root
|
||||
Tree.prototype.search = function(val) {
|
||||
// Start by searching the root
|
||||
Tree.prototype.search = function (val) {
|
||||
var found = this.root.search(val);
|
||||
if(found === null)
|
||||
{
|
||||
if (found === null) {
|
||||
console.log(val + " not found");
|
||||
}
|
||||
else{
|
||||
console.log("Found:"+found.value);
|
||||
else {
|
||||
console.log("Found:" + found.value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Add a new value to the tree
|
||||
Tree.prototype.addValue = function(val) {
|
||||
// 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();
|
||||
|
Reference in New Issue
Block a user