Question

In: Computer Science

INTERFACE (BinaryTreeInterface): BinaryTreeInterface.java This interface represents all the functionalities a generic binary tree. Operations + getRootData():...

INTERFACE (BinaryTreeInterface): BinaryTreeInterface.java
This interface represents all the functionalities a generic binary tree.
Operations
+ getRootData(): T - This method returns the data stored in the root node of a tree. Note that you cannot return the data of an empty tree.
+ getRootNode(): BinaryNode<T> - This method returns the reference to the root node of a tree.
+ setRootNode(BinaryNode<T>):void - This method sets the root node of a tree.
+ getHeight():int - This method returns the height of a tree.
+ getNumberOfNodes(): int - This method returns the number of nodes in a tree.
+ isEmpty():boolean - This method returns true, if a tree is empty, false otherwise.
+ clear():void – clears a tree.

CLASS (BinaryTree): BinaryTree.java
This generic class implements the BinaryTreeInterface. The class has one attribute root, which represents the root node of a tree. In addition, it has the following behaviors:
+BinaryTree(): constructor - initializes root to null.
+ BinaryTree(data): constructor - initializes the root node’s data.
+ BinaryTree(data, leftTree, rightTree): constructor - initializes the root node with the given values.
+ setTree(data, leftTree, rightTree): void- set the tree to the root with data and the leftTree and rightTree subtrees. Make sure that the new created tree has only one point of entry, which is the root.
+inorderTraversal(): void – displays all the nodes in tree using inorder traversal. It displays the content of the nodes separated by a space in a single line. For example, if the nodes’ content are 1, 2, 3, and 4, the method displays “1 2 3 4”. We have to have a non empty tree for traversals.

Please implement those interface and class today, thank you

Solutions

Expert Solution

/*** BinaryTreeInterface.java ***/

public interface BinaryTreeInterface<T>
{
   //This method returns the data stored in the root node of a tree.
   //Note that you cannot return the data of an empty tree.
   public T getRootData();
   //This method returns the reference to the root node of a tree.
   public BinaryNode<T> getRootNode();
   //This method sets the root node of a tree.
   public void setRootNode(BinaryNode<T> root);
   //This method returns the height of a tree.
   public int getHeight();
   //This method returns the number of nodes in a tree.
   public int getNumberOfNodes();
   //This method returns true, if a tree is empty, false otherwise.
   public boolean isEmpty();
   //clears a tree.
   public void clear();
}

/*** BinaryNode.java ***/

public class BinaryNode<T>
{
   T data;
   BinaryNode<T> left;
   BinaryNode<T> right;
  
   public BinaryNode(T data)
   {
       this.data = data;
       left = null;
       right = null;
   }
  
   public BinaryNode(T data, BinaryNode<T> left, BinaryNode<T> right)
   {
       this.data = data;
       this.left = left;
       this.right = right;
   }
  
   public T getData()
   {
       return data;
   }
}

/*** BinaryTree.java ***/
public class BinaryTree<T> implements BinaryTreeInterface<T>
{
  
   private BinaryNode<T> root;
  
   //constructor - initializes root to null.
   public BinaryTree()
   {
       root = null;
   }
  
   //constructor - initializes the root node’s data.
   public BinaryTree(T data)
   {
       root = new BinaryNode<>(data);
   }
  
   //constructor - initializes the root node with the given values.
   public BinaryTree(T data, BinaryNode<T> leftTree, BinaryNode<T> rightTree)
   {
       root = new BinaryNode<>(data, leftTree, rightTree);
   }
  
   //set the tree to the root with data and the leftTree and rightTree subtrees.
   //Make sure that the new created tree has only one point of entry, which is the root.
   public void setTree(T data, BinaryNode<T> leftTree, BinaryNode<T> rightTree)
   {
       root = new BinaryNode<>(data, leftTree, rightTree);
   }
  
   //Helper method to displays all the nodes in tree using inorder traversal.
   private void inorder(BinaryNode<T> btnode)
   {
       if(btnode!=null)
       {
           inorder(btnode.left);
           System.out.print(btnode.getData() + " ");
           inorder(btnode.right);
       }
   }
  
   //displays all the nodes in tree using inorder traversal.
   public void inorderTraversal()
   {
       inorder(root);
   }
  
