Skip to content

Commit c4c5b77

Browse files
author
Christian Bender
committed
wrote the tree more object oriented
1 parent b9d749a commit c4c5b77

File tree

1 file changed

+84
-72
lines changed

1 file changed

+84
-72
lines changed

Data Structures/Tree/Binary Search Tree.js

Lines changed: 84 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,87 +10,99 @@
1010
* bigger.
1111
*/
1212

13-
// Node in the tree
14-
function Node(val) {
15-
this.value = val;
16-
this.left = null;
17-
this.right = null;
18-
}
19-
20-
// Search the tree for a value
21-
Node.prototype.search = function(val) {
22-
if (this.value == val) {
23-
return this;
24-
} else if (val < this.value && this.left != null) {
25-
return this.left.search(val);
26-
} else if (val > this.value && this.right != null) {
27-
return this.right.search(val);
13+
// class Node
14+
var Node = (function () {
15+
// Node in the tree
16+
function Node(val) {
17+
this.value = val;
18+
this.left = null;
19+
this.right = null;
2820
}
29-
return null;
30-
}
3121

32-
// Visit a node
33-
Node.prototype.visit = function() {
34-
// Recursively go left
35-
if (this.left != null) {
36-
this.left.visit();
37-
}
38-
// Print out value
39-
console.log(this.value);
40-
// Recursively go right
41-
if (this.right != null) {
42-
this.right.visit();
43-
}
44-
}
22+
// Search the tree for a value
23+
Node.prototype.search = function (val) {
24+
if (this.value == val) {
25+
return this;
26+
} else if (val < this.value && this.left != null) {
27+
return this.left.search(val);
28+
} else if (val > this.value && this.right != null) {
29+
return this.right.search(val);
30+
}
31+
return null;
32+
};
4533

46-
// Add a node
47-
Node.prototype.addNode = function(n) {
48-
if (n.value < this.value) {
49-
if (this.left == null) {
50-
this.left = n;
51-
} else {
52-
this.left.addNode(n)
34+
// Visit a node
35+
Node.prototype.visit = function () {
36+
// Recursively go left
37+
if (this.left != null) {
38+
this.left.visit();
5339
}
54-
} else if (n.value > this.value) {
55-
if (this.right == null) {
56-
this.right = n;
57-
} else {
58-
this.right.addNode(n);
40+
// Print out value
41+
console.log(this.value);
42+
// Recursively go right
43+
if (this.right != null) {
44+
this.right.visit();
5945
}
60-
}
61-
}
46+
};
6247

63-
function Tree() {
64-
// Just store the root
65-
this.root = null;
66-
}
48+
// Add a node
49+
Node.prototype.addNode = function (n) {
50+
if (n.value < this.value) {
51+
if (this.left == null) {
52+
this.left = n;
53+
} else {
54+
this.left.addNode(n)
55+
}
56+
} else if (n.value > this.value) {
57+
if (this.right == null) {
58+
this.right = n;
59+
} else {
60+
this.right.addNode(n);
61+
}
62+
}
63+
};
6764

68-
// Inorder traversal
69-
Tree.prototype.traverse = function() {
70-
this.root.visit();
71-
}
65+
// returns the constructor
66+
return Node;
67+
}());
7268

73-
// Start by searching the root
74-
Tree.prototype.search = function(val) {
75-
var found = this.root.search(val);
76-
if(found === null)
77-
{
78-
console.log(val + " not found");
79-
}
80-
else{
81-
console.log("Found:"+found.value);
82-
}
83-
}
8469

85-
// Add a new value to the tree
86-
Tree.prototype.addValue = function(val) {
87-
var n = new Node(val);
88-
if (this.root == null) {
89-
this.root = n;
90-
} else {
91-
this.root.addNode(n);
92-
}
93-
}
70+
// class Tree
71+
var Tree = (function () {
72+
function Tree() {
73+
// Just store the root
74+
this.root = null;
75+
};
76+
77+
// Inorder traversal
78+
Tree.prototype.traverse = function () {
79+
this.root.visit();
80+
};
81+
82+
// Start by searching the root
83+
Tree.prototype.search = function (val) {
84+
var found = this.root.search(val);
85+
if (found === null) {
86+
console.log(val + " not found");
87+
}
88+
else {
89+
console.log("Found:" + found.value);
90+
}
91+
};
92+
93+
// Add a new value to the tree
94+
Tree.prototype.addValue = function (val) {
95+
var n = new Node(val);
96+
if (this.root == null) {
97+
this.root = n;
98+
} else {
99+
this.root.addNode(n);
100+
}
101+
};
102+
103+
// returns the constructor
104+
return Tree;
105+
}());
94106

95107
//Implementation of BST
96108
var bst = new Tree();

0 commit comments

Comments
 (0)