Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(JAVA JAVA JAVA) Create a binary search tree but with an array implementation. T

ID: 3678547 • Letter: #

Question

(JAVA JAVA JAVA)

Create a binary search tree but with an array implementation. The data contained in this tree is positive integers, so it may help to initialize the entire tree as some negative sentinel value (like -1)s to indicate that certain values have not been populated. Download the driver file : https://cse.sc.edu/~shephejj/csce146/Labs/Lab08Driver/IntBSTreeTester.java

, in order to test it out.

The class IntBSTree is given fill in the following methods

insert: This method returns nothing and takes in an integer value that is then placed as a new node in the tree based on the binary tree properties. A reminder values greater than the parent go to the right subtree and values smaller go to the left subtree. Also it may be a good idea to use a recursive method in order to place these values.

printInorder: This method which returns nothing and has no parameters prints the in order traversal of the tree. For in order traversal each left subtree must be visited, then the value is printed out, and finally each of the right subtrees is visited. It is a good idea to make a recursive method to assist with this. Also if done correctly in-order traversals print out each of the integers in ascending order.

printBreadthOrder: This method which returns nothing and has no parameters prints the breadth order traversal of the tree. For this one each element in a row (in other words it and all of its siblings) are printed from left to right.

getDepth: The depth of a node is the number of edges from the root to that number. This method returns the depth and takes in a parameter corresponding to the integer value of a node whose depth is returned. If the value is not in the tree a -1 should be returned. Again a recursive helper method may be useful to solve this.

Correct Print Out:

Int BST Tester!

Creating tree

Populating Tree with values

Testing insertion by in-order traversal

1

2

3

4

5

6

7

8

9

10

Testing breadth order traversal

4

2

8

1

3

7

10

5

9

6

Getting depth of 6

4

Getting depth of 14

-1

——————————

Driver file :

Explanation / Answer

/* * Java Program to Implement Binary Search Tree */ import java.util.Scanner; /* Class BSTNode */ class BSTNode { BSTNode left, right; int data; /* Constructor */ public BSTNode() { left = null; right = null; data = 0; } /* Constructor */ public BSTNode(int n) { left = null; right = null; data = n; } /* Function to set left node */ public void setLeft(BSTNode n) { left = n; } /* Function to set right node */ public void setRight(BSTNode n) { right = n; } /* Function to get left node */ public BSTNode getLeft() { return left; } /* Function to get right node */ public BSTNode getRight() { return right; } /* Function to set data to node */ public void setData(int d) { data = d; } /* Function to get data from node */ public int getData() { return data; } } /* Class BST */ class BST { private BSTNode root; /* Constructor */ public BST() { root = null; } /* Function to check if tree is empty */ public boolean isEmpty() { return root == null; } /* Functions to insert data */ public void insert(int data) { root = insert(root, data); } /* Function to insert data recursively */ private BSTNode insert(BSTNode node, int data) { if (node == null) node = new BSTNode(data); else { if (data