8000 added BFS, DFSPostOrder, DFSPreOrder and DFSInOrder algorithms as cla… · AllAlgorithms/javascript@46fd985 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46fd985

Browse files
XenomShoxabranhe
authored andcommitted
added BFS, DFSPostOrder, DFSPreOrder and DFSInOrder algorithms as class mthods, all of them return array representaions
1 parent b13781b commit 46fd985

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

data-structures/BinaryTree.js

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class BinarySearchTree {
1010
constructor() {
1111
this.root = null;
1212
}
13+
1314
insert(value) {
1415
var newNode = new Node(value);
1516
if (this.root === null) {
@@ -34,6 +35,7 @@ class BinarySearchTree {
3435
}
3536
}
3637
}
38+
3739
find(value) {
3840
if (this.root === null) return false;
3941
var current = this.root,
@@ -50,6 +52,7 @@ class BinarySearchTree {
5052
if (!found) return undefined;
5153
return current;
5254
}
55+
5356
contains(value) {
5457
if (this.root === null) return false;
5558
var current = this.root,
@@ -61,6 +64,53 @@ class BinarySearchTree {
6164
}
6265
return false;
6366
}
67+
68+
BreadthFirstSearch() {
69+
let node = this.root,
70+
data = [],
71+
queue = [];
72+
queue.push(node);
73+
74+
while (queue.length) {
75+
node = queue.shift();
76+
data.push(node.value);
77+
if (node.left) queue.push(node.left);
78+
if (node.right) queue.push(node.right);
79+
}
80+
81+
return data;
82+
}
83+
84+
DepthFirstSearchPreOrder() {
85+
let data = [];
86+
function traverse(node) {
87+
data.push(node.value);
88+
if (node.left) traverse(node.left);
89+
if (node.right) traverse(node.right);
90+
}
91+
traverse(this.root);
92+
return data;
93+
}
94+
DepthFirstSearchPostOrder() {
95+
let data = [];
96+
function traverse(node) {
97+
if (node.left) traverse(node.left);
98+
if (node.right) traverse(node.right);
99+
data.push(node.value);
100+
}
101+
traverse(this.root);
102+
return data;
103+
}
104+
DepthFirstSearchInOrder() {
105+
let data = [];
106+
function traverse(node) {
107+
if (node.left) traverse(node.left);
108+
data.push(node.value);
109+
if (node.right) traverse(node.right);
110+
}
111+
traverse(this.root);
112+
return data;
113+
}
64114
}
65115

66116
// 10
@@ -76,8 +126,13 @@ tree.insert(2);
76126
tree.insert(16);
77127
tree.insert(7);
78128

79-
console.log(tree.find(13));
80-
console.log(tree.find(19));
129+
console.log("node with value 13:", tree.find(13));
130+
console.log("node with value 19:", tree.find(19));
131+
132+
console.log("tree coontais node with value 2:", tree.contains(2));
133+
console.log("tree coontais node with value 534:", tree.contains(534));
81134

82-
console.log(tree.contains(2));
83-
console.log(tree.contains(534));
135+
console.log("BreadthFirstSearch:", tree.BreadthFirstSearch());
136+
console.log("DepthFirstSearchPreOrder:", tree.DepthFirstSearchPreOrder());
137+
console.log("DepthFirstSearchPostOrder:", tree.DepthFirstSearchPostOrder());
138+
console.log("DepthFirstSearchInOrder:", tree.DepthFirstSearchInOrder());

0 commit comments

Comments
 (0)
0