JAVA
!!!! WRITE A CODE FOR CLASS TREE!!!!! ( This classMain is specified)
BinaryTree
Classes: Tree, Main
Definition:
A binary tree is an acyclic graph in which each node has at mosttwo children. Furthermore, each node k has a value vk and all nodeslower and further left in the tree of k contain smaller values andall nodes lower and further right in the tree of k contain largervalues. The first node of the tree - the node that has no parentnode - is called the root node. A node with no child is called aleaf.
Figure : A binary tree. The node with thevalue 15 is the root node of the whole tree. The red nodes 3, 10,19, 24, 36 and 45 are leaves, because they have no child nodes.
THE FOLLOWING codes are in the question struktur to helpwith the answer !!!!!!!!!!!!!!!!!!!!
1- class Tree
/*** A binary tree with int values.*/class Tree {
/*** The value of this node in the tree.*/private int node;
/*** The left subtree.*/private Tree lhs;
/*** The right subtree.*/private Tree rhs;
/*** Creates a new tree with the value node.* @param node The value of this node in the tree.*/public Tree(int node) {this.node = node;this.lhs = null;this.rhs = null;
}
/*** Method to add a node to the tree. Duplicates will berefused.** @param insert the new number*/public void add(int insert) {
}
/*** Method to calculate the depth of a tree. It is defined by themaximal* number of edges to a leaf.** @return the depth of the tree*/public int depth() {
}
/*** Method to find a number in the tree.** @param wanted the wanted number* @return true, if the wanted number exists in the try, elsefalse*/public boolean exists(int wanted) {
}
/*** Method to find the smallest number in the tree.** @return the smallest number in the tree*/public int smallest() {
}
/*** Method to find the biggest number in the tree.** @return the biggest number in the tree*/public int biggest() {
}
/*** Method to check whether a tree is degenerate.** @return true if every node has either no or one child node, elsefalse*/public boolean isDegenerate() {
}
/*** Method that formats the tree into a readable string.** @return the formatted string*/public String toString() {
}}
2- CLASS Main
class Main {
public static void main(String[] args) {
Tree tree = new Tree(15);
int[] set1 = {15, 21, 8, 41, 8, 5, 41, 33};int[] set2 = {18, 45, 36, 19, 3, 24, 19, 10};
for (int i = 0; i < set1.length; i++) {tree.add(set1);}
System.out.println(tree.toString());System.out.println("The depth of the tree is " + tree.depth() +".");System.out.println("Does the number 8 exist in the tree? " +tree.exists(8));System.out.println("Does the number 24 exist in the tree? " +tree.exists(24));System.out.println(tree.smallest() + " is the smallest number inthe tree. ");System.out.println(tree.biggest() + " is the biggest number in thetree. ");System.out.println("Is the tree degenerate? " +tree.isDegenerate());
for (int i = 0; i < set2.length; i++) {tree.add(set2);}
System.out.println(tree.toString());System.out.println("The depth of the tree is " + tree.depth() +".");System.out.println("Does the number 8 exist in the tree? " +tree.exists(8));System.out.println("Does the number 24 exist in the tree? " +tree.exists(24));System.out.println(tree.smallest() + " is the smallest number inthe tree. ");System.out.println(tree.biggest() + " is the biggest number in thetree. ");System.out.println("Is the tree degenerate? " +tree.isDegenerate());
}}
The Tree class:
Extend the given class Tree by a method add, which recursivelyinserts new values into the tree. These should be inserted as nodesin the correct places according to the above definition of thebinary tree. Make sure that there are no duplicates in aBinaryTree
Implement the following methodsrecursively:1-
depth: To calculate the depth of the tree. The depth is definedas the number of edges in the longest path from the root node toone of the leaves. The depth of the tree from figure 1 is: 15 ->21 -> 41 -> 33 -> 24/36 = 4.2- exists: Check if a value exists in the tree.
3- smallest: Read the smallest value in the tree.4- biggest: Read the biggest value in the tree.
Override the toString() method so that itoutputs the tree formatted. A possible format is: (((2) 4 (6)) 7(((8) 9) 15 (19))).
A binary tree in which each node has either no child node orexactly one child node is called a degenerate tree. Implement amethod isDegenerate() that checks whether a tree is degenerate.
The Main class:
This class is specified (up)
PS:
PLEASE EXPLAIN THE CODE WITH COMMENTS AND CHECK YOURANSWER BEFOR REPLYING - THANK YOU IN ADVANCE
JAVA !!!! WRITE A CODE FOR CLASS TREE!!!!! ( This class Main is specified) BinaryTree Classes: Tree, Main Definition: A
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
JAVA !!!! WRITE A CODE FOR CLASS TREE!!!!! ( This class Main is specified) BinaryTree Classes: Tree, Main Definition: A
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!