Skip to content

Commit 2c65a99

Browse files
committed
Add RedBlackTree
1 parent 8e84d7b commit 2c65a99

9 files changed

+619
-113
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
"karma-jasmine": "1.0.2",
2626
"karma-requirejs": "1.1.0",
2727
"requirejs": "2.3.2",
28-
"typescript": "2.0.6"
28+
"typescript": "2.1.4"
2929
}
3030
}

tests/BinaryTreeSpec.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import {BinaryTree} from "../ts/BinaryTree";
1+
import {BinaryTree} from "../ts/BinaryTree/BinaryTree";
22

33
describe('BinaryTree', function () {
44
describe('add' , function () {
55
it('should be able to add node', function () {
6-
var tree = new BinaryTree<number>();
6+
const tree = new BinaryTree<number>();
77
tree.add(2);
88
expect(tree.root.value).toEqual(2);
99
tree.add(1);
1010
expect(tree.root.left.value).toEqual(1);
1111
});
1212
it('should be able to add node', function () {
13-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
13+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
1414
tree.add(5);
1515
expect(tree.root.value).toEqual(5);
1616
tree.add(6);
@@ -21,7 +21,7 @@ describe('BinaryTree', function () {
2121
});
2222

2323
it('should be able to add node to binary tree with object values', function () {
24-
var tree = new BinaryTree<any>(function (a,b) {return a.amount > b.amount;});
24+
const tree = new BinaryTree<any>(function (a,b) {return a.amount > b.amount;});
2525
tree.add({amount : 5});
2626
expect(tree.root.value).toEqual({amount : 5});
2727
tree.add({amount : 6});
@@ -31,7 +31,7 @@ describe('BinaryTree', function () {
3131
});
3232

3333
it('should be able to walk the binary tree in order', function () {
34-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
34+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
3535
tree.add(5);
3636
tree.add(6);
3737
tree.add(4);
@@ -44,7 +44,7 @@ describe('BinaryTree', function () {
4444
});
4545

4646
it('should be able to walk the binary tree that has object values in order', function () {
47-
var tree = new BinaryTree<any>(function (a,b) {return a.amount > b.amount;});
47+
const tree = new BinaryTree<any>(function (a,b) {return a.amount > b.amount;});
4848
tree.add({amount: 6});
4949
tree.add({amount: 4});
5050
tree.add({amount: 5});
@@ -58,7 +58,7 @@ describe('BinaryTree', function () {
5858

5959

6060
it('should be able to walk the binary tree in order', function () {
61-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
61+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
6262
tree.add(5);
6363
tree.add(6);
6464
tree.add(4);
@@ -71,7 +71,7 @@ describe('BinaryTree', function () {
7171
});
7272

7373
it('should be able to walk the binary tree in reverseorder', function () {
74-
var tree = new BinaryTree<any>(function (a,b) {return a.amount > b.amount;});
74+
const tree = new BinaryTree<any>(function (a,b) {return a.amount > b.amount;});
7575
tree.add({amount: 6});
7676
tree.add({amount: 4});
7777
tree.add({amount: 5});
@@ -85,20 +85,20 @@ describe('BinaryTree', function () {
8585

8686
describe(' search', function () {
8787
it('should return null when no root', function () {
88-
var tree = new BinaryTree<any>(function (a,b) {return a > b;});
88+
const tree = new BinaryTree<any>(function (a,b) {return a > b;});
8989
expect(tree.search(5)).toBeNull();
9090
});
9191

9292
it('should return element when found', function () {
93-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
93+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
9494
tree.add(5);
9595
expect(tree.search(5).value).toEqual(5);
9696
tree.add(6);
9797
expect(tree.search(6).value).toEqual(6);
9898
});
9999

100100
it('should return null when no element was found', function () {
101-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
101+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
102102
tree.add(5);
103103
expect(tree.search(23)).toBeNull();
104104
tree.add(6);
@@ -107,7 +107,7 @@ describe('BinaryTree', function () {
107107
});
108108

109109
it('should be able to get max value', function () {
110-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
110+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
111111
expect(tree.min()).toBeNull();
112112
tree.add(1);
113113
tree.add(2);
@@ -117,7 +117,7 @@ describe('BinaryTree', function () {
117117
});
118118

119119
it('should be able to get min value', function () {
120-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
120+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
121121
expect(tree.max()).toBeNull();
122122
tree.add(1);
123123
tree.add(2);
@@ -127,56 +127,56 @@ describe('BinaryTree', function () {
127127
});
128128

129129
it('should be able to get node successor', function () {
130-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
130+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
131131
tree.add(1);
132132
tree.add(2);
133133
tree.add(4);
134134
tree.add(5);
135-
expect(tree.sucessor(2).value).toEqual(4);
135+
expect(tree.successor(2).value).toEqual(4);
136136
});
137137

138138
it('should be able to get node successor when it doesn\'t have right child', function () {
139-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
139+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
140140
tree.add(1);
141141
tree.add(10);
142142
tree.add(9);
143143
tree.add(8);
144-
expect(tree.sucessor(8).value).toEqual(9);
145-
expect(tree.sucessor(9).value).toEqual(10);
144+
expect(tree.successor(8).value).toEqual(9);
145+
expect(tree.successor(9).value).toEqual(10);
146146
});
147147

148148
it('should return null for node successor if tree is empty', function () {
149-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
150-
expect(tree.sucessor(2)).toBeNull();
149+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
150+
expect(tree.successor(2)).toBeNull();
151151
});
152152

153153
it('should return null if there is no successor', function () {
154-
var tree = new BinaryTree<number>(function (a,b) {return a > b;});
154+
const tree = new BinaryTree<number>(function (a,b) {return a > b;});
155155
tree.add(1);
156-
expect(tree.sucessor(1)).toBeNull();
156+
expect(tree.successor(1)).toBeNull();
157157
tree.add(2);
158158
tree.add(4);
159159
tree.add(5);
160-
expect(tree.sucessor(5)).toBeNull();
161-
expect(tree.sucessor(4).value).toEqual(5);
160+
expect(tree.successor(5)).toBeNull();
161+
expect(tree.successor(4).value).toEqual(5);
162162
});
163163

164164
describe('delete', function () {
165165
it(`should return false if node was not found`, function () {
166-
var tree = new BinaryTree<number>();
166+
const tree = new BinaryTree<number>();
167167
expect(tree.delete(1)).toEqual(false);
168168
});
169169

170170
it(`should remove node if it doesn't have children`, function () {
171-
var tree = new BinaryTree<number>();
171+
const tree = new BinaryTree<number>();
172172
tree.add(1);
173173
expect(tree.search(1)).not.toBeNull();
174174
tree.delete(1);
175175
expect(tree.search(1)).toBeNull();
176176
});
177177

178178
it(`should move the left child up if the node to remove has only left child and no right child`, function () {
179-
var tree = new BinaryTree<number>();
179+
const tree = new BinaryTree<number>();
180180
tree.add(2);
181181
tree.add(1);
182182
expect(tree.search(2)).not.toBeNull();
@@ -186,7 +186,7 @@ describe('BinaryTree', function () {
186186
});
187187

188188
it(`should move the right child up if the node to remove has only right child and no left child`, function () {
189-
var tree = new BinaryTree<number>();
189+
const tree = new BinaryTree<number>();
190190
tree.add(1);
191191
tree.add(2);
192192
expect(tree.search(1)).not.toBeNull();

tests/RedBlackTreeSpec.ts

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
import {} from "../ts/BinaryTree/RedBlackTree";
2+
import RedBlackTree from "../ts/BinaryTree/RedBlackTree";
3+
4+
describe('RedBlackTree', function () {
5+
describe('add' , function () {
6+
7+
it('should be able to add node', function () {
8+
const tree = new RedBlackTree<number>();
9+
tree.add(2);
10+
expect(tree.root.value).toEqual(2);
11+
tree.add(1);
12+
expect(tree.root.left.value).toEqual(1);
13+
expect(tree.root.value).toEqual(2);
14+
});
15+
it('should be able to add node', function () {
16+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
17+
tree.add(5);
18+
expect(tree.root.value).toEqual(5);
19+
tree.add(6);
20+
expect(tree.root.right.value).toEqual(6);
21+
tree.add(4);
22+
expect(tree.root.left.value).toEqual(4);
23+
});
24+
});
25+
26+
it('should be able to add node to binary tree with object values', function () {
27+
const tree = new RedBlackTree<any>(function (a:any,b:any) {return a.amount > b.amount;});
28+
tree.add({amount : 5});
29+
expect(tree.root.value).toEqual({amount : 5});
30+
tree.add({amount : 6});
31+
expect(tree.root.right.value).toEqual({amount : 6});
32+
tree.add({amount : 4});
33+
expect(tree.root.left.value).toEqual({amount : 4});
34+
});
35+
36+
it('should be able to walk the binary tree in order', function () {
37+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
38+
tree.add(5);
39+
tree.add(6);
40+
tree.add(4);
41+
tree.add(1);
42+
tree.add(2);
43+
expect(tree.inorderTreeWalk(function (pv:number[], cv:number) {
44+
pv.push(cv);
45+
return pv;
46+
}, [])).toEqual([1,2,4,5,6]);
47+
});
48+
49+
it('should be able to walk the binary tree that has object values in order', function () {
50+
const tree = new RedBlackTree<any>(function (a:any,b:any) {return a.amount > b.amount;});
51+
tree.add({amount: 6});
52+
tree.add({amount: 4});
53+
tree.add({amount: 5});
54+
tree.add({amount: 1});
55+
tree.add({amount: 2});
56+
expect(tree.inorderTreeWalk(function (pv:number[], cv:any) {
57+
pv.push(cv.amount);
58+
return pv;
59+
}, [])).toEqual([1,2,4,5,6]);
60+
});
61+
62+
63+
it('should be able to walk the binary tree in order', function () {
64+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
65+
tree.add(5);
66+
tree.add(6);
67+
tree.add(4);
68+
tree.add(1);
69+
tree.add(2);
70+
expect(tree.reverseTreeWalk(function (pv:number[], cv:number) {
71+
pv.push(cv);
72+
return pv;
73+
}, [])).toEqual([6,5,4,2,1]);
74+
});
75+
76+
it('should be able to walk the binary tree in reverseorder', function () {
77+
const tree = new RedBlackTree<any>(function (a:any,b:any) {return a.amount > b.amount;});
78+
tree.add({amount: 6});
79+
tree.add({amount: 4});
80+
tree.add({amount: 5});
81+
tree.add({amount: 1});
82+
tree.add({amount: 2});
83+
expect(tree.reverseTreeWalk(function (pv:number[], cv:any) {
84+
pv.push(cv.amount);
85+
return pv;
86+
}, [])).toEqual([6,5,4,2,1]);
87+
});
88+
89+
describe(' search', function () {
90+
it('should return null when no root', function () {
91+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
92+
expect(tree.search(5)).toBeNull();
93+
});
94+
95+
it('should return element when found', function () {
96+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
97+
tree.add(5);
98+
expect(tree.search(5).value).toEqual(5);
99+
tree.add(6);
100+
expect(tree.search(6).value).toEqual(6);
101+
});
102+
103+
it('should return null when no element was found', function () {
104+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
105+
tree.add(5);
106+
expect(tree.search(23)).toBeNull();
107+
tree.add(6);
108+
expect(tree.search(16)).toBeNull();
109+
});
110+
});
111+
112+
it('should be able to get min value', function () {
113+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
114+
expect(tree.min()).toBeNull();
115+
tree.add(1);
116+
tree.add(2);
117+
tree.add(4);
118+
tree.add(5);
119+
expect(tree.min().value).toEqual(1);
120+
});
121+
122+
it('should be able to get min value', function () {
123+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
124+
expect(tree.max()).toBeNull();
125+
tree.add(1);
126+
tree.add(2);
127+
tree.add(4);
128+
tree.add(5);
129+
expect(tree.max().value).toEqual(5);
130+
});
131+
132+
it('should be able to get node successor', function () {
133+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
134+
tree.add(1);
135+
tree.add(2);
136+
tree.add(4);
137+
tree.add(5);
138+
expect(tree.successor(2).value).toEqual(4);
139+
});
140+
141+
it('should be able to get node successor when it doesn\'t have right child', function () {
142+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
143+
tree.add(1);
144+
tree.add(10);
145+
tree.add(9);
146+
tree.add(8);
147+
expect(tree.successor(8).value).toEqual(9);
148+
expect(tree.successor(9).value).toEqual(10);
149+
});
150+
151+
it('should return null for node successor if tree is empty', function () {
152+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
153+
expect(tree.successor(2)).toBeNull();
154+
});
155+
156+
it('should return null if there is no successor', function () {
157+
const tree = new RedBlackTree<number>(function (a:number,b:number) {return a > b;});
158+
tree.add(1);
159+
expect(tree.successor(1)).toBeNull();
160+
tree.add(2);
161+
tree.add(4);
162+
tree.add(5);
163+
expect(tree.successor(5)).toBeNull();
164+
expect(tree.successor(4).value).toEqual(5);
165+
});
166+
167+
xdescribe('delete', function () {
168+
it(`should return false if node was not found`, function () {
169+
const tree = new RedBlackTree<number>();
170+
expect(tree.delete(1)).toEqual(false);
171+
});
172+
173+
it(`should remove node if it doesn't have children`, function () {
174+
const tree = new RedBlackTree<number>();
175+
tree.add(1);
176+
expect(tree.search(1)).not.toBeNull();
177+
tree.delete(1);
178+
expect(tree.search(1)).toBeNull();
179+
});
180+
181+
it(`should move the left child up if the node to remove has only left child and no right child`, function () {
182+
const tree = new RedBlackTree<number>();
183+
tree.add(2);
184+
tree.add(1);
185+
expect(tree.search(2)).not.toBeNull();
186+
tree.delete(2);
187+
expect(tree.search(2)).toBeNull();
188+
expect(tree.root.value).toEqual(1);
189+
});
190+
191+
it(`should move the right child up if the node to remove has only right child and no left child`, function () {
192+
const tree = new RedBlackTree<number>();
193+
tree.add(1);
194+
tree.add(2);
195+
expect(tree.search(1)).not.toBeNull();
196+
tree.delete(1);
197+
expect(tree.search(1)).toBeNull();
198+
expect(tree.root.value).toEqual(2);
199+
});
200+
});
201+
});

0 commit comments

Comments
 (0)