Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the StringTree.java to add a method to traversal the tree using java.util.Stack instead of recursion. At the end, display the tree to ensure the

Modify the StringTree.java to add a method to traversal the tree using java.util.Stack instead of recursion. At the end, display the tree to ensure the order of records is the same as the recursive method. Download the stringFile.txt from Moodle as input for the database.

//******************************************************************** // StringTree.java // //********************************************************************

import java.util.*;

public class StringTree { private Node root;

//---------------------------------------------------------------- // Creates an initially empty tree. //---------------------------------------------------------------- public StringTree() { root = null; }

//---------------------------------------------------------------- // Adds a string to the tree. //---------------------------------------------------------------- public void addString (String str) { root = addStringToSubTree(str, root); }

//---------------------------------------------------------------- // Adds a string to the subtree with the given root node //---------------------------------------------------------------- private Node addStringToSubTree (String str, Node node) { Node result = node; if (node == null) result = new Node(str); // If the new string comes before the string in the node, add // the new string to the left child. Otherwise, add it to the // right child. else if (str.compareTo(node.value) < 0) node.left = addStringToSubTree(str, node.left); else node.right = addStringToSubTree(str, node.right);

return result; }

//---------------------------------------------------------------- // Prints the result of a depth-first traversal of the tree using // recursion. //---------------------------------------------------------------- public void traverseWithRecursion() { traverseWithRecursion(root); }

//---------------------------------------------------------------- // Prints the elements in the specified tree using recursion. //---------------------------------------------------------------- private void traverseWithRecursion (Node node) { if (node != null) { traverseWithRecursion (node.left);

System.out.print (node.value+" ");

traverseWithRecursion (node.right); } } }

//******************************************************************** // Node for a binary tree of Strings. //******************************************************************** class Node { public String value; public Node left; public Node right;

public Node (String value) { this.value = value; this.left = left; this.right = right; } }

stringFile.txt

CV06C1250B MA06C1042A MA06C1043A SU04D1043B SU04D1042B CV06C1250A CV06D1250B HY09F3014A KI04D1297A

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

Database Concepts

Authors: David Kroenke, David J. Auer

3rd Edition

0131986252, 978-0131986251

More Books

Students also viewed these Databases questions

Question

To solve p + 3q = 5z + tan( y - 3x)

Answered: 1 week ago