112k views
1 vote
Follow the following instructions to write a Java program and solve the problems :

1.Write a Java method named fillTree that can fill a Binary Search Tree using 10 words that are read from the user via the keyboard. The first two words should be your first name and last name. The other 8 words can be anything of your choice. The Binary Search Tree should come from the main method. The main method calls fill Tree and pass it a Binary Search Tree object to be filled;
2.Write a Java method named displayTree. The main method will pass it the Tree created above and the way to display the Tree as a string, such as "postOrder" and "inOrder". Then displayTree will display the Tree as instructed by the main method;
3.Test the two methods fillTree and displayTree by calling them in the main method.

User Nebulae
by
8.3k points

2 Answers

5 votes

Final answer:

You need to implement a Binary Search Tree in Java with methods for insertion and traversal. The program reads 10 words from the user to fill the tree and then display it based on the order specified: 'inOrder' or 'postOrder'. The example provided is a framework that requires further implementation for the BST methods.

Step-by-step explanation:

To write a Java program that includes a method fillTree for filling a Binary Search Tree (BST) with words and another method displayTree for displaying the tree in different orders, you would first need to create a BST class with methods for inserting and traversing nodes. After this, you read input from the user to fill the tree and then display it accordingly. Below is a simplified example of how the methods can be structured:



import java.util.Scanner;

public class BSTNode {
String word;
BSTNode left, right;

// BSTNode Constructor and methods
}

public class BinarySearchTree {
private BSTNode root;

// Methods to insert and traverse

public void insert(String word) {
// Insertion logic
}

// Traversal methods (in-order, post-order)
}

public class Main {
public static void fillTree(BinarySearchTree bst) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
System.out.print("Enter word " + (i + 1) + ": ");
String word = scanner.nextLine();
bst.insert(word);
}
scanner.close();
}

public static void displayTree(BinarySearchTree bst, String orderType) {
if (orderType.equals("postOrder")) {
bst.postOrderTraversal();
} else if (orderType.equals("inOrder")) {
bst.inOrderTraversal();
}
// Implement other traversal types
}

public static void main(String[] args) {
BinarySearchTree bst = new BinarySearchTree();
fillTree(bst);
displayTree(bst, "inOrder");
displayTree(bst, "postOrder");
}
}



The above code is a simple framework that you will need to expand upon by implementing the actual insertion and traversal logic for the BST.

User Nojevive
by
8.4k points
5 votes

Final answer:

Here the Java program that fills a Binary Search Tree (BST) and displays:

```java

import java.util.Scanner;

class Node {

String data;

Node left, right;

public Node(String item) {

data = item;

left = right = null;

}

}

class BinarySearchTree {

Node root;

public void insert(String data) {

root = insertRec(root, data);

}

private Node insertRec(Node root, String data) {

if (root == null) {

root = new Node(data);

return root;

}

if (data.compareTo(root.data) < 0)

root.left = insertRec(root.left, data);

else if (data.compareTo(root.data) > 0)

root.right = insertRec(root.right, data);

return root;

}

public void displayTree(String order) {

if (order.equals("inOrder"))

inOrder(root);

else if (order.equals("postOrder"))

postOrder(root);

}

private void inOrder(Node root) {

if (root != null) {

inOrder(root.left);

System.out.print(root.data + " ");

inOrder(root.right);

}

}

private void postOrder(Node root) {

if (root != null) {

postOrder(root.left);

postOrder(root.right);

System.out.print(root.data + " ");

}

}

}

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

BinarySearchTree tree = new BinarySearchTree();

System.out.println("Enter 10 words:");

for (int i = 0; i < 10; i++) {

String word = scanner.next();

tree.insert(word);

}

System.out.println("Displaying inOrder:");

tree.displayTree("inOrder");

System.out.println("\\Displaying postOrder:");

tree.displayTree("postOrder");

}

}

```

Step-by-step explanation:

The code defines a `BinarySearchTree` class with methods to insert nodes into the tree and display the tree in either in-order or post-order traversal. The `insert` method adds nodes to the tree based on the given word's alphabetical order. The `displayTree` method prints the tree nodes based on the traversal order specified. In the `Main` class, a `Scanner` object reads 10 words from the user, and these words are inserted into the binary search tree using the `insert` method.

Finally, the `displayTree` method is called twice in the `main` method, first to display the tree in in-order traversal and then in post-order traversal. This structure allows users to input data, construct a binary search tree, and visualize it in different traversal orders.

The Java program allows the user to input 10 words, including their first and last name, into a Binary Search Tree using the `fillTree` method. Then, the `displayTree` method showcases the tree in both in-order and post-order traversal in the `main` method.

User Mike Hadlow
by
8.4k points