Need help in java datastructure. Please be descriptive and show results. Thank y
ID: 3806556 • Letter: N
Question
Need help in java datastructure. Please be descriptive and show results. Thank you, Objectives: To gain experience with Binary Search Trees and building your own LinkedList. Documentation: l. Explain the purpose of the program as detail as possible 8%. 2. Develop a solution for the problem and mention algorithms to be used -12 3. List data structures to be used in solution. 5%. 4. Give a description of how to use the program and expected input/output 5% 5. Explain the purpose of each class you develop in the program. 5%. Programming: l. For each method, give the pre and post conditions and invariant, if any 10 2. Program execution according to the requirements given 50% 3. Naming of program as required 5% Description of Program You are to write a program name BSTree.java that will: l. Generate 100 random integer numbers ranging from 1-99. 2. Store these numbers in a data structure of your choice and display the data on the screen DO NOTSORTTHIS DATA STRUCTURE. 3. Now build a Binary Search Tree using this set ofnumbers. You MUST insert the values into the tree starting from the first element of your Data Structure and go sequentially. 4. After building the tree, use an infix recursive method to display the data on the screen. 5. To build the Binary Search Tree, you must create your own Linked List.Explanation / Answer
/**
*
* @author Sam
*/
public class BSTree {
class Node{ // data structure of each node
final int data; //stores data
Node left; //resresents left child
Node right; //represents right child
public Node(int data) { // constructor to create node
this.data = data;
left = right = null;
}
public int compareTo(Node o) { //compare to another node
return data - o.data; //returns -ve if other node is larger
}
}
Node root;
public BSTree() {
root = null; //initialize
}
void insert(int x) {
if (root == null) { // if root is null
root = new Node(x); // we need to insert root
return; // this will occur only once
}
Node parent; //parent node is used to store the node where inserion will occur
Node child = root;
Node newNode = new Node(x); //new node is created
do {
parent = child; //make child the new parent
if (newNode.compareTo(parent) < 0) //if new node value is less than parent
child = parent.left; // we go to feft of the tree
else
child = parent.right; // and vise-versa
}while (child!=null);// condition to check parent is leaf
if (newNode.compareTo(parent) < 0) // if new node is less than its parent
parent.left = newNode; // we make it the new feft child
else
parent.right = newNode; //else opposite
}
private static void printInfix (Node root){
if(root == null)
return; //terminating condition
printInfix(root.left); //go to left
System.out.print(root.data + " | "); //print own value
printInfix(root.right); //then go to right
}
public static void main(String[] args) { //driver method
BSTree bst = new BSTree();
for (int i = 0; i<100; i++)
bst.insert((int)(Math.random()*98)+1);
printInfix(bst.root);
}
}
Output:
4 | 5 | 5 | 5 | 7 | 7 | 7 | 8 | 9 | 10 | 10 | 11 | 12 | 12 | 14 | 14 | 15 | 15 | 15 | 15 | 17 | 19 | 19 | 20 | 22 | 23 | 24 | 25 | 25 | 25 | 25 | 26 | 26 | 26 | 27 | 27 | 28 | 28 | 30 | 31 | 33 | 33 | 34 | 35 | 35 | 35 | 36 | 36 | 36 | 37 | 43 | 43 | 43 | 46 | 48 | 50 | 51 | 54 | 54 | 58 | 59 | 61 | 62 | 64 | 65 | 66 | 67 | 68 | 68 | 68 | 68 | 71 | 71 | 75 | 76 | 76 | 79 | 79 | 79 | 81 | 81 | 82 | 83 | 84 | 85 | 86 | 86 | 87 | 89 | 89 | 89 | 90 | 94 | 94 | 95 | 95 | 96 | 97 | 97 | 98 |
Go through the code, and incase you face difficulties, feel free to comment below. Hope you like the code!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.