Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a TreeNode class with the following methods: default constructor, two ove

ID: 3575046 • Letter: C

Question

Create a TreeNode class with the following methods: default constructor, two overloaded constructors, copy constructor, getValue, getLeft, getRight, setValue, setLeft, setRight Create a BTInterface with the following abstract methods: getRoot, setRoot, isEmpty, swapSubtrees, singleParent, preorder, postOrder, inOrder, insert Create an abstract BinaryTree class that implements the interface. Include the default constructor, a private helper method called checkNode for singleParent() and toString. Make insert an abstract method. Derive a BinarySearchTree class from BinaryTree with the following methods: default constructor, overloaded constructor (use a variable length parameter), insert, Create a TreeDemo class that creates two BinarySearchTree objects. Use default and overloaded constructors. For tree one: call the methods makeTree (makes a complete tree), printTree, swapSubtrees, printTree. Simliar for tree two. Also, print out the number of single parents in each tree. Define makeTree and printTree in this class.

Explanation / Answer


   class TreeNode {
private String value;
private TreeNode left;
private TreeNode right;
   //Default Constructor
   public TreeNode(){
   System.out.println("Root is null");}
   //Overloaded Constructors
public TreeNode(String Value){
this.value=value;
}
public TreeNode(String value, TreeNode left, TreeNode right) {
this.value = value;
this.left=left;
       this.right=right;
}
//Copy constructor
TreeNode copy() {
TreeNode left = null;
TreeNode right = null;
if (this.left != null) {
left = this.left.copy();
}
if (this.right != null) {
right = this.right.copy();
}
return new TreeNode(value, left, right);
}
   //setValue
   public void setValue(String value)
   {
this.value = value;}
   //getValue
   public String getValue() {
return value;
}
//setLeft
public void setLeft(TreeNode value)
   {
this.left = value;}
   //setRight
   public void setRight(TreeNode value)
   {
this.right = value;}
   //getLeft
   public TreeNode getLeft(){return left;}
  
   //getRight
   public TreeNode getRight(){return right;}
  
}