Skip to content
Snippets Groups Projects
Commit 4964626c authored by Jenny Zhang's avatar Jenny Zhang
Browse files

Define linkSequentNode function to link a new node to an existing node, prioritizing the left side

parent 81aed659
No related branches found
No related tags found
No related merge requests found
function sequentAst(value, left, right) { // Define sequentNode constructor function
function sequentNode(value, left, right) {
this.value = value; this.value = value;
this.left = left; this.left = left;
this.right = right; this.right = right;
} }
function isSequentAst(obj) { // Define isSequentNode function to check if an object is an instance of sequentNode
return obj instanceof sequentAst && function isSequentNode(obj) {
return obj instanceof sequentNode &&
'value' in obj && 'value' in obj &&
'left' in obj && 'left' in obj &&
'right' in obj; 'right' in obj;
} }
\ No newline at end of file
// Define linkSequentNode function to link a new node to an existing node, prioritizing the left side
function linkSequentNode(existingNode, newNode) {
if (!isSequentNode(existingNode)) {
throw new Error('Existing node must be a sequentNode');
}
if (!isSequentNode(newNode)) {
throw new Error('New node must be a sequentNode');
}
// Prioritize linking to the left
if (!existingNode.left) {
existingNode.left = newNode;
} else if (!existingNode.right) {
existingNode.right = newNode;
} else {
// If both left and right nodes are occupied, recursively try to link to the left child node
linkNode(existingNode.left, newNode);
}
}
module.exports = {
sequentNode,
isSequentNode,
linkSequentNode
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment