Skip to content

Commit 3d86b4a

Browse files
committed
BST init 👗
1 parent 9fde159 commit 3d86b4a

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

examples/datastructures/bst.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Node {
2+
constructor(data, left, right) {
3+
this.data = this.data;
4+
this.left = this.left;
5+
this.right = this.right;
6+
}
7+
8+
show() {
9+
return this.data;
10+
}
11+
}
12+
13+
class BST {
14+
constructor() {
15+
this.root = null;
16+
}
17+
18+
insert(data) {
19+
const node = new Node(data,null,null);
20+
const temp = this.root;
21+
if(temp == null) {
22+
temp = node;
23+
return;
24+
}
25+
while(data < temp.data)
26+
temp = temp.left;
27+
while(data > temp.data)
28+
temp = temp.right;
29+
if(data < temp.data)
30+
temp.left = node;
31+
else temp.right = node;
32+
}
33+
34+
inorder(node) {
35+
if(node != null) {
36+
inorder(node.left);
37+
putstr(node.show() + " ");
38+
inorder(node.right);
39+
}
40+
}
41+
42+
preorder(node) {
43+
if(node != null) {
44+
putstr(node.show() + " ");
45+
inorder(node.left);
46+
inorder(node.right);
47+
}
48+
}
49+
50+
postorder(node) {
51+
if(node != null) {
52+
inorder(node.left);
53+
inorder(node.right);
54+
putstr(node.show() + " ");
55+
}
56+
}
57+
58+
}
59+
60+
module.exports = BST;

test/datastructures/bst.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const expect = require('chai').expect;
2+
const BST = require('../../examples/datastructures/bst');
3+
4+
describe('=> DICTIONARY', function() {
5+
let numbers;
6+
before(function () {
7+
numbers = new BST();
8+
});
9+
10+
it('should create an Empty BST', function(done) {
11+
expect(numbers.root).to.equal(null);
12+
done();
13+
});
14+
15+
it('should insert Nodes in the BST', function(done) {
16+
numbers.insert(23);
17+
numbers.insert(45);
18+
numbers.insert(16);
19+
numbers.insert(37);
20+
numbers.insert(3);
21+
numbers.insert(99);
22+
numbers.insert(22);
23+
expect(numbers.inorder(numbers.root)).to.equal("3 16 22 23 37 45 99");
24+
done();
25+
});
26+
27+
});

test/usecases/radixSort.test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)