Skip to content

Commit 41f5d65

Browse files
committed
Fixes in the rotations
1 parent d861d43 commit 41f5d65

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,35 @@
22

33
'use strict';
44

5-
function Node(key, value, left, right, isRed) {
5+
var Colors = {
6+
RED: 0,
7+
BLACK: 1
8+
};
9+
10+
global.Colors = Colors;
11+
12+
13+
function Node(key, value, left, right, color) {
614
this._key = key;
715
this._left = left;
816
this._right = right;
917
this._value = value;
10-
this._isRed = isRed;
18+
this._color = color;
1119
}
1220

1321
Node.prototype.isRed = function () {
14-
return !!this._isRed;
22+
return this._color === Colors.RED;
1523
};
1624

1725
Node.prototype.flipColor = function () {
18-
this._isRed = !this._isRed;
26+
if (this._color === Colors.RED) {
27+
this._color = Colors.BLACK;
28+
} else {
29+
this._color = Colors.RED;
30+
}
1931
};
2032

21-
'key value left right'
33+
'key value left right color'
2234
.split(' ')
2335
.forEach(function (key) {
2436
var valueName = key.substr(0, 1).toUpperCase() + key.substr(1, key.length);
@@ -39,7 +51,7 @@
3951

4052
RBTree.prototype.put = function (key, value) {
4153
this._root = this._put(key, value, this._root);
42-
this._root.flipColor();
54+
this._root.setColor(Colors.BLACK);
4355
};
4456

4557
RBTree.prototype.isRed = function (node) {
@@ -52,7 +64,7 @@
5264
RBTree.prototype._put = function (key, value, node) {
5365
var newRoot = node;
5466
if (node === null) {
55-
return new Node(key, value, null, null, true);
67+
return new Node(key, value, null, null, Colors.RED);
5668
}
5769
if (node.getKey() > key) {
5870
node._left = this._put(key, value, node._left);
@@ -82,6 +94,8 @@
8294
var temp = x._left;
8395
node.setRight(temp);
8496
x._left = node;
97+
x.setColor(node.getColor());
98+
node.setColor(Colors.RED);
8599
}
86100
return x;
87101
};
@@ -92,6 +106,8 @@
92106
var temp = x._right;
93107
node._left = temp;
94108
x._right = node;
109+
x.setColor(node.getColor());
110+
node.setColor(Colors.RED);
95111
}
96112
return x;
97113
};

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
var mod = require('../../src/data-structures/red-black-tree.js'),
44
Node = mod.Node,
5-
RBTree = mod.RBTree;
5+
RBTree = mod.RBTree,
6+
Colors = mod.Colors;
67

78
describe('Node', function () {
89

@@ -11,15 +12,12 @@ describe('Node', function () {
1112
});
1213

1314
it('should set all properties via the constructor', function () {
14-
var node = new Node('key', 'value', 1, 2, true);
15+
var node = new Node('key', 'value', 1, 2, Colors.RED);
1516
expect(node.getKey()).toBe('key');
1617
expect(node.getLeft()).toBe(1);
1718
expect(node.getRight()).toBe(2);
1819
expect(node.getValue()).toBe('value');
1920
expect(node.isRed()).toBeTruthy();
20-
node = new Node('key', 'value', null, null, undefined);
21-
//if we set isRed to falcy it should be turned to red
22-
expect(node.isRed()).toBe(false);
2321
});
2422

2523
describe('Node flipColor', function () {
@@ -53,6 +51,7 @@ describe('RBTree', function () {
5351
expect(tree._root.getKey()).toBe('foo');
5452
expect(tree._root.getValue()).toBe('bar');
5553
});
54+
5655
it('should be able to insert a node in 1 level tree', function () {
5756
var tree = new RBTree();
5857
tree.put(1, 'bar');
@@ -62,7 +61,16 @@ describe('RBTree', function () {
6261
tree.put(2, 'baz');
6362
expect(tree._root._right).not.toBeNull();
6463
expect(tree._root._right._isRed).toBeFalsy();
64+
65+
tree = new RBTree();
66+
tree.put(1, 'bar');
67+
tree.put(2, 'bar');
68+
tree.put(3, 'bar');
69+
tree.put(4, 'bar');
70+
console.log(tree._root);
71+
// expect(tree._root._right._isRed).toBeFalsy();
6572
});
73+
6674
});
6775

6876
});

0 commit comments

Comments
 (0)