   //This method returns the data stored in the root node of a tree.
   //Note that you cannot return the data of an empty tree.
   public T getRootData()
   {
       return root.getData();
   }
   //This method returns the reference to the root node of a tree.
   public BinaryNode<T> getRootNode()
   {
       return root;
   }
   //This method sets the root node of a tree.
   public void setRootNode(BinaryNode<T> root)
   {
       this.root = root;
   }
  
   //Helper method to calculate height of the binary tree
   private int getHeight(BinaryNode<T> btnode)
   {
       if(btnode==null)
           return 0;
      
       int hleft = getHeight(btnode.left);
       int hright = getHeight(btnode.right);
       int max = hleft>hright? hleft: hright;
      
       return max + 1;
   }
   //This method returns the height of a tree.
   public int getHeight()
   {
       return getHeight(root);
   }
  
   //Helper method returns the number of nodes in a tree.
   private int getNumberOfNodes(BinaryNode<T> btnode)
   {
       if(btnode==null)
           return 0;
      
       return 1 + getNumberOfNodes(btnode.left) +
           getNumberOfNodes(btnode.right);
   }
  
   //This method returns the number of nodes in a tree.
   public int getNumberOfNodes()
   {
       return getNumberOfNodes(root);
   }
   //This method returns true, if a tree is empty, false otherwise.
   public boolean isEmpty()
   {
       return root==null;
   }
   //clears a tree.
   public void clear()
   {
       root = null;
   }
}

/*** BinaryTreeApp.java ***/
public class BinaryTreeApp
{
   //main method
   public static void main (String[] args)
   {
       BinaryNode<Integer> btnode = new BinaryNode<>(5, new BinaryNode<>(4, null, null),
           new BinaryNode<>(6, null, null));
      
       BinaryTree<Integer> b = new BinaryTree<>(2, new BinaryNode<>(1, null, null), btnode);
      
       b.inorderTraversal();
       System.out.println ();
       System.out.println ("Height: " + b.getHeight());
       System.out.println ("Number of nodes: " + b.getNumberOfNodes());
       System.out.println ("Is the tree empty? " + b.isEmpty());
       b.clear();
       System.out.println ("Is the tree empty? " + b.isEmpty());
   }
}

Output:

1 2 4 5 6
Height: 3
Number of nodes: 5
Is the tree empty? false
Is the tree empty? true


Related Solutions

(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels...
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels fully filled. Define a new class named BSTWithTestPerfect that extends BST with the following methods: (Hint: The number of nodes in a perfect binary tree is 2^(height+1) - 1.) /** Returns true if the tree is a perfect binary tree */ public boolean isPerfectBST() Class Name: Exercise25_03
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 . •...
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
How to read and print all contents in a binary file using a Binary Search Tree...
How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information. An example would be a sports record binary file that consist of name of athlete, height , weight, championships won. Athlete: Michael Jordan Height: 6’6” Weight : 205 lbs Championships won: 6 =================== Athlete: LeBron James Height: 6’8” Weight: 250 lbs Championships won:...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order traversal, preorder traversal, and find. Please separate the code in the four parts and explain in detail what is happening. Also, if you can please basic C language. If not, then I understand. Thank you for your time. The test cases are 'm', 'd', 'g', 'r', 'p', 'b', and 'x'. Output: Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i Enter...
/** * This class implements a basic Binary Search Tree that supports insert and get operations....
/** * This class implements a basic Binary Search Tree that supports insert and get operations. In * addition, the implementation supports traversing the tree in pre-, post-, and in-order. Each node * of the tree stores a key, value pair. * * @param <K> The key type for this tree. Instances of this type are used to look up key, value * pairs in the tree. * @param <V> The value type for this tree. An instance of this...
Design algorithms for the following operations for a binary tree T: . preorderNext(p) : Return the...
Design algorithms for the following operations for a binary tree T: . preorderNext(p) : Return the position visited after p in a preorder traversal of T, or NULL if p is the last node visited. . inorderNext(p) : Return the position visited after p in an inorder traversal of T, or NULL if p is the last node visited. . postorderNext(p) : Return the position visited after p in a postorder traversal of T, or NULL if p is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT