Question: Node.java public class Node { private E info; private Node parent; private Node left; private Node right; public Node(E val) { info=val; parent =null; left

 Node.java public class Node { private E info; private Node parent;

Node.java public class Node { private E info; private Node parent; private Node left; private Node right; public Node(E val) { info=val; parent =null; left =null; right = null; } public E getInfo() { return this.info; } public void setInfo(E val) { this.info = val; } public void setLeft(Node v ) { this.left =v; } public void setRight(Node v) { this.right=v; } public void setParent(Node v) { this.parent=v; } public Node getParent() { return this.parent; } public Node getLeft() { return this.left; } public Node getRight() { return this.right; } }

BinaryTree.java

import java.util.ArrayList; import java.util.LinkedList;

public class BinaryTree {

Node root; int numNodes; ArrayList list = new ArrayList();

public BinaryTree(E val) { root = new Node(val); numNodes = 1; }

public void setLeftBT(Node p, Node c) {

p.setLeft(c); c.setParent(p); numNodes++; }

public void setRightBT(Node p, Node c) {

p.setRight(c); c.setParent(p); numNodes++; }

public boolean isLeaf(Node p) { if (p == null) { return false; } if (p.getLeft() == null && p.getRight() == null) { return true; } return false; }

public boolean isSubRoot(Node p) {

if (p.getLeft() != null || p.getRight() != null) { return true; } return false; }

public boolean isMainRoot(Node p) {

if (p.getParent() == null) { return true; } return false; }

public boolean deleteLeaf(Node p) { if (p.getParent() != null) { p.setParent(null); --numNodes; return true; } return false; }

public LinkedList cloneToLL(ArrayList list) { LinkedList linkedList = new LinkedList(); for (int i = 0; i

public static void main(String[] args) { BinaryTree bt = new BinaryTree("cat"); //bt.root = new Node("cat"); Node n1 = new Node("dog"); Node n2 = new Node("rabbit"); // make n1 left child of the root bt.setLeftBT(bt.root, n1); // make n2 right child of the root bt.setRightBT(bt.root, n2); Node n3 = new Node("mouse"); Node n4 = new Node("hamster"); bt.setLeftBT(n1, n3); bt.setRightBT(n2, n4);

bt.postOrder(bt.root);

System.out.println(" Is Leaf Node : " + bt.isLeaf(n2)); System.out.println(" Is Sub Root : " + bt.isSubRoot(n4)); System.out.println(" Is Main Root : " + bt.isMainRoot(bt.root)); //Node deletion if (bt.isLeaf(n2)) { bt.deleteLeaf(n2); } else { System.out.println("Not Leaf Node"); } //copy System.out.println("After Cloning : " + bt.cloneToLL(bt.preOrder(bt.root)));

}

public void buildBT() { BinaryTree bt = new BinaryTree(0);

}

public ArrayList preOrder(Node t) { if (t == null) { return null; } System.out.println(t.getInfo()); list.add(t.getInfo()); preOrder(t.getLeft()); preOrder(t.getRight()); return list; }

public void postOrder(Node t) { if (t == null) { return; } postOrder(t.getLeft()); postOrder(t.getRight()); System.out.println(t.getInfo()); }

public void inOrder(Node t) {

if (t == null) { return; } inOrder(t.getLeft()); System.out.println(t.getInfo()); inOrder(t.getRight()); } }

2. Use ArrayList) to build a binaryTree of nodes starting with root value 0, and a chain of left child, left grand child, etc, with values 1,2,3,.., 15. Add the following method to the BinaryTree.java class public static void buildBTO ( Could you try to do it without using ArrayList or any kind of data structure? 2. Use ArrayList) to build a binaryTree of nodes starting with root value 0, and a chain of left child, left grand child, etc, with values 1,2,3,.., 15. Add the following method to the BinaryTree.java class public static void buildBTO ( Could you try to do it without using ArrayList or any kind of data structure

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!