Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

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 BinaryTree { 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()); } }
Develop 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 p) ( //returns true if p is root and false otherwise. public boolean isRoot(Node p) // if p is a leaf node deletes the node from the tree returning //true, otherwise returns false. public boolean deleteLeaf (Node p) //copies information in the binary tree into a linked list. The //information is the "info" field. Use preorder traversal to get info // Create additional methods or extra declare data structures // if and when needed. public LinkedList cloneTOLL ()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions

Question

What is management growth? What are its factors

Answered: 1 week ago

Question

2 The main characteristics of the market system.

Answered: 1 week ago