All resources and materials have been provided as well as references. This is all that needed to
Fantastic news! We've Found the answer you've been seeking!
Question:
The references can be found within the class comments
Task: Implement a search tree class
/* Lab-07: BinarySearchTree Implementation Rules: 1. Allow Tester to iterate through all nodes using the in-order traversal as the default. This means, in Tester the following code should work for an instance of this class called bst that is storing Student objects for the data: BinarySearchTree_Lab07 bst = new BinarySearchTree_Lab07(); bst.add("Man"); bst.add("Soda"); bst.add("Flag"); bst.add("Home"); bst.add("Today"); bst.add("Jack"); for(String s : bst) System.out.println(s); */ public class BinarySearchTree_Lab07 { //====================================================================================== Properties private Node root; private int nodeCount; //====================================================================================== Constructors public BinarySearchTree_Lab07() { } // Constructor that takes an array of items and populates the tree public BinarySearchTree_Lab07(T[] items) { } //====================================================================================== Methods public void add(T data) { // Implement recursively and do NOT allow duplicates } // Returns the traversal of this tree as an array public String[] preOrder_Traversal() { return null; } public String[] inOrder_Traversal() { return null; } public String[] postOrder_Traversal() { return null; } public String[] breadthFirst_Traversal() { return null; } // Since this is a binary SEARCH tree, you should write // an efficient solution to this that takes advantage of the order // of the nodes in a BST. Your algorithm should be, on average, // O(h) where h is the height of the BST. public boolean contains(T data) { return false; } // returns the smallest value in the tree // or throws an IllegalStateException() if the // tree is empty. Write the recursive version public T min() { return min(root); } // this method is done for you. private T min(Node n) { // Produce this method. return null; } // returns the largest value in the tree // or throws an IllegalStateException() if the // tree is empty. Write the recursive version public T max() { return max(root); } // this method is done for you. private T max(Node n) { // Produce this method. return null; } // Returns whether the tree is empty public boolean isEmpty() { return false; } // returns the height of this BST. If a BST is empty, then // consider its height to be -1. public int getHeight() { return -1; } // returns the number of leaf nodes public int leafCount() { return -1; } // returns the "level" of the value in a tree. // the root is considered level 0 // the children of the root are level 1 // the children of the children of the root are level 2 // and so on. If a value does not appear in the tree, return -1 // 15 // / // 10 28 // // 12 40 // / // 30 // In the tree above: // getLevel(15) would return 0 // getLevel(10) would return 1 // getLevel(30) would return 3 // getLevel(8) would return -1 public int getLevel(T n) { return -1; } // ********************************************************* // EXTRA CREDIT: 5pts // ********************************************************* // A tree is height-balanced if at each node, the heights // of the node's two subtrees differs by no more than 1. // Special note about null subtrees: // 10 // // 20 // Notice in this example that 10's left subtree is null, // and its right subtree has height 0. We would consider this // to be a balanced tree. If the tree is empty, return true; public boolean isBalanced() { return false; } //====================================================================================== Inner Node Class private class Node { private T data; private Node left, right; private Node(T data) { this.data = data; left = right = null; } } }
Tester
import java.util.Arrays; public class Tester { // This Lab is to implement a BinarySearchTree SET. So, NO duplicate entries // are allowed. You are also to code your BinarySearchTree so that you can // use a for-each loop public static void main(String[] args) { BinarySearchTree_Lab07 bst = new BinarySearchTree_Lab07(); bst.add(25); bst.add(12); bst.add(80); bst.add(8); bst.add(20); bst.add(63); bst.add(12); // should not be added bst.add(25); // should not be added bst.add(90); bst.add(20); // should not be added bst.add(44); bst.add(73); bst.add(85); bst.add(71); System.out.println(Arrays.toString(bst.preOrder_Traversal())); System.out.println(Arrays.toString(bst.inOrder_Traversal())); System.out.println(Arrays.toString(bst.postOrder_Traversal())); System.out.println(Arrays.toString(bst.breadthFirst_Traversal())); /* In-Order : 8 12 20 25 44 63 71 73 80 85 90 Post-Order : 8 20 12 44 71 73 63 85 90 80 25 Pre-Order : 25 12 8 20 80 63 44 73 71 90 85 Breath First: 25 12 80 8 20 63 90 44 73 85 71 */ System.out.println(bst.getHeight()); // 4 System.out.println(bst.contains(44)); // true System.out.println(bst.contains(99)); // false System.out.println(bst.getLevel(73)); // 3 System.out.println(bst.isBalanced()); // false System.out.println(bst.isEmpty()); // false System.out.println(bst.leafCount()); // 6 System.out.println(bst.min()); // 8 System.out.println(bst.max()); // 90 /* Uncomment the following loop after setting up the * BinarySearchTree_Lab07 for iteration */ //for(int s : bst) // System.out.print(s + " "); } }
Related Book For
Business Ethics A Stakeholder And Issues Management Approach
ISBN: 9781523091546
7th Edition
Authors: Joseph W. Weiss
Posted Date: