Question

In: Computer Science

Develop a Java application to implement a binary tree data structure. A tree data structure starts...

Develop a Java application to implement a binary tree data structure. A tree data structure starts from the top node, called the root. Each node in the tree has a set of children, which are also nodes of the tree and can have children of their own, and so on. This keeps on going till we get to the bottom of the tree, to the “leaf” nodes. Each node in the tree, except for the root, has a parent. A binary tree is a specialized form of a tree data structure in which each node in the tree has at most two children.

A binary tree can be represented using the format in the example below. Each line contains at most three characters. The first character is the ID of the node itself. The next two characters are the child nodes. If a line has only 2 characters, then that node has only one child. If a line has one character only, then it is a leaf node (i.e., it has no children). Example

A B C

B D E

C F G

D H I

E J K

F L

G

H

I

J

K

L

The application should exhibit the following functionality (see the sample output provided):

  • Display a menu to the user and request for the desired option.
  • Based on the user’s input, request for additional information as follows:

o If the user wants to add a node, request for the name / ID of the new node to be added as well as the name of the desired parent of that node.

▪ If the parent node already has two children, the new node cannot be added since this is a binary tree (and each node can only have at most two children).

  • Display an appropriate message to the user.
  • Display the menu again for the user to make another choice.

▪If the parent node already has one child, use recursion to add the new node as the second child (right child) of the parent node.

  • Display the new tree with the new node added. This should be done using a recursive method in your project (printTree() ).
  • Display the menu options.

▪  If the parent node has no children, use recursion to add the new node as the left child

of the parent node.

  • Display the new tree with the new node added.
  • Display the menu options.

o If the user wants to know the size of the tree (i.e., the number of nodes in the tree or sub-tree), request for the name / ID of the node that is the root of the desired tree / sub-tree. The size of a tree or sub-tree is the number of its children and other ‘descendants’ (including any leaf nodes) plus one – the root node itself. For example, the size of the sub-tree with root ‘B’ in Figure 2 above is 7.

Recursively count the number of nodes in the tree / sub-tree and display this with an appropriate message.

  • Display the newly updated tree.
  • Display the menu options.

o If the user wants to find a node, request for the name / ID of the node to search for in the tree.

▪ Search recursively for the node in the tree; if the node is found, display an appropriate

message, otherwise, indicate that the node does not exist in the tree.

•Display the menu options.

o If the user wants to exit, terminate the program.

A sample output (to be followed exactly) is included below.

Example Output

ABC

BDE

DHI

H

I
EJK
J
K
CFG
FL
L
G
There are 12 nodes in this tree.

Please select from one of the following options:

1. Add Node
2. Tree Size
3. Find Node

0. Exit
->1
Please input the node you want to add->
P
Please input the parent node of P->
A
Parent already has 2 children.
Please select from one of the following options:

1. Add Node
2. Tree Size
3. Find Node
0. Exit

->1
Please input the node you want to add->
P
Please input the parent node of P->
F
Node successfully added!
ABC
BDE
DHI
H
I
EJK
J
K
CFG
FLP

L
P
G
Please select from one of the following options:

1. Add Node

2. Tree Size

3. Find Node

0. Exit

->2

Please input the root node->

F

There are 3 nodes in that tree
FLP
Please select from one of the following options:

1. Add Node
2. Tree Size
3. Find Node
0. Exit
->3
Please input the node you want to look for->
P
Node P found!
P
Please select from one of the following options:

1. Add Node
2. Tree Size
3. Find Node
0. Exit
->3
Please input the node you want to look for->
N
Node N does not exist.
Please select from one of the following options: 1. Add Node
2. Tree Size
3. Find Node
0. Exit
->0

The main class in your application should be named BinaryTree. This class should instantiate an instance of the second class – to be named TreeDataStructure – and that instance should be used to call the appropriate methods to generate the initial tree, display the menu to the user and exhibit the functionality described above. Apart from the main method, the BinaryTree class should also have a method to print the menu options (void printMenu() ). You can copy and paste the code below into your main class to generate the initial tree.

