diff --git a/__tests__/structures/Tree.spec.ts b/__tests__/structures/Tree.spec.ts index c5097cd..af6ffee 100644 --- a/__tests__/structures/Tree.spec.ts +++ b/__tests__/structures/Tree.spec.ts @@ -130,9 +130,9 @@ describe('Tree', () => { const n3 = createTreeNode(3, 'c') const n4 = createTreeNode(4, 'd') - treeInsertChild(n2, n1) - treeInsertChild(n3, n1) - treeInsertChild(n4, n1) + treeInsertChild(n1, n2) + treeInsertChild(n1, n3) + treeInsertChild(n1, n4) expect(treeIsFirstChild(n2, n1)).toBeFalsy() expect(treeIsFirstChild(n3, n1)).toBeFalsy() @@ -147,13 +147,13 @@ describe('Tree', () => { const n3 = createTreeNode(3, 'c') const n4 = createTreeNode(4, 'd') - treeAppendChild(n2, n1) + treeAppendChild(n1, n2) expect(treeIsOnlyChild(n2, n1)).toBeTruthy() - treeAppendChild(n3, n1) + treeAppendChild(n1, n3) expect(treeIsOnlyChild(n2, n1)).toBeFalsy() - treeAppendChild(n4, n1) + treeAppendChild(n1, n4) expect(treeIsLastChild(n2, n1)).toBeFalsy() expect(treeIsLastChild(n3, n1)).toBeFalsy() @@ -171,16 +171,16 @@ describe('Tree', () => { const n6 = createTreeNode(6, 'f') const n7 = createTreeNode(7, 'g') - treeInsertChild(n2, n1) + treeInsertChild(n1, n2) expect(treeIsOnlyChild(n2, n1)).toBeTruthy() - treeInsertChild(n3, n1) + treeInsertChild(n1, n3) expect(treeIsOnlyChild(n3, n1)).toBeFalsy() - treeInsertChild(n4, n1) - treeInsertChild(n5, n1) - treeInsertChild(n6, n1) - treeInsertChild(n7, n2) + treeInsertChild(n1, n4) + treeInsertChild(n1, n5) + treeInsertChild(n1, n6) + treeInsertChild(n2, n7) expect(n1.size).toBe(7) expect(n2.size).toBe(2) @@ -197,12 +197,12 @@ describe('Tree', () => { const n6 = createTreeNode(6, 'f') const n7 = createTreeNode(7, 'g') - treeInsertChild(n2, n1) - treeInsertChild(n3, n1) - treeInsertChild(n4, n1) - treeInsertChild(n5, n1) - treeInsertChild(n6, n2) - treeInsertChild(n7, n2) + treeInsertChild(n1, n2) + treeInsertChild(n1, n3) + treeInsertChild(n1, n4) + treeInsertChild(n1, n5) + treeInsertChild(n2, n6) + treeInsertChild(n2, n7) expect(treeDepth(n1)).toBe(0) expect(treeDepth(n2)).toBe(1) @@ -222,12 +222,12 @@ describe('Tree', () => { const n6 = createTreeNode(6, 'f') const n7 = createTreeNode(7, 'g') - treeAppendChild(n2, n1) - treeAppendChild(n3, n1) - treeAppendChild(n4, n1) - treeAppendChild(n5, n1) - treeAppendChild(n6, n2) - treeAppendChild(n7, n2) + treeAppendChild(n1, n2) + treeAppendChild(n1, n3) + treeAppendChild(n1, n4) + treeAppendChild(n1, n5) + treeAppendChild(n2, n6) + treeAppendChild(n2, n7) const result: Tree[] = [] @@ -247,12 +247,12 @@ describe('Tree', () => { const n6 = createTreeNode(6, 'f') const n7 = createTreeNode(7, 'g') - treeAppendChild(n2, n1) - treeAppendChild(n3, n1) - treeAppendChild(n4, n1) - treeAppendChild(n5, n1) - treeAppendChild(n6, n2) - treeAppendChild(n7, n2) + treeAppendChild(n1, n2) + treeAppendChild(n1, n3) + treeAppendChild(n1, n4) + treeAppendChild(n1, n5) + treeAppendChild(n2, n6) + treeAppendChild(n2, n7) let result = treeQuery(n1, node => 1 === node.key || 3 === node.key) expect(result.size).toBe(2) diff --git a/package.json b/package.json index 4884033..aba8eb5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cosmicmind/algojs", - "version": "0.0.1-rc-092723-2", + "version": "0.0.1-rc-102823-2", "description": "An algorithms and data structures library in TypeScript.", "keywords": [], "author": { diff --git a/src/structures/Tree.ts b/src/structures/Tree.ts index 5ddb702..0f132e0 100644 --- a/src/structures/Tree.ts +++ b/src/structures/Tree.ts @@ -35,6 +35,7 @@ */ import { + Optional, assert, guard, } from '@cosmicmind/foundationjs' @@ -50,6 +51,7 @@ import { listIsLast, listInsert, listAppend, + listRemove, listIterateFromFirst, } from '@/structures' @@ -79,19 +81,28 @@ export const treeCreate = (props?: Omit): T => ({ /** * @performance O(1) */ -export function treeInsertChild(insert: T, parent: T): void { - insert.parent = parent - listInsert(parent.children, insert) - treeIncreaseSize(parent, insert.size) +export function treeInsertChild(parent: T, node: T): void { + node.parent = parent + listInsert(parent.children, node) + treeIncreaseSize(parent, node.size) } /** * @performance O(1) */ -export function treeAppendChild(insert: T, parent: T): void { - insert.parent = parent - listAppend(parent.children, insert) - treeIncreaseSize(parent, insert.size) +export function treeAppendChild(parent: T, node: T): void { + node.parent = parent + listAppend(parent.children, node) + treeIncreaseSize(parent, node.size) +} + +function treeRemove(node: T, compare = TreeCompareFn): void { + const parent = node.parent as Optional + if (guard(parent)) { + listRemove(parent.children as List, node, compare) + treeDecreaseSize(parent, node.size) + node.parent = SentinelNode + } } /** @@ -117,7 +128,7 @@ export function treeIsLeaf(node: T): boolean { /** * @performance O(1) */ -export function treeIsChild(node: T, parent: T, compare = TreeCompareFn): boolean { +export function treeIsChild(parent: T, node: T, compare = TreeCompareFn): boolean { return guard(node.parent) && 0 === compare(node.parent, parent) } @@ -145,7 +156,7 @@ export function treeIsOnlyChild(node: T, parent: T, compare = Tr /** * @performance O(h) where h = height of Tree */ -export function treeIsChildDeep(node: T, parent: T, compare = TreeCompareFn): boolean { +export function treeIsChildDeep(parent: T, node: T, compare = TreeCompareFn): boolean { let n = node.parent while (guard(n)) { if (0 === compare(n, parent)) {