Core Java

Java Node Example

In this article, we will discuss a simple Java Node class through examples.

1. What is a Node

An Individual Node in java is a class that is used to create the individual data holding blocks for various data structures, which organize data in a nonsequential fashion.

Java Node

2. Implementations of Node class

A Node class can be customized to store one or more data fields and pointer links inside each of the individual objects, depending on the needs of the required data structure.

2.1 Node class in Linked List

In this section, we will discuss the node class used in defining a Singly Linked List.

In the Case of Singly Linked List, the Node class usually contains 2 values,

  1. Data Field, which contains the data stored in the current Node.
  2. Pointer Field of type Node, which contains the address information of the next Node in the Linked List.

The following code snippet will show the structure of the Node Class in the Singly Linked List.

SinglyLinkedListNode.java

public class SinglyLinkedListNode {
    protected int data;
    protected SinglyLinkedListNode next;

    public SinglyLinkedListNode() {
        next = null;
        data = 0;
    }

    public SinglyLinkedListNode(int d, SinglyLinkedListNode n) {
        data = d;
        next = n;
    }

    public void setLinkNext(SinglyLinkedListNode n) {
        next = n;
    }


    public SinglyLinkedListNode getLinkNext() {
        return next;
    }


    public void setData(int d) {
        data = d;
    }

    public int getData() {
        return data;
    }
}

In the above code snippet, the next is the pointer to the next node in the Singly Linked List and data is the value stored in the current node of Singly Linked List.

2.2 Node class in Doubly Linked List and Binary Tree

In this section we will discuss the node class used in defining a Doubly Linked List and Binary tree.

In case of Both DLL and Binary Tree, the Node class contains 3 values.

For Doubly Linked List, Node class usually have 3 values,

  1. Data Field, which contains the data stored in the current Node.
  2. Next Pointer Field of type Node, which contains the address information of the next Node in the Linked List.
  3. Prev Pointer Field of type Node, which contains the address information of the previous Node in the Linked List.

Following Code snippet will show the structure of the Node Class in Doubly Linked List.

DoublyLinkedListNode.java

public class DoublyLinkedListNode {
    protected int data;
    protected DoublyLinkedListNode next, prev;

    public DoublyLinkedListNode() {
        next = null;
        prev = null;
        data = 0;
    }

    public DoublyLinkedListNode(int d, DoublyLinkedListNode n, DoublyLinkedListNode p) {
        data = d;
        next = n;
        prev = p;
    }

    public void setLinkNext(DoublyLinkedListNode n) {
        next = n;
    }

    public void setLinkPrev(DoublyLinkedListNode p) {
        prev = p;
    }

    public DoublyLinkedListNode getLinkNext() {
        return next;
    }

    public DoublyLinkedListNode getLinkPrev() {
        return prev;
    }

    public void setData(int d) {
        data = d;
    }

    public int getData() {
        return data;
    }
}

In the above code snippet, the next is the pointer to the next node and prev is the pointer to the previous node in the Doubly Linked List and data is the value stored in the current node of Doubly Linked List.

For Binary tree, Node class usually have 3 values,

  1. Data Field, which contains the data stored in the current Node.
  2. Left Pointer Field of type Node, which contains the address information of the root Node of Left Subtree in Binary Tree or null for leaf pointer.
  3. Right Pointer Field of type Node, which contains the address information of the root Node of Right Subtree in Binary Tree or null for leaf pointer.

The following Code snippet will show the structure of the Node Class in Binary Tree.

BinaryTreeNode.java

class BinaryTreeNode {
    int value;
    BinaryTreeNode left;

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public BinaryTreeNode getLeft() {
        return left;
    }

    public void setLeft(BinaryTreeNode left) {
        this.left = left;
    }

    public BinaryTreeNode getRight() {
        return right;
    }

    public void setRight(BinaryTreeNode right) {
        this.right = right;
    }

    BinaryTreeNode right;

    BinaryTreeNode(int value) {
        this.value = value;
        right = null;
        left = null;
    }
}

In the above code snippet, the right is the pointer to the root of the right subtree node and left is the pointer to the root of the left subtree of the Binary Tree and value is the value stored in the current node of the Binary Tree.

2.3 Node class in N-Ary Tree and Trie

In this section, we will discuss the node class used in defining an N-ary tree and Trie.

In the Case of the N-ary tree and Trie, the Node class usually contains 2 values,

  1. Data Field, which contains the data stored in the current Node.
  2. Pointer Field, which is an array of items of type Node, where each item contains the address information of the next Node in the Linked List.

The following Code snippet will show the structure of the Node Class in N-ary Tree and Trie Linked List.

NaryTreeNode.java

import java.util.ArrayList;
import java.util.List;

public class NaryTreeNode {

    public NaryTreeNode parentNode; // The parent of the current node
    public List<NaryTreeNode> childList; // The children's of the current node
    public String dataValue;

    public NaryTreeNode getParentNode() {
        return parentNode;
    }

    public void setParentNode(NaryTreeNode parentNode) {
        this.parentNode = parentNode;
    }

    public List<NaryTreeNode> getChildList() {
        return childList;
    }

    public void setChildList(List<NaryTreeNode> childList) {
        this.childList = childList;
    }

    public String getDataValue() {
        return dataValue;
    }

    public void String(String dataValue) {
        this.dataValue = dataValue;
    }

    public static int getMaxNumberOfChildren() {
        return maxNumberOfChildren;
    }

    public static void setMaxNumberOfChildren(int maxNumberOfChildren) {
        NaryTreeNode.maxNumberOfChildren = maxNumberOfChildren;
    }

    public static int maxNumberOfChildren; // Equal to the n-arity;

    public NaryTreeNode(String dataValue) {
        this.dataValue = dataValue;
        childList = new ArrayList<NaryTreeNode>(maxNumberOfChildren);
    }

    public void addChild(NaryTreeNode childNaryTreeNode, int position) throws Exception {
        if (position >= maxNumberOfChildren - 1) {
            throw new Exception("Max number of childeren reached");
        } else {
            System.out.println("this.children=" + this.childList);
            if (this.childList.get(position) != null) {
                // There is already a child node on this position; throw some error;
            } else {
                childNaryTreeNode.parentNode = this;
                this.childList.set(position, childNaryTreeNode);
            }
        }
    }
}

In the above code snippet, the parentNode stores the parent information of the current node, childList stores the list of all the children of the current node and dataValue stores the information stored in the current node.

TrieNode.java

public class TrieNode {
    final int ALPHABET_SIZE = 26;
    TrieNode[] trieChildList = new TrieNode[ALPHABET_SIZE];

    boolean isEndOfWord; // used in implementation of Prefix Search, signifies the end of word.

    public TrieNode[] getTrieChildList() {
        return trieChildList;
    }

    public void setTrieChildList(TrieNode[] trieChildList) {
        this.trieChildList = trieChildList;
    }

    public boolean isEndOfWord() {
        return isEndOfWord;
    }

    public void setEndOfWord(boolean endOfWord) {
        isEndOfWord = endOfWord;
    }

    TrieNode() {
        isEndOfWord = false;
        for (int i = 0; i < ALPHABET_SIZE; i++)
            trieChildList[i] = null;
    }
}

In the above code snippet, the trieChildList is the list of all the child nodes of the current node in Trie.

4. Applications of Node class

During the course of this article, we have seen various use-case featuring the Node class. Java Node class is actually being used as a generic name for any object template which is used in a building block for any non-sequential Data structure.

6. Download the source code

Download
You can download the full source code of this example here: Java Node Example

Last updated on Aug. 11th, 2021

Abhinav Nath Gupta

Abhinav holds a Master degree in Computer Science and Engineering from the National Institute of Technology Karnataka. He has finished his graduation from Information Technology Department in the Anand Engineering College, Agra. During his studies he has been involved with a large number of projects ranging from Networking and Cryptography. He works as a software development engineer at a software development firm in bengaluru where he is mainly involved with projects based on Nodejs. He is interested in cryptography, data security, cryptocurrency and cloud computing, and published articles regarding these topics. He can be reached at abhi.aec89@gmail.com.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button