Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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; 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
public class BinaryTreeDevelop code and add the following methods to the BinaryTree.java class and ensure they work correctly. // returns true if p is leaf and false otherwise public boolean isLeaf(Node{ Node root; int numNodes; 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 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); } public void buildBT() { BinaryTree bt= new BinaryTree (0); } public void preOrder(Node t){ if (t==null) return; System.out.println(t.getInfo()); preOrder (t.getLeft()); preOrder(t.getRight()); } 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()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started