Skip to content

Commit

Permalink
feat(CV-0-1): reordered nodes in Tree and added tree remove
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jonathan committed Oct 28, 2023
1 parent 4b322bd commit 026cb9c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 41 deletions.
60 changes: 30 additions & 30 deletions __tests__/structures/Tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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[] = []

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
31 changes: 21 additions & 10 deletions src/structures/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

import {
Optional,
assert,
guard,
} from '@cosmicmind/foundationjs'
Expand All @@ -50,6 +51,7 @@ import {
listIsLast,
listInsert,
listAppend,
listRemove,
listIterateFromFirst,
} from '@/structures'

Expand Down Expand Up @@ -79,19 +81,28 @@ export const treeCreate = <T extends Tree>(props?: Omit<T, keyof Tree>): T => ({
/**
* @performance O(1)
*/
export function treeInsertChild<T extends Tree>(insert: T, parent: T): void {
insert.parent = parent
listInsert(parent.children, insert)
treeIncreaseSize(parent, insert.size)
export function treeInsertChild<T extends Tree>(parent: T, node: T): void {
node.parent = parent
listInsert(parent.children, node)
treeIncreaseSize(parent, node.size)
}

/**
* @performance O(1)
*/
export function treeAppendChild<T extends Tree>(insert: T, parent: T): void {
insert.parent = parent
listAppend(parent.children, insert)
treeIncreaseSize(parent, insert.size)
export function treeAppendChild<T extends Tree>(parent: T, node: T): void {
node.parent = parent
listAppend(parent.children, node)
treeIncreaseSize(parent, node.size)
}

function treeRemove<T extends Tree>(node: T, compare = TreeCompareFn<T>): void {
const parent = node.parent as Optional<T>
if (guard<T>(parent)) {
listRemove(parent.children as List<T>, node, compare)
treeDecreaseSize(parent, node.size)
node.parent = SentinelNode
}
}

/**
Expand All @@ -117,7 +128,7 @@ export function treeIsLeaf<T extends Tree>(node: T): boolean {
/**
* @performance O(1)
*/
export function treeIsChild<T extends Tree>(node: T, parent: T, compare = TreeCompareFn<T>): boolean {
export function treeIsChild<T extends Tree>(parent: T, node: T, compare = TreeCompareFn<T>): boolean {
return guard<T>(node.parent) && 0 === compare(node.parent, parent)
}

Expand Down Expand Up @@ -145,7 +156,7 @@ export function treeIsOnlyChild<T extends Tree>(node: T, parent: T, compare = Tr
/**
* @performance O(h) where h = height of Tree
*/
export function treeIsChildDeep<T extends Tree>(node: T, parent: T, compare = TreeCompareFn<T>): boolean {
export function treeIsChildDeep<T extends Tree>(parent: T, node: T, compare = TreeCompareFn<T>): boolean {
let n = node.parent
while (guard<T>(n)) {
if (0 === compare(n, parent)) {
Expand Down

0 comments on commit 026cb9c

Please sign in to comment.