如何实现 is empty for a Node class
How to implement is empty for a Node class
我有以下表示树节点的代码:
public class Node {
/**
* All the attributes.
* @param value: the generic value assigned to this node.
* @param leftNode: the left leaf of this node.
* @param rightNode: the right leaf of this node.
*/
private Map map;
private Node leftNode;
private Node rightNode;
/**
* Constructor Node.
* Creates a node with it's leafs being null.
* @param map
*/
public Node(Map map){
this(map, null, null);
}
/**
* Constructor Node.
* Creates a node with it's leafs assigned accordingly.
* @param value
* @param left_node
* @param right_node
*/
public Node(Map value, Node left_node, Node right_node){
this.leftNode = left_node;
this.rightNode = right_node;
this.map = value;
}
/**
* Returns the value of the node.
* @return
*/
public Map getValue(){
return this.map;
}
/**
* Returns the left child of the node.
* @return
*/
public Node getLeftChild(){
return this.leftNode;
}
/**
* Returns the right child of the node.
* @return
*/
public Node getRightChild(){
return this.rightNode;
}
/**
* Removes the value of the node and returns the value.
* @return
*/
public Map removeValue(){
Map temp = this.map;
this.map = null;
return temp;
}
/**
* Removes the left child of the node and returns the child node.
* @return
*/
public Node removeLeftChild(){
Node temp = this.leftNode;
this.leftNode = null;
return temp;
}
/**
* Removes the right child of the node and returns the child node.
* @return
*/
public Node removeRightChild(){
Node temp = this.rightNode;
this.rightNode = null;
return temp;
}
/**
* Sets the value of the node to a given value.
* @throws IllegalArgumentException if the value is empty
* @param map
*/
public void setValue(Map map){
if (this.map == null)
throw new IllegalArgumentException("The value is empty.");
this.map = map;
}
/**
* Sets the left child of the node to a given node.
* @throws IllegalArgumentException if the node is empty
* @param node
*/
public void setLeftChild(Node node){
if (node.isEmpty())
throw new IllegalArgumentException("The node is empty.");
this.leftNode = node;
}
/**
* Sets the right child of the node to a given node.
* @throws IllegalArgumentException if the node is empty
* @param node
*/
public void setRightChild (Node node){
if (node.isEmpty())
throw new IllegalArgumentException("The node is empty.");
this.rightNode = node;
}
/**
* Checks if the following Node is empty.
* Node can be null or it's value being null for it to be empty.
* @return
*/
public boolean isEmpty(){
if (this == null){
return true;
}
else return false;
}
}
但是,当我尝试运行以下代码时(我测试了其他情况并且我的树有效),这将给我一个 NullPointerException。
主要
{...
Node test2_2 = new Node(new Map<> (5, 2));
System.out.println("Object left child is: " + test2_2.getLeftChild().isEmpty());
}
线程异常 "main" java.lang.NullPointerException
在 MainPackage.Main.main(Main.java:35)
C:\Users\mehrz\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java 返回: 1
有什么建议吗?
在你的构造函数中,你为 Node 属性设置了 null 而不是通过以下方式调整你的构造函数:
/**
* Constructor Node.
* Creates a node with it's leafs being null.
* @param map
*/
public Node(Map map){
this(map, new Node(), new Node());
}
我有以下表示树节点的代码:
public class Node {
/**
* All the attributes.
* @param value: the generic value assigned to this node.
* @param leftNode: the left leaf of this node.
* @param rightNode: the right leaf of this node.
*/
private Map map;
private Node leftNode;
private Node rightNode;
/**
* Constructor Node.
* Creates a node with it's leafs being null.
* @param map
*/
public Node(Map map){
this(map, null, null);
}
/**
* Constructor Node.
* Creates a node with it's leafs assigned accordingly.
* @param value
* @param left_node
* @param right_node
*/
public Node(Map value, Node left_node, Node right_node){
this.leftNode = left_node;
this.rightNode = right_node;
this.map = value;
}
/**
* Returns the value of the node.
* @return
*/
public Map getValue(){
return this.map;
}
/**
* Returns the left child of the node.
* @return
*/
public Node getLeftChild(){
return this.leftNode;
}
/**
* Returns the right child of the node.
* @return
*/
public Node getRightChild(){
return this.rightNode;
}
/**
* Removes the value of the node and returns the value.
* @return
*/
public Map removeValue(){
Map temp = this.map;
this.map = null;
return temp;
}
/**
* Removes the left child of the node and returns the child node.
* @return
*/
public Node removeLeftChild(){
Node temp = this.leftNode;
this.leftNode = null;
return temp;
}
/**
* Removes the right child of the node and returns the child node.
* @return
*/
public Node removeRightChild(){
Node temp = this.rightNode;
this.rightNode = null;
return temp;
}
/**
* Sets the value of the node to a given value.
* @throws IllegalArgumentException if the value is empty
* @param map
*/
public void setValue(Map map){
if (this.map == null)
throw new IllegalArgumentException("The value is empty.");
this.map = map;
}
/**
* Sets the left child of the node to a given node.
* @throws IllegalArgumentException if the node is empty
* @param node
*/
public void setLeftChild(Node node){
if (node.isEmpty())
throw new IllegalArgumentException("The node is empty.");
this.leftNode = node;
}
/**
* Sets the right child of the node to a given node.
* @throws IllegalArgumentException if the node is empty
* @param node
*/
public void setRightChild (Node node){
if (node.isEmpty())
throw new IllegalArgumentException("The node is empty.");
this.rightNode = node;
}
/**
* Checks if the following Node is empty.
* Node can be null or it's value being null for it to be empty.
* @return
*/
public boolean isEmpty(){
if (this == null){
return true;
}
else return false;
}
}
但是,当我尝试运行以下代码时(我测试了其他情况并且我的树有效),这将给我一个 NullPointerException。
主要
{...
Node test2_2 = new Node(new Map<> (5, 2));
System.out.println("Object left child is: " + test2_2.getLeftChild().isEmpty());
}
线程异常 "main" java.lang.NullPointerException 在 MainPackage.Main.main(Main.java:35) C:\Users\mehrz\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java 返回: 1
有什么建议吗?
在你的构造函数中,你为 Node 属性设置了 null 而不是通过以下方式调整你的构造函数:
/**
* Constructor Node.
* Creates a node with it's leafs being null.
* @param map
*/
public Node(Map map){
this(map, new Node(), new Node());
}