mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-08 03:45:23 +08:00
wrote the tree more object oriented
This commit is contained in:
@ -10,6 +10,8 @@
|
|||||||
* bigger.
|
* bigger.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// class Node
|
||||||
|
var Node = (function () {
|
||||||
// Node in the tree
|
// Node in the tree
|
||||||
function Node(val) {
|
function Node(val) {
|
||||||
this.value = val;
|
this.value = val;
|
||||||
@ -27,7 +29,7 @@ Node.prototype.search = function(val) {
|
|||||||
return this.right.search(val);
|
return this.right.search(val);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
};
|
||||||
|
|
||||||
// Visit a node
|
// Visit a node
|
||||||
Node.prototype.visit = function () {
|
Node.prototype.visit = function () {
|
||||||
@ -41,7 +43,7 @@ Node.prototype.visit = function() {
|
|||||||
if (this.right != null) {
|
if (this.right != null) {
|
||||||
this.right.visit();
|
this.right.visit();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// Add a node
|
// Add a node
|
||||||
Node.prototype.addNode = function (n) {
|
Node.prototype.addNode = function (n) {
|
||||||
@ -58,29 +60,35 @@ Node.prototype.addNode = function(n) {
|
|||||||
this.right.addNode(n);
|
this.right.addNode(n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
// returns the constructor
|
||||||
|
return Node;
|
||||||
|
}());
|
||||||
|
|
||||||
|
|
||||||
|
// class Tree
|
||||||
|
var Tree = (function () {
|
||||||
function Tree() {
|
function Tree() {
|
||||||
// Just store the root
|
// Just store the root
|
||||||
this.root = null;
|
this.root = null;
|
||||||
}
|
};
|
||||||
|
|
||||||
// Inorder traversal
|
// Inorder traversal
|
||||||
Tree.prototype.traverse = function () {
|
Tree.prototype.traverse = function () {
|
||||||
this.root.visit();
|
this.root.visit();
|
||||||
}
|
};
|
||||||
|
|
||||||
// Start by searching the root
|
// Start by searching the root
|
||||||
Tree.prototype.search = function (val) {
|
Tree.prototype.search = function (val) {
|
||||||
var found = this.root.search(val);
|
var found = this.root.search(val);
|
||||||
if(found === null)
|
if (found === null) {
|
||||||
{
|
|
||||||
console.log(val + " not found");
|
console.log(val + " not found");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log("Found:" + found.value);
|
console.log("Found:" + found.value);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// Add a new value to the tree
|
// Add a new value to the tree
|
||||||
Tree.prototype.addValue = function (val) {
|
Tree.prototype.addValue = function (val) {
|
||||||
@ -90,7 +98,11 @@ Tree.prototype.addValue = function(val) {
|
|||||||
} else {
|
} else {
|
||||||
this.root.addNode(n);
|
this.root.addNode(n);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
// returns the constructor
|
||||||
|
return Tree;
|
||||||
|
}());
|
||||||
|
|
||||||
//Implementation of BST
|
//Implementation of BST
|
||||||
var bst = new Tree();
|
var bst = new Tree();
|
||||||
|
Reference in New Issue
Block a user