public static void main(String[] args) {

TreeDataStructure root =

root.addChild("B", "A");

root.addChild("C", "A");

root.addChild("D", "B");

root.addChild("E", "B");

new TreeDataStructure("A");

root.addChild("F", "C");
root.addChild("G", "C");
root.addChild("H", "D");
root.addChild("I", "D");
root.addChild("J", "E");
root.addChild("K", "E");
root.addChild("L", "F");

The TreeDataStructure class should implement the INode interface (which is provided below). The methods addChild(), find(), printTree(), and size() in the TreeDataStructure class must be implemented recursively. NOTE: Using recursion is one of the main objectives of this assignment. A solution that does not use recursion to implement the required functionality will earn zero points. Create an interface named INode within your package (following steps similar to how you would create a class) and copy and paste the interface code below into it.

public interface INode {

public boolean addChild(String ID, String parentID);

public INode find(String value);

public INode getParent();

public int size();

public String toString();

public String getId();

public void printTree();

Hints

  1. It is important to store information about the parent of each node as part of that node’s internal structure. In addition, each node should have information about its’ children.
  2. You may represent the children of a node possibly using an array of nodes.

Solutions

Expert Solution

import java.util.HashMap;
import java.util.Map;
import java.util.*;
interface INode {


    public boolean addChild(String ID, String parentID);
    public INode find(String value);
    public INode getParent();
    public int size();
    public String toString();
    public String getId();
    public void printTree();
    public void printInInorder();
}
class TreeDataStructure implements INode {

    private String data;
    private TreeDataStructure left, right, parent;
    public TreeDataStructure(){} // no argument constructor

    public TreeDataStructure(String data) // one argument constructor
    {
        this.data = data;
        left = right = parent = null;
    }

    private TreeDataStructure newNode(String data)
    {
        TreeDataStructure temp = new TreeDataStructure(data);
        return temp;
    }
    @Override
    public boolean addChild(String ID, String parentID) {

        return addNode(this, ID, parentID);
    }
    public boolean addNode(TreeDataStructure root, String id, String parentId)
    {
        if(root==null)
            return false;
        if(root.data.equals(parentId))
        {
            if(root.left == null)
            {
                TreeDataStructure temp = newNode(id);
                root.left = temp;
                temp.parent = root;
                return true;
            }
            else if (root.right == null)
            {
                TreeDataStructure temp = newNode(id);
                root.right = temp;
                temp.parent = root;
                return true;
            }
            else {
                System.out.println("Parent already has 2 children.");
                return false;
            }
        }
        return addNode(root.left, id, parentId) || addNode(root.right, id, parentId);
    }
    @Override
    public INode find(String value) {
        INode node = findNode(this, value);
        return node;
    }
    private TreeDataStructure findNode(TreeDataStructure root, String node)
    {
        if(root != null){
            if(root.data.equals(node)){
                return root;
            } else {
                TreeDataStructure foundNode = findNode(root.left, node);
                if(foundNode == null) {
                    foundNode = findNode(root.right, node);
                }
                return foundNode;
            }
        } else {
            return null;
        }
    }
    @Override
    public INode getParent() {
        return this.parent;
    }

    @Override
    public int size() {
        return getSize(this);
    }

    private int getSize(TreeDataStructure node)
    {
        if(node == null)
            return 0;
        return getSize(node.left) + getSize(node.right) + 1;
    }
    @Override
    public String getId() {
        return this.data;
    }

    @Override
    public void printTree() {
        print(this);
    }
    private void print(TreeDataStructure node) {

        if (node == null)
            return;
        System.out.print(node.data);
        if (node.left != null)
            System.out.print(node.left.data);
        if (node.right != null)
            System.out.print(node.right.data);
        System.out.println();
        this.print(node.left);
        this.print(node.right);
    }
    @Override
    public void printInInorder()
    {
        printInorder(this);
    }
    public void printInorder(TreeDataStructure root)
    {
        if(root==null)
            return;
        System.out.print(root.data);
        printInorder(root.left);
        printInorder(root.right);
    }
}
public class BinaryTree
{
    public static void main(String args[])
    {
        TreeDataStructure root = new TreeDataStructure("A");
        root.addChild("B", "A");
        root.addChild("C", "A");
        root.addChild("D", "B");
        root.addChild("E", "B");
        root.addChild("F", "C");
        root.addChild("G", "C");
        root.addChild("H", "D");
        root.addChild("I", "D");
        root.addChild("J", "E");
        root.addChild("K", "E");
        root.addChild("L", "F");
        root.printTree();
        System.out.println("There are " + root.size() + " nodes in this tree.");
        int input = 0;
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.println("Please select from one of the following options:");
            System.out.println("1. Add Node");
            System.out.println("2. Tree Size");
            System.out.println("3. Find Node");
            System.out.println("0. Exit");
            System.out.print("->");
            input = scanner.nextInt();

            switch (input)
            {
                case 1:
                    System.out.println("Please input the node you want to add->");
                    String id = scanner.next();
                    System.out.println("Please input the parent node of " + id + "->");
                    String parentId = scanner.next();
                    if(root.addChild(id, parentId)) {
                        System.out.println("Node successfully added!");
                        root.printTree();
                    }
                    break;
                case 2:
                    System.out.println("Please input the root node->");
                    id = scanner.next();
                    INode temp = root.find(id);
                    if(temp != null) {
                        System.out.println("There are " + temp.size() + " nodes in that tree");
                        temp.printInInorder();
                        System.out.println();
                    }
                    else
                        System.out.println("Root node " + id + " not present in the tree!");
                    break;
                case 3:
                    System.out.println("Please input the node you want to look for->");
                    id = scanner.next();
                    if(root.find(id)!=null)
                        System.out.println("Node " + id + " found!");
                    else
                        System.out.println("Node " + id + " does not exist.");
                     break;
                default:
                    System.out.println("Enter correct input data!!!");
                    break;
            }
        }while (input != 0);
    }
}

Related Solutions

Java program that uses binary tree and recursions to implement Tower of Hanoi game where there...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there can be any amount of disks and there are either 3,4, or 5 pegs to store the disks(# of pegs and disks is manually inputted by user), in Tower of Hanoi game, the object of the game is move all the pieces from the 1st peg to the right most peg or the opposite side. You can place disks on top of each other...
java. Consider a binary tree with integer values in its nodes, implement a method that returns...
java. Consider a binary tree with integer values in its nodes, implement a method that returns the sum of the values contained in all of the nodes of the binary tree with root n.Hint: use recursion.
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
Binary Tree Develop algorithms for performing various operations of binary tree like insertion and deletion of...
Binary Tree Develop algorithms for performing various operations of binary tree like insertion and deletion of elements, finding an element in the binary tree. Analyse time and space complexity of the designed algorithm Write C++ program to implement binary tree
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
C++ Implement the array based Binary Heap data structure as discussed in class. This structure should...
C++ Implement the array based Binary Heap data structure as discussed in class. This structure should have a couple of constructures (default constructor, and a constructor that takes an array pointer and a size), a method for inserting items into the heap, a method for removing items from the heap, and a method that returns the number of items currently stored in the heap. This implementation should be templated so that it can store any type of data (you may...
write a java program that implements the splay tree data structure for the dictionary abstract data...
write a java program that implements the splay tree data structure for the dictionary abstract data type. Initially the program reads data from "in.dat", and establishes an ordinary binary search tree by inserting the data into the tree. The data file contains integers, one per line. in.dat file contents: 3456 5678 1234 2369 7721 3354 1321 4946 3210 8765 Then the program starts an interactive mode. The commands are as follows. S 1000 - splay the tree at 1000 F...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
Code using C++ A binary search tree is a data structure designed for efficient item insertion,...
Code using C++ A binary search tree is a data structure designed for efficient item insertion, deletion, and retrieval. These 3 operations share an average run time complexity of O(log(n)). Such time complexities are guaranteed whenever you are working with a balanced binary search tree. However, if you have a tree that begins leaning heavily to either its left or right side, then you can expect the performances of the insertion, deletion, and retrieval operations to degrade. As an example,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT