diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 103dc4e..44ece16 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -1727,7 +1727,7 @@ export class BinaryTree = BinaryTreeNode /** * The `print` function is used to display a binary tree structure in a visually appealing way. - * @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null | + * @param {N | null | undefined} beginRoot - The `root` parameter is of type `BTNKey | N | null | * undefined`. It represents the root node of a binary tree. The root node can have one of the * following types: */ diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 268e742..95f317d 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -103,11 +103,16 @@ export class RedBlackTree = RedBlackTr while (x !== this.NIL) { y = x; - if (x && node.key < x.key) { - x = x.left; - } else { - x = x?.right; + if (x) { + if (node.key < x.key) { + x = x.left; + } else if (node.key > x.key){ + x = x?.right; + } else { + return; + } } + } node.parent = y; diff --git a/src/types/helpers.ts b/src/types/common.ts similarity index 100% rename from src/types/helpers.ts rename to src/types/common.ts diff --git a/src/types/index.ts b/src/types/index.ts index 020d17c..2530dca 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,3 +1,3 @@ export * from './data-structures'; -export * from './helpers'; +export * from './common'; export * from './utils'; diff --git a/test/unit/data-structures/binary-tree/rb-tree.test.ts b/test/unit/data-structures/binary-tree/rb-tree.test.ts index b31c168..eb13e21 100644 --- a/test/unit/data-structures/binary-tree/rb-tree.test.ts +++ b/test/unit/data-structures/binary-tree/rb-tree.test.ts @@ -487,4 +487,21 @@ describe('RedBlackTree', () => { } isDebug && console.log(performance.now() - cS); }); + + it('duplicates', () => { + tree.addMany([9,8,7,8,8,8,2,3,6,5,5,4]); + tree.print(); + + expect(tree.size).toBe(8); + expect(tree.isBST()).toBe(true); + expect(tree.isAVLBalanced()).toBe(true); + tree.addMany([10, 5, 2, 11]); + expect(tree.size).toBe(10); + expect(tree.isBST()).toBe(true); + expect(tree.isAVLBalanced()).toBe(true); + + tree.clear(); + tree.addMany([10, 20, 30, 40, 50, 60]) + expect(tree.isAVLBalanced()).toBe(false); + }) });