From 82168eb4a273afbaf007289c542e9120ebf3ff73 Mon Sep 17 00:00:00 2001 From: Revone Date: Tue, 31 Oct 2023 23:21:54 +0800 Subject: [PATCH] [rbtree] Currently, the insertion and deletion of 1000 data entries are supported. However, when the data volume increases to 10000 entries, null value issues are bound to arise due to repetitive deletions and additions. --- src/data-structures/binary-tree/rb-tree.ts | 8 +- .../binary-tree/rb-tree.test.ts | 106 ++++++++++++++++++ 2 files changed, 110 insertions(+), 4 deletions(-) diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 9009419..e72b88a 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -140,7 +140,7 @@ export class RedBlackTree { y.color = z.color; } if (yOriginalColor === 0) { - this.fixDelete(x); + this._fixDelete(x); } } helper(this.root); @@ -170,7 +170,7 @@ export class RedBlackTree { return dfs(beginRoot); } - + /** * The function returns the leftmost node in a red-black tree. * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in @@ -283,11 +283,11 @@ export class RedBlackTree { } /** - * The fixDelete function is used to rebalance the Red-Black Tree after a node deletion. + * The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion. * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a * red-black tree. */ - protected fixDelete(x: RBTreeNode): void { + protected _fixDelete(x: RBTreeNode): void { let s: RBTreeNode; while (x !== this.root && x.color === 0) { if (x === x.parent.left) { 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 fd76114..37b8643 100644 --- a/test/unit/data-structures/binary-tree/rb-tree.test.ts +++ b/test/unit/data-structures/binary-tree/rb-tree.test.ts @@ -1,4 +1,5 @@ import { RedBlackTree, RBTreeNode } from '../../../../src'; +import {getRandomInt} from "../../../utils"; describe('RedBlackTree', () => { let tree: RedBlackTree; @@ -134,3 +135,108 @@ describe('RedBlackTree', () => { }); }); }); + + +describe('RedBlackTree', () => { + let tree: RedBlackTree; + + beforeEach(() => { + tree = new RedBlackTree(); + }); + + it('should insert nodes into the tree', () => { + tree.insert(10); + expect(tree.getNode(10)).toBeDefined(); + tree.insert(20); + expect(tree.getNode(20)).toBeDefined(); + tree.insert(5); + expect(tree.getNode(5)).toBeDefined(); + }); + + it('should delete nodes from the tree', () => { + tree.insert(10); + tree.insert(20); + tree.insert(5); + tree.delete(20); + expect(tree.getNode(20)).toBe(tree.NIL); + }); + + it('should get the successor of a node', () => { + tree.insert(10); + tree.insert(20); + const node = tree.getNode(10); + const successor = tree.getSuccessor(node); + expect(successor?.key).toBe(20); + }); + + it('should get the predecessor of a node', () => { + tree.insert(10); + tree.insert(20); + const node = tree.getNode(20); + const predecessor = tree.getPredecessor(node); + expect(predecessor?.key).toBe(10); + }); + + it('should rotate nodes to the left', () => { + tree.insert(10); + tree.insert(20); + tree.insert(5); + const node = tree.getNode(10); + tree.insert(15); + // Verify that rotation has occurred + expect(node.left.key).toBe(5); + expect(node.right.key).toBe(20); + }); + + it('should rotate nodes to the right', () => { + tree.insert(10); + tree.insert(20); + tree.insert(5); + const node = tree.getNode(20); + tree.insert(25); + // Verify that rotation has occurred + expect(node.left.key).toBe(0); + expect(node.right.key).toBe(25); + }); + + it('should fix the tree after deletion', () => { + tree.insert(10); + tree.insert(20); + tree.insert(5); + tree.insert(15); + tree.delete(15); + // Verify that the tree is still valid + // You can add assertions to check the Red-Black Tree properties + }); + + it('should fix the tree after insertion', () => { + for (let i = 0; i < 1000; i++) { + tree.insert(getRandomInt(-100, 1000)); + tree.delete(getRandomInt(-100, 1000)); + } + tree.insert(1); + tree.insert(2); + tree.insert(5); + tree.insert(15); + tree.insert(25); + tree.insert(10); + tree.insert(8); + tree.insert(28); + tree.insert(111); + tree.insert(12); + tree.delete(2); + tree.insert(22); + tree.insert(50); + tree.insert(155); + tree.insert(225); + tree.insert(7); + tree.delete(15); + tree.insert(23); + tree.insert(33); + tree.insert(15); + + + // Verify that the tree is still a valid Red-Black Tree + // You can add assertions to check the Red-Black Tree properties + }); +});