Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Tree Data Structure in Javascript
The tree data structure represents hierarchical relationships like organization charts, file systems, and DOM elements. A tree consists of nodes connected in a parent-child relationship, where each node has a value and references to its children, with no duplicate references.
Tree Terminology
Understanding key tree terms is essential:
- Root: The top node with no parent
- Parent: A node that has children
- Child: A node connected to a parent above it
- Leaf: A node with no children
- Height: Maximum depth from root to any leaf
- Depth: Number of edges from root to a specific node
Basic Tree Implementation
Let's create a simple tree node structure:
class TreeNode {
constructor(value) {
this.value = value;
this.children = [];
}
addChild(child) {
this.children.push(child);
}
}
// Create a tree structure
let root = new TreeNode("CEO");
let manager1 = new TreeNode("Manager 1");
let manager2 = new TreeNode("Manager 2");
let employee1 = new TreeNode("Employee 1");
let employee2 = new TreeNode("Employee 2");
root.addChild(manager1);
root.addChild(manager2);
manager1.addChild(employee1);
manager1.addChild(employee2);
console.log("Root:", root.value);
console.log("Children of root:", root.children.map(child => child.value));
Root: CEO Children of root: [ 'Manager 1', 'Manager 2' ]
Tree Traversal Methods
Trees can be traversed in different ways. Here are the most common approaches:
Depth-First Search (DFS)
function traverseDFS(node, result = []) {
if (!node) return result;
result.push(node.value);
for (let child of node.children) {
traverseDFS(child, result);
}
return result;
}
let dfsResult = traverseDFS(root);
console.log("DFS Traversal:", dfsResult);
DFS Traversal: [ 'CEO', 'Manager 1', 'Employee 1', 'Employee 2', 'Manager 2' ]
Breadth-First Search (BFS)
function traverseBFS(root) {
if (!root) return [];
let result = [];
let queue = [root];
while (queue.length > 0) {
let current = queue.shift();
result.push(current.value);
for (let child of current.children) {
queue.push(child);
}
}
return result;
}
let bfsResult = traverseBFS(root);
console.log("BFS Traversal:", bfsResult);
BFS Traversal: [ 'CEO', 'Manager 1', 'Manager 2', 'Employee 1', 'Employee 2' ]
Binary Tree Implementation
Binary trees are a special case where each node has at most two children:
class BinaryTreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
// Create binary tree
let binaryRoot = new BinaryTreeNode(10);
binaryRoot.left = new BinaryTreeNode(5);
binaryRoot.right = new BinaryTreeNode(15);
binaryRoot.left.left = new BinaryTreeNode(3);
binaryRoot.left.right = new BinaryTreeNode(7);
// In-order traversal (Left, Root, Right)
function inOrderTraversal(node, result = []) {
if (node) {
inOrderTraversal(node.left, result);
result.push(node.value);
inOrderTraversal(node.right, result);
}
return result;
}
console.log("In-order traversal:", inOrderTraversal(binaryRoot));
In-order traversal: [ 3, 5, 7, 10, 15 ]
Tree Search Operations
Searching for specific values in a tree:
function searchTree(node, target) {
if (!node) return false;
if (node.value === target) {
return true;
}
for (let child of node.children) {
if (searchTree(child, target)) {
return true;
}
}
return false;
}
console.log("Search for 'Employee 1':", searchTree(root, "Employee 1"));
console.log("Search for 'CEO':", searchTree(root, "CEO"));
console.log("Search for 'Unknown':", searchTree(root, "Unknown"));
Search for 'Employee 1': true Search for 'CEO': true Search for 'Unknown': false
Common Use Cases
| Use Case | Tree Type | Example |
|---|---|---|
| File System | General Tree | Folders and files |
| Organization Chart | General Tree | Employee hierarchy |
| Database Indexing | B-Tree | Fast data retrieval |
| Decision Making | Binary Tree | Yes/No decisions |
Conclusion
Trees are fundamental hierarchical data structures with applications in file systems, databases, and algorithms. Understanding tree traversal and basic operations is essential for efficient data organization and retrieval.
