-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Peter Hovanec
committed
Nov 5, 2024
1 parent
2df2e43
commit 610e12f
Showing
3 changed files
with
171 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,43 @@ | ||
|
||
|
||
class OrdinalNode { | ||
constructor(value) { | ||
this.value = value; | ||
this.children = []; | ||
this.parent = null; | ||
this.value = value; | ||
this.children = []; | ||
this.parent = null; | ||
} | ||
|
||
addChild(childNode) { | ||
childNode.parent = this; | ||
this.children.push( childNode); | ||
childNode.parent = this; | ||
this.children.push(childNode); | ||
} | ||
|
||
removeChild(childNode) { | ||
const index = this.children.indexOf(childNode); | ||
if (index > -1) { | ||
this.children.splice(index, 1); | ||
childNode.parent = null; | ||
childNode.parent = null; | ||
} | ||
} | ||
|
||
getChildAt(index) { | ||
return this.children[index]; | ||
return this.children[index]; | ||
} | ||
|
||
getChildrenCount() { | ||
return this.children.length; | ||
} | ||
|
||
findChild(value) { | ||
return this.children.find((child) => child.value === value) || null; | ||
} | ||
|
||
getSiblings() { | ||
if (this.parent) { | ||
return this.parent.children.filter((child) => child !== this); | ||
} | ||
return []; | ||
} | ||
|
||
|
||
} | ||
|
||
export default OrdinalNode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,83 @@ | ||
|
||
|
||
import OrdinalNode from "./OrdinalNode"; | ||
|
||
class OrdinalTree { | ||
constructor() { | ||
this.root = null; | ||
this.root = null; | ||
} | ||
|
||
|
||
add(value, parentValue = null) { | ||
const newNode = new OrdinalNode(value); | ||
if (this.root === null) { | ||
this.root = newNode; | ||
this.root = newNode; | ||
} else { | ||
const parent = this.findNode(this.root, parentValue); | ||
if (parent) { | ||
parent.addChild(newNode); | ||
parent.addChild(newNode); | ||
} else { | ||
console.error("Parent not found"); | ||
} | ||
} | ||
} | ||
|
||
|
||
findNode(currentNode, value) { | ||
if (currentNode.value === value) { | ||
return currentNode; | ||
return currentNode; | ||
} | ||
|
||
for (const child of currentNode.children) { | ||
const foundNode = this.findNode(child, value); | ||
if (foundNode) { | ||
return foundNode; | ||
return foundNode; | ||
} | ||
} | ||
return null; | ||
return null; | ||
} | ||
|
||
|
||
traverse(callback) { | ||
const traverseNode = (node) => { | ||
callback(node); | ||
node.children.forEach(traverseNode); | ||
callback(node); | ||
node.children.forEach(traverseNode); | ||
}; | ||
if (this.root) { | ||
traverseNode(this.root); | ||
traverseNode(this.root); | ||
} | ||
} | ||
|
||
|
||
remove(value) { | ||
const nodeToRemove = this.findNode(this.root, value); | ||
if (nodeToRemove && nodeToRemove.parent) { | ||
nodeToRemove.parent.removeChild(nodeToRemove); | ||
} else if (nodeToRemove === this.root) { | ||
this.root = null; // If the root is being removed | ||
} | ||
} | ||
|
||
getHeight() { | ||
const heightOfNode = (node) => { | ||
if (!node) return -1; | ||
const heights = node.children.map(heightOfNode); | ||
return 1 + Math.max(-1, ...heights); | ||
}; | ||
return heightOfNode(this.root); | ||
} | ||
|
||
getAllValues() { | ||
const values = []; | ||
this.traverse((node) => values.push(node.value)); | ||
return values; | ||
} | ||
|
||
findNodeWithCallback(callback) { | ||
const findNode = (node) => { | ||
if (callback(node)) return node; | ||
for (const child of node.children) { | ||
const foundNode = findNode(child); | ||
if (foundNode) return foundNode; | ||
} | ||
return null; | ||
}; | ||
return findNode(this.root); | ||
} | ||
} | ||
|
||
export default OrdinalTree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters