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 7666922 commit 2a5c55e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
32 changes: 32 additions & 0 deletions __tests__/structures/Tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import {
treeIsOnlyChild,
treeIterator,
treeQuery,
treeInsertChildBefore,
treeInsertChildAfter,
} from '@/index'

type A = Tree & {
Expand Down Expand Up @@ -185,6 +187,36 @@ describe('Tree', () => {
expect(n1.size).toBe(2)
})

it('treeInsertChildBefore', () => {
const n1 = createTreeNode(1, 'a')
const n2 = createTreeNode(2, 'b')
const n3 = createTreeNode(3, 'c')
const n4 = createTreeNode(4, 'd')

treeInsertChild(n1, n2)
treeInsertChildBefore(n1, n3, n2)
treeInsertChildBefore(n1, n4, n3)

expect(n1.children.first).toStrictEqual(n4)
expect(n4.next).toStrictEqual(n3)
expect(n3.next).toStrictEqual(n2)
})

it('treeInsertChildAfter', () => {
const n1 = createTreeNode(1, 'a')
const n2 = createTreeNode(2, 'b')
const n3 = createTreeNode(3, 'c')
const n4 = createTreeNode(4, 'd')

treeInsertChild(n1, n2)
treeInsertChildAfter(n1, n3, n2)
treeInsertChildAfter(n1, n4, n3)

expect(n1.children.last).toStrictEqual(n4)
expect(n2.next).toStrictEqual(n3)
expect(n3.next).toStrictEqual(n4)
})

it('node.size', () => {
const n1 = createTreeNode(1, 'a')
const n2 = createTreeNode(2, 'b')
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-b",
"version": "0.0.1-rc-102823-2-c",
"description": "An algorithms and data structures library in TypeScript.",
"keywords": [],
"author": {
Expand Down
20 changes: 20 additions & 0 deletions src/structures/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import {
listInsert,
listAppend,
listRemove,
listInsertBefore,
listInsertAfter,
listIterateFromFirst,
} from '@/structures'

Expand Down Expand Up @@ -87,6 +89,24 @@ export function treeInsertChild<T extends Tree>(parent: T, node: T): void {
treeIncreaseSize(parent, node.size)
}

/**
* @performance O(1)
*/
export function treeInsertChildBefore<T extends Tree>(parent: T, node: T, before: T, compare = TreeCompareFn<T>): void {
node.parent = parent
listInsertBefore(parent.children as List<T>, node, before, compare)
treeIncreaseSize(parent, node.size)
}

/**
* @performance O(1)
*/
export function treeInsertChildAfter<T extends Tree>(parent: T, node: T, after: T, compare = TreeCompareFn<T>): void {
node.parent = parent
listInsertAfter(parent.children as List<T>, node, after, compare)
treeIncreaseSize(parent, node.size)
}

/**
* @performance O(1)
*/
Expand Down

0 comments on commit 2a5c55e

Please sign in to comment.