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
Node in Javascript
In JavaScript tree data structures, a node is the fundamental building block that contains data and references to other connected nodes. For binary trees, each node has three essential properties: the data it stores and references to its left and right child nodes.
Node Structure
A typical tree node contains the following components:
data ? Stores the actual value or information in the node
left ? Reference to the left child node
right ? Reference to the right child node
Creating a Node Class
Here's how to implement a basic Node class in JavaScript:
class Node {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
}
// Create a simple node
const node1 = new Node(10);
console.log("Node data:", node1.data);
console.log("Left child:", node1.left);
console.log("Right child:", node1.right);
Node data: 10 Left child: null Right child: null
Building Connected Nodes
You can create a small tree by connecting multiple nodes:
// Create nodes
const root = new Node(20);
const leftChild = new Node(10);
const rightChild = new Node(30);
// Connect nodes
root.left = leftChild;
root.right = rightChild;
console.log("Root:", root.data);
console.log("Left child:", root.left.data);
console.log("Right child:", root.right.data);
Root: 20 Left child: 10 Right child: 30
Visual Representation
Integration with Tree Structures
Nodes are typically used within larger tree data structures. Here's how a Node might be integrated into a Binary Search Tree:
class BinarySearchTree {
constructor() {
this.root = null;
}
// Inner Node class
static Node = class {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
}
insert(data) {
this.root = this._insertNode(this.root, data);
}
_insertNode(node, data) {
if (node === null) {
return new BinarySearchTree.Node(data);
}
if (data < node.data) {
node.left = this._insertNode(node.left, data);
} else {
node.right = this._insertNode(node.right, data);
}
return node;
}
}
// Example usage
const tree = new BinarySearchTree();
tree.insert(20);
tree.insert(10);
tree.insert(30);
console.log("Tree root:", tree.root.data);
console.log("Left child:", tree.root.left.data);
console.log("Right child:", tree.root.right.data);
Tree root: 20 Left child: 10 Right child: 30
Different Node Types
For different tree types, nodes may have varying structures:
Binary Tree Node ? Has left and right properties
N-ary Tree Node ? Has a children array for multiple child nodes
Linked List Node ? Has only a next property
Conclusion
Nodes are the foundation of tree data structures in JavaScript. The simple three-property design (data, left, right) provides the building blocks for implementing binary trees, binary search trees, and other hierarchical data structures efficiently.
