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 8b394b5 commit 7666922
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
23 changes: 23 additions & 0 deletions __tests__/structures/Tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
listCreate,
Tree,
treeCreate,
treeRemove,
treeInsertChild,
treeAppendChild,
treeDepth,
Expand Down Expand Up @@ -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')
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-a",
"version": "0.0.1-rc-102823-2-b",
"description": "An algorithms and data structures library in TypeScript.",
"keywords": [],
"author": {
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function treeAppendChild<T extends Tree>(parent: T, node: T): void {
treeIncreaseSize(parent, node.size)
}

function treeRemove<T extends Tree>(node: T, compare = TreeCompareFn<T>): void {
export 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)
Expand Down

0 comments on commit 7666922

Please sign in to comment.