Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program must be written in Java This program will use the BinarySearchTree class I have provided the BinarySearchTree, BinaryTree and DataElement 1. Write the

This program must be written in Java

This program will use the BinarySearchTree class

I have provided the BinarySearchTree, BinaryTree and DataElement

1. Write the method nodeCount that takes as a a parameter a reference to the root node of a binary tree and returns the number of nodes in a binary tree. Add this method to class BinaryTree

2. Write the method leavesCount that takes as a a parameter a reference to the root node of a binary tree and returns the number of leaves and prints their values. Add this method to class BinaryTree

3. Write the method swapTrees that takes as a a parameter a reference to the root node of a binary tree and swaps all of the left and right subtrees of a binary tree.Add this method to class BinaryTree

4. Write the method singleParent that returns the number of nodes in a binary tree that have only one child. Add this method to class BinaryTree

Please explain how this is done in comments within the code im not understanding really what a binary tree does or how to implement these methods or why they need to be implemented.

public class BinarySearchTree extends BinaryTree { //default constructor //Postcondition: root = null; public BinarySearchTree() { super(); } //copy constructor  public BinarySearchTree(BinarySearchTree otherTree) { super(otherTree); } //Method to determine whether searchItem is in the binary  //search tree. //Postcondition: Returns true if searchItem is found in the  // binary search tree; otherwise, returns false.  public boolean search(DataElement searchItem) { BinaryTreeNode current; boolean found = false; if(root == null) System.out.println("Cannot search an empty tree."); else { current = root; while(current != null && !found) { if(current.info.equals(searchItem)) found = true; else if(current.info.compareTo(searchItem) > 0) current = current.llink; else current = current.rlink; }//end while }//end else return found; }//end search //Method to insert insertItem in the binary search tree. //Postcondition: If no node in the binary search tree has  // the same info as insertItem, a node with  // the info insertItem is created and inserted  // in the binary search tree.  public void insert(DataElement insertItem) { BinaryTreeNode current; //reference variable to  //traverse the tree BinaryTreeNode trailCurrent = null; //reference variable  //behind current BinaryTreeNode newNode; //reference variable to  //create the node newNode = new BinaryTreeNode(); newNode.info = insertItem.getCopy(); newNode.llink = null; newNode.rlink = null; if(root == null) root = newNode; else { current = root; while(current != null) { trailCurrent = current; if(current.info.equals(insertItem)) { System.err.print("The insert item is already in " + "the list -- duplicates are " + "not allowed."); return; } else if(current.info.compareTo(insertItem) > 0) current = current.llink; else current = current.rlink; }//end while if(trailCurrent.info.compareTo(insertItem) > 0) trailCurrent.llink = newNode; else trailCurrent.rlink = newNode; } }//end insert //Method to delete deleteItem from the binary search tree  //Postcondition: If a node with the same info as deleteItem  // is found, it is deleted from the binary  // search tree.  public void deleteNode(DataElement deleteItem) { BinaryTreeNode current; //reference variable to  //traverse the tree BinaryTreeNode trailCurrent; //reference variable  //behind current boolean found = false; if(root == null) System.err.println("Cannot delete from the empty tree."); else { current = root; trailCurrent = root; while(current != null && !found) { if(current.info.equals(deleteItem)) found = true; else { trailCurrent = current; if(current.info.compareTo(deleteItem) > 0) current = current.llink; else current = current.rlink; } }//end while if(current == null) System.out.println("The delete item is not in " + "the list."); else if(found) { if(current == root) root = deleteFromTree(root); else if(trailCurrent.info.compareTo(deleteItem) > 0) trailCurrent.llink = deleteFromTree(trailCurrent.llink); else trailCurrent.rlink = deleteFromTree(trailCurrent.rlink); }//end if } }//end deleteNode //Method to delete the node, to which p points, from the //binary search tree. //Postcondition: The node to which p points is deleted // from the binary search tree. The reference // of the root node of the binary search tree // after deletion is returned. private BinaryTreeNode deleteFromTree(BinaryTreeNode p) { BinaryTreeNode current; //reference variable to  //traverse the tree BinaryTreeNode trailCurrent; //reference variable  //behind current if(p == null) System.err.println("Error: The node to be deleted " + "is null."); else if(p.llink == null && p.rlink == null) p = null; else if(p.llink == null) p = p.rlink; else if(p.rlink == null) p = p.llink; else { current = p.llink; trailCurrent = null; while(current.rlink != null) { trailCurrent = current; current = current.rlink; }//end while p.info = current.info; if(trailCurrent == null) //current did not move;  //current == p.llink; adjust p p.llink = current.llink; else trailCurrent.rlink = current.llink; }//end else return p; }//end deleteFromTree } 

public class BinaryTree { //Definition of the node protected class BinaryTreeNode { DataElement info; BinaryTreeNode llink; BinaryTreeNode rlink; } protected BinaryTreeNode root; //default constructor  //Postcondition: root = null; public BinaryTree() { root = null; } //copy constructor public BinaryTree(BinaryTree otherTree) { if(otherTree.root == null) //otherTree is empty root = null; else root = copy(otherTree.root); } //Method to determine whether the binary tree is empty. //Postcondition: Returns true if the binary tree is empty; // otherwise, returns false.  public boolean isEmpty() { return (root == null); } //Method to do an inorder traversal of the binary tree. //Postcondition: The nodes of the binary tree are output // in the inorder sequence. public void inorderTraversal() { inorder(root); } //Method to do a preorder traversal of the binary tree. //Postcondition: The nodes of the binary tree are output // in the preorder sequence. public void preorderTraversal() { preorder(root); } //Method to do a postorder traversal of the binary tree. //Postcondition: The nodes of the binary tree are output // in the postorder sequence.  public void postorderTraversal() { postorder(root); } //Method to determine the height of the binary tree. //Postcondition: The height of the binary tree is returned.  public int treeHeight() { return height(root); } //Method to determine the number of nodes in the  //binary tree. //Postcondition: The number of nodes in the binary tree // is returned.  public int treeNodeCount() { return nodeCount(root); } //Method to determine the number of leaves in the  //binary tree. //Postcondition: The number of leaves in the binary tree // is returned.  public int treeLeavesCount() { return leavesCount(root); } //Method to destroy the binary tree. //Postcondition: root = null  public void destroyTree() { root = null; } //Method to make a copy of the binary tree  //specified by otherTree points.  //Postcondition: A copy of otherTree is assigned to // this binary tree.  public void copyTree(BinaryTree otherTree) { if(this != otherTree) //avoid self-copy { root = null; if(otherTree.root != null) //otherTree is nonempty root = copy(otherTree.root); } } //Method to make a copy of the binary tree to  //which otherTreeRoot points.  //Postcondition: A copy of the binary tree to which // otherTreeRoot is created and the reference of // the root node of the copied binary tree // is returned. private BinaryTreeNode copy(BinaryTreeNode otherTreeRoot) { BinaryTreeNode temp; if(otherTreeRoot == null) temp = null; else { temp = new BinaryTreeNode(); temp.info = otherTreeRoot.info.getCopy(); temp.llink = copy(otherTreeRoot.llink); temp.rlink = copy(otherTreeRoot.rlink); } return temp; }//end copy //Method to do an inorder traversal of the binary //tree to which p points.  //Postcondition: The nodes of the binary tree to which p // points are output in the inorder sequence.  private void inorder(BinaryTreeNode p) { if(p != null) { inorder(p.llink); System.out.print(p.info + " "); inorder(p.rlink); } } //Method to do a preorder traversal of the binary //tree to which p points.  //Postcondition: The nodes of the binary tree to which p // points are output in the preorder sequence.  private void preorder(BinaryTreeNode p) { if(p != null) { System.out.print(p.info + " "); preorder(p.llink); preorder(p.rlink); } } //Method to do a postorder traversal of the binary //tree to which p points.  //Postcondition: The nodes of the binary tree to which p // points are output in the postorder sequence.  private void postorder(BinaryTreeNode p) { if(p != null) { postorder(p.llink); postorder(p.rlink); System.out.print(p.info + " "); } } //Method to determine the height of the binary tree //to which p points.  //Postcondition: The height of the binary tree to which p // points is returned.  private int height(BinaryTreeNode p) { if(p == null) return 0; else return 1 + max(height(p.llink), height(p.rlink)); } //Method to determine the larger of x and y. //Postcondition: The larger of x and y is returned.  private int max(int x, int y) { if(x >= y) return x; else return y; } //Method to determine the number of nodes in the binary  //tree to which p points.  //Postcondition: The number of nodes in the binary tree // to which p points is returned.  private int nodeCount(BinaryTreeNode p) { System.out.println(""); return 0; } //Method to determine the number of leaves in the binary  //tree to which p points. //Postcondition: The number of leaves in the binary tree // to which p points is returned.  private int leavesCount(BinaryTreeNode p) { System.out.println(""); return 0; } } 

public abstract class DataElement { public abstract boolean equals(DataElement otherElement); //Method to determine whether two objects contain the  //same data. //Postcondition: Returns true if this object contains the  // same data as the object otherElement; // otherwise, it returns false. public abstract int compareTo(DataElement otherElement); //Method to compare two objects. //Postcondition: Returns a value < 0 if this object is  // less than the object otherElement; // Returns 0 if this object is the same as  // the object otherElement. // Returns a value > 0 if this object is  // greater than the object otherElement. public abstract void makeCopy(DataElement otherElement); //Method to copy otherElement into this object. //Postcondition: The data of otherElement is copied into // this object. public abstract DataElement getCopy(); //Method to return a copy of this object. //Postcondition: A copy of this object is created and // a reference of the copy is returned. } 

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions