Skip to content

Commit ad7984b

Browse files
committed
Change direct property access instead of set/get methods
1 parent 3f4407c commit ad7984b

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/data-structures/red-black-tree.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,47 +59,57 @@
5959
return new Node(key, value, null, null, true);
6060
}
6161
if (node.getKey() > key) {
62-
node.setLeft(this._put(key, value, node.getLeft()));
62+
node._left = this._put(key, value, node._left);
6363
} else if (node.getKey() < key) {
64-
node.setRight(this._put(key, value, node.getRight()));
64+
node._right = this._put(key, value, node._right);
6565
}
66-
if (this.isRed(node.getRight()) && !this.isRed(node.getLeft())) {
66+
if (this.isRed(node._right) && !this.isRed(node._left)) {
6767
newRoot = this._rotateLeft(node);
6868
}
69-
if (this.isRed(node.getLeft()) && this.isRed(node.getLeft().getLeft())) {
69+
if (this.isRed(node._left) && this.isRed(node._left._left)) {
7070
newRoot = this._rotateRight(node);
7171
}
72-
if (this.isRed(node.getLeft()) && this.isRed(node.getRight())) {
72+
if (this.isRed(node._left) && this.isRed(node._right)) {
7373
this._flipColors(node);
7474
}
7575
return newRoot;
7676
};
7777

7878
RBTree.prototype._flipColors = function (node) {
79-
node.getLeft().flipColor();
80-
node.getRight().flipColor();
79+
node._left.flipColor();
80+
node._right.flipColor();
8181
};
8282

8383
RBTree.prototype._rotateLeft = function (node) {
84-
var x = node.getRight();
84+
var x = node._right;
8585
if (x !== null) {
86-
var temp = x.getLeft();
86+
var temp = x._left;
8787
node.setRight(temp);
88-
x.setLeft(node);
88+
x._left = node;
8989
}
9090
return x;
9191
};
9292

9393
RBTree.prototype._rotateRight = function (node) {
94-
var x = node.getLeft();
94+
var x = node._left;
9595
if (x !== null) {
96-
var temp = x.getRight();
97-
node.setLeft(temp);
98-
x.setRight(node);
96+
var temp = x._right;
97+
node._left = temp;
98+
x._right = node;
9999
}
100100
return x;
101101
};
102102

103+
RBTree.prototype.getIterator = function () {
104+
return new RBTIterator(this);
105+
};
106+
107+
function RBTIterator(tree) {
108+
this._tree = tree;
109+
}
110+
103111
global.RBTree = RBTree;
104112

113+
114+
105115
}(typeof window === 'undefined' ? module.exports : window));

test/data-structures/red-black-tree.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ describe('RBTree', function () {
5353
expect(tree._root.getKey()).toBe('foo');
5454
expect(tree._root.getValue()).toBe('bar');
5555
});
56-
5756
it('should be able to insert a node in 1 level tree', function () {
5857
var tree = new RBTree();
5958
tree.put(1, 'bar');
@@ -64,7 +63,6 @@ describe('RBTree', function () {
6463
expect(tree._root._right).not.toBeNull();
6564
expect(tree._root._right._isRed).toBeFalsy();
6665
});
67-
6866
});
6967

7068
});

0 commit comments

Comments
 (0)