- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Node in Javascript
Each element in a tree is a node. We need to define a node before we proceed to define a binary tree as a tree consists of nodes. We'll create a very simple node definition that has 3 properties, namely: left, right and data.
left − This holds the reference to the left child of this node.
right − This holds the reference to the right child of this node.
data − This holds the reference to the data we want to store in this node.
Let us see the code representation of such a structure.
Examle
class Node { constructor(data, left = null, right = null) { this.data = data; this.left = left; this.right = right; } }
We've defined the Node data structure with a constructor that takes 3 properties, data left and right. We'll mostly just create a node with null left and right properties as we'll be inserting values at leaves.
For ease of use, we'll define Node as a property of the BinarySearchTree class that we'll create in order to keep this class within the place we use it.
Note that such nodes with 2 explicit left and right properties are needed for binary trees. For multiway trees like B trees or B+ trees, we define a property called children which is an array (or some other container like data structure).
- Related Articles
- Finding next greater node for each node in JavaScript
- Child node count in JavaScript?
- Replace a child node with a new node in JavaScript?
- First and last child node of a specific node in JavaScript?
- Removing a node in a Javascript Tree
- Inserting a node in a Javascript AVL Tree
- Asynchronous Functions and the Node Event Loop in Javascript
- Finding middlemost node of a linked list in JavaScript
- Remove all child elements of a DOM node in JavaScript?
- Remove the child node of a specific element in JavaScript?
- JavaScript Program for Inserting a Node in a Linked List
- Node name of an HTML element using javascript?
- Value of the class attribute node of an element in JavaScript?
- How can I remove a child node in HTML using JavaScript?
- How to convert a node list to an array in JavaScript?
