Question: Please help write this in Java. A Binary Search Tree written in Java is provided (see below). public class BinaryTree > { private BinaryTreeNode root;

Please help write this in Java.

A Binary Search Tree written in Java is provided (see below).

public class BinaryTree> { private BinaryTreeNode root; // the root of the tree private BinaryTreeNode cursor; // the current node /** * Constructor for initializing a tree with node * being set as the root of the tree. * @param node */ public BinaryTree(BinaryTreeNode node) { root = node; } /** * Moves the cursor to the root. */ public void toRoot() { cursor = root; } /** * Returns the cursor node. * @return cursor */ public BinaryTreeNode getCursor() { return cursor; } /** * Sets the root to the provided node. * ONLY USE IN THE DELETE METHOD * @param node */ public void setRoot(BinaryTreeNode node) { root = node; } /** * Checks if the tree node has a left child node * @return true if left child exists else false */ public boolean hasLeftChild() { return cursor.getLeft() != null; } /** * Checks if the tree node has a right child node * @return true if right child exists else false */ public boolean hasRightChild() { return cursor.getRight() != null; } /** * Move the cursor to the left child */ public void toLeftChild() { cursor = cursor.getLeft(); } /** * Move the cursor to the right child */ public void toRightChild() { cursor = cursor.getRight(); } /** * @return height of the tree */ public int height() { if (root != null) { return root.height(); } else { return 0; } } /* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { if (root != null) { return root.toStringPreOrder("."); } else { return ""; } } } public class BinaryTreeNode> { private BinaryTreeNode left; // the left child private BinaryTreeNode right; // the right child private T data; // the data in this node public BinaryTreeNode() { this(null, null, null); } public BinaryTreeNode(T theData) { this(theData, null, null); } public BinaryTreeNode(T theData, BinaryTreeNode leftChild, BinaryTreeNode rightChild) { data = theData; left = leftChild; right = rightChild; } public T getData() { return data; } public BinaryTreeNode getLeft() { return left; } public BinaryTreeNode getRight() { return right; } public void setLeft(BinaryTreeNode newLeft) { left = newLeft; } public void setRight(BinaryTreeNode newRight) { right = newRight; } public void setData(T newData) { data = newData; } public void preOrder() { System.out.println(data); if (left != null) { left.preOrder(); } if (right != null) { right.preOrder(); } } public int height() { int leftHeight = 0; // Height of the left subtree int rightHeight = 0; // Height of the right subtree int height = 0; // The height of this subtree //If we have a left subtree, determine its height if (left != null) { leftHeight = left.height(); } //If we have a right subtree, determine its height if (right != null) { rightHeight = right.height(); } //The height of the tree rooted at this node is one more than the //height of the 'taller' of its children. if (leftHeight > rightHeight) { height = 1 + leftHeight; } else { height = 1 + rightHeight; } //Return the answer return height; } /** * @param pathString * @return the tree nodes in pre-order traversal */ public String toStringPreOrder(String pathString) { String treeString = pathString + " : " + data + " "; if (left != null) { treeString += left.toStringPreOrder(pathString + "L"); } if (right != null) { treeString += right.toStringPreOrder(pathString + "R"); } return treeString; } }

a. Implement a method Insert (T data) in BinarySearchTree.java to insert a node having value of key = data in a Binary Search Tree. See the algorithm presented below.

Please help write this in Java. A Binary Search Tree written in

TREE-INSERT(T, z) 1 y NIL 2 x T root 3 while x NIL. if z.key x.key x, left 7 else x x. right 9 if y NIL 100 Troot z ll tree T was empty 11 elseif z.key y.key 12 y.left z 13 else y right b. Implement In-order-Traversal 0 n BinarysearchTree-java that prints the elements of the BST in an in-order format. See the algorithm presented below. If your insertion of elements in the BST is correct, then the In-Order-Traversal output should print the elements in a sorted order. INORDER-TREE-WALK (x) 1 if x NIL 2 INORDER-TREE WALK (x left) 3 print key 4 INORDER-TREE-WALK (x.right) Write a driver program to test the BInarysearchTree class. Make sure you use all the methods implemented above

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!