Theory
A tree is a hierarchical data structure made up of nodes connected by edges. Important terms include root, parent, child, leaf, depth and height. Trees are widely used in file systems, databases and searching.
Example
class TreeNode {
constructor(value) {
this.value = value;
this.children = [];
}
}
const root = new TreeNode("Root");
root.children.push(new TreeNode("Child"));
console.log(root.value);
Expected Output
Root
Practice
Create a simple tree with one root and three child nodes.
Interview Questions
- What is a tree?
- What is the root node?
- What is a leaf node?
- What is the difference between a tree and graph?