
Let’s review data structures! It’s fun and exciting! In this section you’ll review and then build your very own data structures along with me! I’ll be using JavaScript to code mine, but feel free to use whatever language you like.
The Code
To get you off the ground, feel free to use the code bits below. The first is for a basic tree Node:
class Node{
constructor(val){
this.value = val;
this.children = [];
}
addChild(val){
const newNode = new (val);
this.children.push(newNode);
return newNode;
}
}
module.exports = Node;
Here’s one for a BinaryTreeNode
class BinaryTreeNode{
constructor(val){
this.value = val;
this.right = null;
this.left = null;
}
addRight(val){
const newNode = new BinaryTreeNode(val);
this.right = newNode;
return newNode;
}
addLeft(val){
const newNode = new BinaryTreeNode(val);
this.left = newNode;
return newNode;
}
}