From 76669225664734ca47ed6f481691613d9073e946 Mon Sep 17 00:00:00 2001 From: Daniel Jonathan Date: Sat, 28 Oct 2023 15:32:04 -0600 Subject: [PATCH] feat(CV-0-1): reordered nodes in Tree and added tree remove --- __tests__/structures/Tree.spec.ts | 23 +++++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- src/structures/Tree.ts | 2 +- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/__tests__/structures/Tree.spec.ts b/__tests__/structures/Tree.spec.ts index af6ffee..b0f16c9 100644 --- a/__tests__/structures/Tree.spec.ts +++ b/__tests__/structures/Tree.spec.ts @@ -46,6 +46,7 @@ import { listCreate, Tree, treeCreate, + treeRemove, treeInsertChild, treeAppendChild, treeDepth, @@ -162,6 +163,28 @@ describe('Tree', () => { expect(n1.size).toBe(4) }) + it('treeRemove', () => { + const n1 = createTreeNode(1, 'a') + const n2 = createTreeNode(2, 'b') + const n3 = createTreeNode(3, 'c') + const n4 = createTreeNode(4, 'd') + + treeInsertChild(n1, n2) + treeInsertChild(n1, n3) + treeInsertChild(n1, n4) + + expect(n1.size).toBe(4) + + treeRemove(n2) + expect(n1.size).toBe(3) + + treeRemove(n3) + expect(n1.size).toBe(2) + + treeRemove(n1) + expect(n1.size).toBe(2) + }) + it('node.size', () => { const n1 = createTreeNode(1, 'a') const n2 = createTreeNode(2, 'b') diff --git a/package-lock.json b/package-lock.json index 1638084..0317bbc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@cosmicmind/algojs", - "version": "0.0.1-rc-102823-2-a", + "version": "0.0.1-rc-102823-2-b", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@cosmicmind/algojs", - "version": "0.0.1-rc-102823-2-a", + "version": "0.0.1-rc-102823-2-b", "license": "BSD-3-Clause", "dependencies": { "@cosmicmind/foundationjs": "^0.0.1-rc-092723-1" diff --git a/package.json b/package.json index 165ad4a..29236af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cosmicmind/algojs", - "version": "0.0.1-rc-102823-2-a", + "version": "0.0.1-rc-102823-2-b", "description": "An algorithms and data structures library in TypeScript.", "keywords": [], "author": { diff --git a/src/structures/Tree.ts b/src/structures/Tree.ts index 0f132e0..fd588bb 100644 --- a/src/structures/Tree.ts +++ b/src/structures/Tree.ts @@ -96,7 +96,7 @@ export function treeAppendChild(parent: T, node: T): void { treeIncreaseSize(parent, node.size) } -function treeRemove(node: T, compare = TreeCompareFn): void { +export function treeRemove(node: T, compare = TreeCompareFn): void { const parent = node.parent as Optional if (guard(parent)) { listRemove(parent.children as List, node, compare)