Write an implementation of a generic java BinaryTree class that fits any primitive data type. your class at minimum should have the following.
Node class with a minimum of three fields and 3 constructor.
BinaryTree class should include the following at least 3 Fields, at least 2 constructors, add, remove, find, getSize method (which returns the amount nods in the tree), getHeight (should return the height), countLeftNode (Should return the mount of left node found in the tree), countRightNode (Should return the mount of right node found in the tree), a preorder , inorder, and postorder print method.
package jan18;
class TreeNode<E> {
private E data;
private TreeNode<E> left;
private TreeNode<E> right;
public TreeNode(E d, TreeNode<E> l, TreeNode<E> r) {
this.data = d;
this.left = l;
this.right = r;
}
public TreeNode(E d) {
this.data = d;
}
public E getData() {
return this.data;
}
public TreeNode<E> getLeft() {
return this.left;
}
public TreeNode<E> getRight() {
return this.right;
}
public void setData(E newData) {
this.data = newData;
}
public void setLeft(TreeNode<E> newLeft) {
this.left = newLeft;
}
public void setRight(TreeNode<E> newRight) {
this.right = newRight;
}
}
public class BinaryTree<E> {
protected TreeNode<E> root;
/**
* Constructs an empty binary tree.
*/
public BinaryTree() {
this.root = null;
}
/**
* Constructs an empty binary tree.
*/
public BinaryTree(E value) {
this.root = new TreeNode<E>(value);
}
/**
* Adds a value to the binary tree (at the shallowest possible location)
*
* @param value
* the value to be added
*/
public void add(E value) {
this.root = this.add(this.root, value);
}
private TreeNode<E> add(TreeNode<E> current, E value) {
if (current == null) {
current = new TreeNode<E>(value, null, null);
} else if (this.size(current.getLeft()) <= this.size(current.getRight())) {
current.setLeft(this.add(current.getLeft(), value));
} else {
current.setRight(this.add(current.getRight(), value));
}
return current;
}
/**
* Determines the size of the binary tree
*
* @return the size (number of nodes in the tree)
*/
public int size() {
return this.size(this.root);
}
private int size(TreeNode<E> current) {
if (current == null) {
return 0;
} else {
return this.size(current.getLeft()) + this.size(current.getRight()) + 1;
}
}
public int height() {
return height(this.root);
}
private int height(TreeNode<E> node) {
if (node == null)
return 0;
/*
* If tree is not empty then height = 1 + max of left height and right
* heights
*/
return 1 + Math.max(height(node.getLeft()), height(node.getRight()));
}
/**
* Determines whether the tree contains a particular value.
*
* @param value
* the value to be searched for
* @return true if value is in the tree, otherwise false
*/
public boolean find(E value) {
return this.find(this.root, value);
}
private boolean find(TreeNode<E> current, E value) {
if (current == null) {
return false;
} else {
return value.equals(current.getData()) || this.find(current.getLeft(), value)
|| this.find(current.getRight(), value);
}
}
/**
* Removes one occurrence of the specified value.
*
* @param value
* the value to be removed
* @return true if the value was found and removed, else false
*/
public boolean remove(E value) {
if (!this.find(value)) {
return false;
} else {
this.root = this.remove(this.root, value);
return true;
}
}
private TreeNode<E> remove(TreeNode<E> current, E value) {
if (value.equals(current.getData())) {
if (current.getLeft() == null) {
current = current.getRight();
} else {
TreeNode<E> righty = current.getLeft();
while (righty.getRight() != null) {
righty = righty.getRight();
}
current.setData(righty.getData());
current.setLeft(this.remove(current.getLeft(), current.getData()));
}
} else if (this.find(current.getLeft(), value)) {
current.setLeft(this.remove(current.getLeft(), value));
} else {
current.setRight(this.remove(current.getRight(), value));
}
return current;
}
public int countLeftNode() {
return countLeftNode(root);
}
private int countLeftNode(TreeNode<E> start) {
if(start == null) {
return 0;
}
int count = 0;
if(start.getLeft() != null) {
count++;
}
count += countLeftNode(start.getLeft());
count += countLeftNode(start.getRight());
return count;
}
public int countRightNode () {
return countRightNode (root);
}
private int countRightNode (TreeNode<E> start) {
if(start == null) {
return 0;
}
int count = 0;
if(start.getRight() != null) {
count++;
}
count += countRightNode (start.getLeft());
count += countRightNode (start.getRight());
return count;
}
/**
* Converts the tree to a String using an inorder traversal.
*
* @return the String representation of the tree.
*/
public String toString() {
if (this.root == null) {
return "[]";
}
String recStr = this.toString(this.root);
return "[" + recStr.substring(0, recStr.length() - 1) + "]";
}
private String toString(TreeNode<E> current) {
if (current == null) {
return "";
}
return this.toString(current.getLeft()) + current.getData().toString() + ","
+ this.toString(current.getRight());
}
public void preOrder() {
preOrder(root);
}
private void preOrder(TreeNode<E> start) {
if(start != null) {
System.out.println(start.getData());
preOrder(start.getLeft());
preOrder(start.getRight());
}
}
public void postOrder() {
postOrder(root);
}
private void postOrder(TreeNode<E> start) {
if(start != null) {
postOrder(start.getLeft());
postOrder(start.getRight());
System.out.println(start.getData());
}
}
public void inOrder() {
inOrder(root);
}
private void inOrder(TreeNode<E> start) {
if(start != null) {
inOrder(start.getLeft());
System.out.println(start.getData());
inOrder(start.getRight());
}
}
}
please upvote. Thanks!
Write an implementation of a generic java BinaryTree class that fits any primitive data type. your...
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...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
in java ..write all complete program from a- e
1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
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);...
please do this lab in Java in given instructions by
tomorrow morning.
TkA CHRI - TREE UTH A HEAPOF PRESDNS CENETH CSC 236-Lab 6 (2 programs) trees 1. Given the two binary trees below: 14 18 16) Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and...
I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list //constructor - create an empty binary tree public BinaryTree() { root = null; } //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); } //deleteTree() - remove all items from tree public void deleteList() { root =...
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...
Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...