Class Node

java.lang.Object
edu.neu.khoury.cs6510.f24.wp161.Node

public class Node extends Object
The Node class represents a single node in a binary tree. Each node contains an integer value, a reference to its left child, and a reference to its right child.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Node(Integer value)
    Constructs a new Node with a given integer value.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Compares this node to another object for equality.
    Returns the left child of this node.
    Returns the right child of this node.
    Returns the integer value stored in this node.
    int
    Returns a hash code value for this node.
    boolean
    Determines if this node is a leaf node.
    void
    setLeft(Node left)
    Sets the left child of this node.
    void
    setRight(Node right)
    Sets the right child of this node.
    void
    Sets the integer value of this node.
    Returns a string representation of the node, including its value, left child, and right child.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Node

      public Node(Integer value)
      Constructs a new Node with a given integer value.
      Parameters:
      value - the integer value to be stored in the node, must not be null
      Throws:
      IllegalArgumentException - if the value is null
  • Method Details

    • getValue

      public Integer getValue()
      Returns the integer value stored in this node.
      Returns:
      the value of the node
    • setValue

      public void setValue(Integer value)
      Sets the integer value of this node.
      Parameters:
      value - the new integer value to be set
    • getLeft

      public Node getLeft()
      Returns the left child of this node.
      Returns:
      the left child node, or null if no left child exists
    • setLeft

      public void setLeft(Node left)
      Sets the left child of this node.
      Parameters:
      left - the node to be set as the left child
    • getRight

      public Node getRight()
      Returns the right child of this node.
      Returns:
      the right child node, or null if no right child exists
    • setRight

      public void setRight(Node right)
      Sets the right child of this node.
      Parameters:
      right - the node to be set as the right child
    • isLeaf

      public boolean isLeaf()
      Determines if this node is a leaf node. A node is considered a leaf node if it has no left or right children.
      Returns:
      true if the node is a leaf (both left and right children are null), false otherwise
    • toString

      public String toString()
      Returns a string representation of the node, including its value, left child, and right child.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this node
    • equals

      public boolean equals(Object o)
      Compares this node to another object for equality. Two nodes are considered equal if they have the same value, left child, and right child.
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare to
      Returns:
      true if the nodes are equal, false otherwise
    • hashCode

      public int hashCode()
      Returns a hash code value for this node. The hash code is generated based on the node's value, left child, and right child.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this node