Skip to content

Commit 8d21315

Browse files
committed
Add AVL Tree.
1 parent 359286e commit 8d21315

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/data-structures/tree/avl-tree/__test__/AvlTRee.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ describe('AvlTree', () => {
122122
});
123123

124124
it('should create balanced tree: case #1', () => {
125+
// @see: https://www.youtube.com/watch?v=rbg7Qf8GkQ4&t=839s
125126
const tree = new AvlTree();
126127

127128
tree.insert(1);
@@ -162,4 +163,26 @@ describe('AvlTree', () => {
162163
expect(tree.root.height).toBe(3);
163164
expect(tree.toString()).toBe('-8,-5,-2,1,2,3,6,15');
164165
});
166+
167+
it('should create balanced tree: case #2', () => {
168+
// @see https://www.youtube.com/watch?v=7m94k2Qhg68
169+
const tree = new AvlTree();
170+
171+
tree.insert(43);
172+
tree.insert(18);
173+
tree.insert(22);
174+
tree.insert(9);
175+
tree.insert(21);
176+
tree.insert(6);
177+
178+
expect(tree.root.value).toBe(18);
179+
expect(tree.root.height).toBe(2);
180+
expect(tree.toString()).toBe('6,9,18,21,22,43');
181+
182+
tree.insert(8);
183+
184+
expect(tree.root.value).toBe(18);
185+
expect(tree.root.height).toBe(2);
186+
expect(tree.toString()).toBe('6,8,9,18,21,22,43');
187+
});
165188
});

0 commit comments

Comments
 (0)