mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
[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.
This commit is contained in:
parent
ed7ead3744
commit
82168eb4a2
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue