-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonEmptyBST.js
52 lines (52 loc) · 1.67 KB
/
NonEmptyBST.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EmptyBST_1 = require("./EmptyBST");
/**
* A class that rappresent a non-empty Binary Tree
*/
var NonEmptyBST = /** @class */ (function () {
function NonEmptyBST(elem, leftTree, rightTree) {
this.root = elem;
this.left = leftTree || new EmptyBST_1.default();
this.right = rightTree || new EmptyBST_1.default();
}
NonEmptyBST.prototype.isEmpty = function () {
return false;
};
NonEmptyBST.prototype.cardinality = function () {
return 1 + this.left.cardinality() + this.right.cardinality();
};
NonEmptyBST.prototype.search = function (elem) {
if (this.root === elem) {
return true;
}
else {
if (elem < this.root) {
return this.left.search(elem);
}
else {
return this.right.search(elem);
}
}
};
NonEmptyBST.prototype.insert = function (elem) {
if (elem < this.root) {
return new NonEmptyBST(this.root, this.left.insert(elem), this.right);
}
else {
return new NonEmptyBST(this.root, this.left, this.right.insert(elem));
}
};
NonEmptyBST.prototype.inOrderTraverse = function (callback) {
this.left.inOrderTraverse(callback);
callback(this.root);
this.right.inOrderTraverse(callback);
};
NonEmptyBST.prototype.preOrderTraverse = function (callback) {
this.right.preOrderTraverse(callback);
callback(this.root);
this.left.preOrderTraverse(callback);
};
return NonEmptyBST;
}());
exports.default = NonEmptyBST;