Question
In this lab you will modify a binary search tree so that the nodes are threaded in order. In-order traversal will be implemented using the
In this lab you will modify a binary search tree so that the nodes are threaded in order. In-order traversal will be implemented using the thread. A client using a binary search tree will be developed.
Codes are provided below:
Identifiers.java
import TreePackage.*; import java.io.*; import java.util.*;
/** * Find the unique identifiers from a .java file. * * @author Charles Hoot * @version 4.0 */ public class Identifiers {
public static void main(String args[]) {
String fileName = getFileName(); System.out.println(); BinarySearchTree
//ADD CODE TO READ THE FILE AND CONSTRUCT THE BINARY SEARCH TREE } catch(IOException e) { System.out.println("There was an error with System.in"); System.out.println(e.getMessage()); } return possible; } private static String getFileName() { Scanner input; String inString = "data.txt"; try { input = new Scanner(System.in); System.out.println("Please enter the name of the file:"); inString = input.next(); } catch(Exception e) { System.out.println("There was an error with System.in"); System.out.println(e.getMessage()); System.out.println("Will try the default file name data.txt"); } return inString; } }
BinaryNode.java
package TreePackage;
/** * An implementation of the ADT Binary Node. * * This code is from Chapter 24 of * Data Structures and Abstractions with Java 4/e * by Carrano * * @version 4.0 */ class BinaryNode
private T data; private BinaryNode
public BinaryNode() { this(null); // Call next constructor } // end default constructor
public BinaryNode(T dataPortion) { this(dataPortion, null, null); // Call next constructor } // end constructor
public BinaryNode(T dataPortion, BinaryNode
// ADD TWO MORE CONSTRUCTORS
/** * Retrieves the data portion of this node. * * @return The object in the data portion of the node. */ public T getData() { return data; } // end getData
/** * Sets the data portion of this node. * * @param newData The data object. */ public void setData(T newData) { data = newData; } // end setData
/** * Retrieves the left child of this node. * * @return The node that is this node's left child. */ public BinaryNode
/** * Sets this node's left child to a given node. * * @param newLeftChild A node that will be the left child. */ public void setLeftChild(BinaryNode
/** * Detects whether this node has a left child. * * @return True if the node has a left child. */ public boolean hasLeftChild() { return leftChild != null; } // end hasLeftChild
/** * Retrieves the right child of this node. * * @return The node that is this node's right child. */ public BinaryNode
/** * Sets this nodes's right child to a given node. * * @param newRightChild A node that will be the right child. */ public void setRightChild(BinaryNode
/** * Detects whether this node has a right child. * * @return True if the node has a right child. */ public boolean hasRightChild() { return rightChild != null; } // end hasRightChild
/** * Detects whether this node is a leaf. * * @return True if the node is a leaf. */ public boolean isLeaf() { return (leftChild == null) && (rightChild == null); } // end isLeaf
/** * Computes the height of the subtree rooted at this node. * * @return The height of the subtree rooted at this node. */ public int getHeight() { return getHeight(this); // Call private getHeight } // end getHeight
private int getHeight(BinaryNode
/** * Counts the nodes in the subtree rooted at this node. * * @return The number of nodes in the subtree rooted at this node. */ public int getNumberOfNodes() { int leftNumber = 0; int rightNumber = 0;
if (leftChild != null) { leftNumber = leftChild.getNumberOfNodes(); }
if (rightChild != null) { rightNumber = rightChild.getNumberOfNodes(); }
return 1 + leftNumber + rightNumber; } // end getNumberOfNodes
/** * Copies the subtree rooted at this node. * * @return The root of a copy of the subtree rooted at this node. */ public BinaryNode
return newRoot; } // end copy
// ADD IN ANOTHER COPY THAT TAKES A PARENT REFERENCE // ADD IN ACCESSORS FOR THE PARENT REFERENCE // AND THREAD REFERENCE
} // end BinaryNode
BinarySearchTree.java
package TreePackage; /** An implementation of the ADT Binary Search Tree. * * This code is from Chapter 25 of * Data Structures and Abstractions with Java 4/e * by Carrano * * @version 4.0 */ public class BinarySearchTree
T result = null; boolean found = false; while (!found) { T currentEntry = currentNode.getData(); int comparison = newEntry.compareTo(currentEntry); if (comparison == 0) { // newEntry matches currentEntry; // return and replace currentEntry found = true; result = currentEntry; currentNode.setData(newEntry); } else if (comparison < 0) { if (currentNode.hasLeftChild()) currentNode = currentNode.getLeftChild(); else { // Add node on the left side found = true; // CHANGE THIS TO SET PARENT POINTERS AND FIX UP THREADS currentNode.setLeftChild(new BinaryNode
} // end if } else { assert comparison > 0; if (currentNode.hasRightChild()) currentNode = currentNode.getRightChild(); else { // Add node on the right side found = true; // CHANGE THIS TO SET PARENT POINTERS AND FIX UP THREADS // currentNode.setRightChild(new BinaryNode
private NodePair findNode(T entry) { BinaryNode
if (found) result = new NodePair(currentNode, parentNode); // found entry is currentNode.getData() return result; } // end findNode
private NodePair getNodeToRemove(BinaryNode
// Remove this node directly, it has at most 1 child private void removeNode(BinaryNode
if (nodeToRemove.hasLeftChild()) { childNode = nodeToRemove.getLeftChild(); } else { childNode = nodeToRemove.getRightChild(); } // Assertion: If nodeToRemove is a leaf, childNode is null assert (nodeToRemove.isLeaf() && childNode == null) || !nodeToRemove.isLeaf(); if (nodeToRemove == getRootNode()) { setRootNode(childNode); } else if (parentNode.getLeftChild() == nodeToRemove) { parentNode.setLeftChild(childNode); } else { parentNode.setRightChild(childNode); } } // end removeNode
private class NodePair { private BinaryNode
public NodePair() { first = null; second = null; }
public NodePair(BinaryNode
BinaryTree.java
package TreePackage;
/** * An implementation of the ADT Binary Tree. * * This code is from Chapter 24 of * Data Structures and Abstractions with Java 4/e * by Carrano * * Modified to use Java's built in Stack class by Charles Hoot * * @version 4.0 */ import java.util.*;
public class BinaryTree
private BinaryNode
public BinaryTree() { root = null; } // end default constructor
public BinaryTree(T rootData) { root = new BinaryNode
public BinaryTree(T rootData, BinaryTree
public void setTree(T rootData) { root = new BinaryNode
public void setTree(T rootData, BinaryTreeInterface
private void privateSetTree(T rootData, BinaryTree
if ((leftTree != null) && !leftTree.isEmpty()) { root.setLeftChild(leftTree.root); // ADD CODE TO SET THE PARENT AND THREAD OF THE LEFT CHILD }
if ((rightTree != null) && !rightTree.isEmpty()) { if (rightTree != leftTree) { root.setRightChild(rightTree.root); } else { root.setRightChild(rightTree.root.copy()); } // ADD CODE TO SET THE PARENT OF THE RiGHT CHILD // SET THE THREAD OUT OF THE ROOT
} // end if
if ((leftTree != null) && (this != leftTree)) { leftTree.clear(); }
if ((rightTree != null) && (this != rightTree)) { rightTree.clear(); }
} // end privateSetTree
public T getRootData() { if (isEmpty()) { throw new EmptyTreeException("Empty tree for operation getRootData"); } else { return root.getData(); } } // end getRootData
public boolean isEmpty() { return root == null; } // end isEmpty
public void clear() { root = null; } // end clear
protected void setRootData(T rootData) { root.setData(rootData); } // end setRootData
protected void setRootNode(BinaryNode
protected BinaryNode
public int getHeight() { // Modified from Carrano to return 0 if the tree is empty if (root == null) { return 0; } else { return root.getHeight(); } } // end getHeight
public int getNumberOfNodes() { // Modified from Carrano to return 0 if the tree is empty if (root == null) { return 0; } else { return root.getNumberOfNodes(); } } // end getNumberOfNodes
public void inorderTraverse() { inorderTraverse(root); } // end inorderTraverse
private void inorderTraverse(BinaryNode
// The inorder Iterator that uses the stack will be replaced // by one that uses threads private class InorderIterator implements Iterator
private Stack
public InorderIterator() { nodeStack = new Stack
public boolean hasNext() { return !nodeStack.isEmpty() || (currentNode != null); } // end hasNext
public T next() { BinaryNode
// Find leftmost node with no left child while (currentNode != null) { nodeStack.push(currentNode); currentNode = currentNode.getLeftChild(); } // end while
// Get leftmost node, then move to its right subtree if (!nodeStack.isEmpty()) { nextNode = nodeStack.pop(); assert nextNode != null; // Since nodeStack was not empty // before the pop currentNode = nextNode.getRightChild(); } else { throw new NoSuchElementException(); }
return nextNode.getData(); } // end next
public void remove() { throw new UnsupportedOperationException(); } // end remove
} // end InorderIterator
/* Create an inorder iterator. * @return The iterator. */ public Iterator
// Only the one iterator will be implemented by this code public Iterator
public Iterator
public Iterator
// ADD IN METHODS FOR ACCESSING THE TREE
}
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