Please help me make sure existing code is correct and then figure out the rest o
ID: 3704852 • Letter: P
Question
Please help me make sure existing code is correct and then figure out the rest of the code that I bolded in treenode.java. Thank you!
public class TreeNode<E extends Comparable<E>>
{
E data;
TreeNode<E> parent;
TreeNode<E> left;
TreeNode<E> right;
/**
* constructor construct a tree node with every field as null
*/
public TreeNode()
{
}
/**
* constructor construct a tree node with all node referece null to hold given data
* @param data The given data of type E
*/
public TreeNode(E data)
{
this.data = data;
this.left = null;
this.right = null;
this.parent = null;
}
/**
* set this node's data as given data
* @param data The given data of type E
*/
public void setData(E data)
{
this.data = data;
}
/**
* get this node's data
* @return the node's data of type E
*/
public E getData()
{
return data;
}
/**
* set this node's parent node as given node
* @param parent The given node
*/
public void setParent(TreeNode<E> parent)
{
this.parent = parent;
}
/**
* get this node's parent node
* @return the node's parent node
*/
public TreeNode<E> getParent()
{
return parent;
}
/**
* set this node's left child node as given node
* @param left The given node
*/
public void setLeft(TreeNode<E> left)
{
this.left = left;
}
/**
* get this node's left child node
* @return the node's left child node
*/
public TreeNode<E> getLeft()
{
return left;
}
/**
* set this node's right child node as given node
* @param right The given node
*/
public void setRight(TreeNode<E> right)
{
this.right = right;
}
/**
* get this node's right child node
* @return the node's right child node
*/
public TreeNode<E> getRight()
{
return right;
}
/**
* check if this node is the left child of its parent
* @return true if this node is the left child of its parent; false otherwise.
* If this node is root, i.e. it has no parent, it also return false
*/
public boolean isLeftChild(){
}
/**
* check if this node is the right child of its parent
* @return true if this node is the right child of its parent; false otherwise.
* If this node is root, i.e. it has no parent, it also return false
*/
public boolean isRightChild(){
}
/**
* check if this node is a leaf
* @return true if this node is a leaf; false otherwise.
*/
public boolean isLeaf(){
return left == null && right == null; //Is this correct?
}
/**
* check if this node is a root
* @return true if this node is a root; false otherwise.
*/
public boolean isRoot(){
return parent == null;
}
}
Explanation / Answer
public class TreeNode { E data; TreeNode parent; TreeNode left; TreeNode right; /** * constructor construct a tree node with every field as null */ public TreeNode() { parent = null; data = null; left = null; right = null; } /** * constructor construct a tree node with all node referece null to hold given data * @param data The given data of type E */ public TreeNode(E data) { this.data = data; this.left = null; this.right = null; this.parent = null; } /** * set this node's data as given data * @param data The given data of type E */ public void setData(E data) { this.data = data; } /** * get this node's data * @return the node's data of type E */ public E getData() { return data; } /** * set this node's parent node as given node * @param parent The given node */ public void setParent(TreeNode parent) { this.parent = parent; } /** * get this node's parent node * @return the node's parent node */ public TreeNode getParent() { return parent; } /** * set this node's left child node as given node * @param left The given node */ public void setLeft(TreeNode left) { this.left = left; } /** * get this node's left child node * @return the node's left child node */ public TreeNode getLeft() { return left; } /** * set this node's right child node as given node * @param right The given node */ public void setRight(TreeNode right) { this.right = right; } /** * get this node's right child node * @return the node's right child node */ public TreeNode getRight() { return right; } /** * check if this node is the left child of its parent * @return true if this node is the left child of its parent; false otherwise. * If this node is root, i.e. it has no parent, it also return false */ public boolean isLeftChild(){ return parent.left == this; } /** * check if this node is the right child of its parent * @return true if this node is the right child of its parent; false otherwise. * If this node is root, i.e. it has no parent, it also return false */ public boolean isRightChild(){ return parent.right == this; } /** * check if this node is a leaf * @return true if this node is a leaf; false otherwise. */ public boolean isLeaf(){ return left == null && right == null; // Yes this is correct } /** * check if this node is a root * @return true if this node is a root; false otherwise. */ public boolean isRoot(){ return parent == null; } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.