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 2a5c55e commit 535478d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
24 changes: 12 additions & 12 deletions __tests__/structures/Tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ describe('Tree', () => {
treeInsertChild(n1, n3)
treeInsertChild(n1, n4)

expect(treeIsFirstChild(n2, n1)).toBeFalsy()
expect(treeIsFirstChild(n3, n1)).toBeFalsy()
expect(treeIsFirstChild(n4, n1)).toBeTruthy()
expect(treeIsLastChild(n2, n1)).toBeTruthy()
expect(treeIsFirstChild(n1, n2)).toBeFalsy()
expect(treeIsFirstChild(n1, n3)).toBeFalsy()
expect(treeIsFirstChild(n1, n4)).toBeTruthy()
expect(treeIsLastChild(n1, n2)).toBeTruthy()
expect(n1.size).toBe(4)
})

Expand All @@ -151,17 +151,17 @@ describe('Tree', () => {
const n4 = createTreeNode(4, 'd')

treeAppendChild(n1, n2)
expect(treeIsOnlyChild(n2, n1)).toBeTruthy()
expect(treeIsOnlyChild(n1, n2)).toBeTruthy()

treeAppendChild(n1, n3)
expect(treeIsOnlyChild(n2, n1)).toBeFalsy()
expect(treeIsOnlyChild(n1, n2)).toBeFalsy()

treeAppendChild(n1, n4)

expect(treeIsLastChild(n2, n1)).toBeFalsy()
expect(treeIsLastChild(n3, n1)).toBeFalsy()
expect(treeIsLastChild(n4, n1)).toBeTruthy()
expect(treeIsFirstChild(n2, n1)).toBeTruthy()
expect(treeIsLastChild(n1, n2)).toBeFalsy()
expect(treeIsLastChild(n1, n3)).toBeFalsy()
expect(treeIsLastChild(n1, n4)).toBeTruthy()
expect(treeIsFirstChild(n1, n2)).toBeTruthy()
expect(n1.size).toBe(4)
})

Expand Down Expand Up @@ -227,10 +227,10 @@ describe('Tree', () => {
const n7 = createTreeNode(7, 'g')

treeInsertChild(n1, n2)
expect(treeIsOnlyChild(n2, n1)).toBeTruthy()
expect(treeIsOnlyChild(n1, n2)).toBeTruthy()

treeInsertChild(n1, n3)
expect(treeIsOnlyChild(n3, n1)).toBeFalsy()
expect(treeIsOnlyChild(n1, n3)).toBeFalsy()

treeInsertChild(n1, n4)
treeInsertChild(n1, n5)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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-102823-2-c",
"version": "0.0.1-rc-102823-2-d",
"description": "An algorithms and data structures library in TypeScript.",
"keywords": [],
"author": {
Expand Down
6 changes: 3 additions & 3 deletions src/structures/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,21 @@ export function treeIsChild<T extends Tree>(parent: T, node: T, compare = TreeCo
/**
* @performance O(1)
*/
export function treeIsFirstChild<T extends Tree>(node: T, parent: T, compare = TreeCompareFn<T>): boolean {
export function treeIsFirstChild<T extends Tree>(parent: T, node: T, compare = TreeCompareFn<T>): boolean {
return listIsFirst(parent.children as List<T>, node, compare)
}

/**
* @performance O(1)
*/
export function treeIsLastChild<T extends Tree>(node: T, parent: T, compare = TreeCompareFn<T>): boolean {
export function treeIsLastChild<T extends Tree>(parent: T, node: T, compare = TreeCompareFn<T>): boolean {
return listIsLast(parent.children as List<T>, node, compare)
}

/**
* @performance O(1)
*/
export function treeIsOnlyChild<T extends Tree>(node: T, parent: T, compare = TreeCompareFn<T>): boolean {
export function treeIsOnlyChild<T extends Tree>(parent: T, node: T, compare = TreeCompareFn<T>): boolean {
return listIsFirst(parent.children as List<T>, node, compare) && listIsLast(parent.children as List<T>, node, compare)
}

Expand Down

0 comments on commit 535478d

Please sign in to comment.