Question

You will be given several strings full of different keyboard characters, some of which are letters of the alphabet. Write a java program that creates a binary tree that uses only the alphabetical characters (a-z, A-Z) as the value within the leaf nodes using recursion and preorder traversal. All other values within the tree (either the root node or internal nodes) will be null. You may create any methods you see fit, so long as the final binary tree is correctly outputted to the console after completion using the preorder traversal method. Note that the alphabet characters do NOT have to be in any order (ASCII code, alphabetical, ect); the binary tree must be made in the order which they appear in the string.

An example of the string you may be supplied with is given below for testing purposes, as well as a graphical example to help visualize the tree:
*@m&a?pw

k k т. w

This means that, in order, the characters in the leaves of your binary tree should be: m, a, x, p, w, k

As a hint, use each character that is NOT an alphabet character as an indicator of an internal node. Note, however, that you will not have them in your console print-out at the end as they are only to be used to find where the leaf nodes of the binary tree are supposed to be created. All internal nodes may be considered null elsewise. The printout, as stated above, should be done using preorder traversal. Extra credit may be given to those who can also print inorder and postorder traversals, though these are not required.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following code is a Java program that implements InOrder traversal of a Binary tree in which the alphabetical nodes in the given tree have been named as is and the unnamed nodes have been assigned numbers. If you require an output with just the alphabets, you can substitute the numbers in the code with a blank space. For our understanding of InOrder traversal, I have left them as numbers.

Code:

import java.util.Stack;

// BT = Binary Tree, T = tree, TN = TreeNode

public class Main {
public static void main(String[] args) throws Exception {
BT bt = BT.create();
bt.inOrder();
}
}

class BT {
static class TN {
String data;
TN left, right;
  
TN(String value) {
this.data = value;
left = right = null;
}
}

TN root;

public void inOrder() {
inOrder(root);
}

private void inOrder(TN node) {
if (node == null) {
return;
}

inOrder(node.left);
System.out.printf("%s ", node.data);
inOrder(node.right);
}

public static BT create() {
BT T = new BT();
TN root = new TN("1");
T.root = root;

T.root.left = new TN("2");
T.root.right = new TN("k");

T.root.left.left = new TN("m");
T.root.left.right = new TN("3");

T.root.left.right.left = new TN("a");
T.root.left.right.right = new TN("4");

T.root.left.right.right.left = new TN("p");
T.root.left.right.right.right = new TN("w");
  
return T;
}
}

Output:

m 2 a 3 p 4 w l k

Add a comment
Know the answer?
Add Answer to:
You will be given several strings full of different keyboard characters, some of which are letters...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help with these two problems? The following binary tree contains the characters 'A' through...

    Can someone help with these two problems? The following binary tree contains the characters 'A' through 'G' in its nodes. List the nodes in the order that they are visited by: A preorder traversal. An inorder traversal. A postorder traversal. The binary tree in Problem 2 is a Binary Search Tree since for every node, all elements stored in the left subtree of the node are smaller than the element in the node and all elements stored in the right...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • QUESTION 4 The shape of binary tree is unknown (unrelated to the tree defined in problem...

    QUESTION 4 The shape of binary tree is unknown (unrelated to the tree defined in problem 3). However, we do have the following information: performing an inorder and a postorder traversals of the tree (containing nine integer nodes with integer values from 1 through 9) yield the following outputs: a) Inorder (LNR): 123456789 b) Postorder (LRN): 1354 2 8 796 Based on the above traversal information, determine the shape of the binary tree. Note that there is no need to...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • c++, data structures Given the following Binary Tree: tree 56 47 69 22 49 59 11...

    c++, data structures Given the following Binary Tree: tree 56 47 69 22 49 59 11 29 62 I 23 30 61 64 1. Show the order in which the nodes in the tree are processed by inorder traversal, postorder traversal, and preorder traversal. 2. Show how the tree would look like after deletion of 29, 59 and 47 3. Show how the original tree would look after the insertion of nodes containing 63, 77,76, 48, 9, and 10 (in...

  • 2. Write a recursive algorithm which computes the number of nodes in a general tree. 3....

    2. Write a recursive algorithm which computes the number of nodes in a general tree. 3. Show a tree achieving the worst-case running time for algorithm depth. 4. Let T be a tree whose nodes store strings. Give an efficient algorithm that computes and prints, for every node v of T, the string stored at v and the height of the subtree rooted at v. Hin Consider 'decorating' the tree, and add a height field to each node (initialized to...

  • [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree,...

    [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT