How would I build this Can someone elaborate on the steps an
How would I build this? Can someone elaborate on the steps and methods for this program along with comments on what each step does?
Program Specification:
Build a binary search tree, using links (not an array) for 15 records. The data in these records will hold names and their associated weights. Read the data from the screen.
Required functionality (Each # should be separate methods):
1. Build the tree from the unique set of names (names are the key value) and their associated weights.
2. Execute a preorder traversal
3. Execute an inorder traversal
4. Execute a postorder traversal
5. Find and print the height of the tree using recursion, do not add a height variable to the tree structure, the algorithm stack should hold this.
6. Determine the number of leaves and print the result (remember a leaf has no children).
7. Implement search functionality that will search for a name and indicate the weight for that individual if they exist in the structure, otherwise stating no match exists.
8. Determine the lowest weight contained in the tree.
9. Find the first name in alphabetical order (this should not go through every node, unless the tree happens to be a linked list).
Solution
Since the question is how to build and not the code, i am going to provide a snippets for it
Assuming following preconditions
1) Binary search tree, nodes stores name and weight.
2) ordering of nodes happens by comparing name first then weight next.
package chegg;
import java.util.Scanner;
class Node {
String name;
int weight;
Node left;
Node right;
Node(int weight, String name) {
this.weight = weight;
this.name = name;
}
}
class Implementaion {
Node root;
//Build the tree
public void insert(int weight, String name) {
root = _Insert(root,weight,name);
}
public Node _Insert(Node node, int weight, String name) {
if(node == null) {
node = new Node(weight, name);
}
else {
if (name.compareTo(node.name) < 1) {
node.left = _Insert(node.left, weight, name);
}
else{
node.right = _Insert(node.right, weight, name);
}
}
return node;
}
//Execute a preorder traversal
public void preorder(Node node) {
if (node!=null) {
System.out.print(node.name +\":\" +node.weight+\"\\t\");
preorder(node.left);
preorder(node.right);
}
}
//Execute a postorder traversal
public void postorder(Node node) {
if (node!=null) {
postorder(node.left);
postorder(node.right);
System.out.print(node.name +\":\" +node.weight+\"\\t\");
}
}
//Execute an inorder traversal
public void inorder(Node node) {
if (node!=null) {
inorder(node.left);
System.out.print(node.name +\":\" +node.weight+\"\\t\");
inorder(node.right);
}
}
//Find and print the height of the tree using recursion,
public int maxHeight(Node node)
{
if (node == null)
return 0;
else
{
/* compute the depth of each subtree */
int lDepth = maxHeight(node.left);
int rDepth = maxHeight(node.right);
/* use the larger one */
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
//Determine the number of leaves and print the result
public int NoofChildren(Node node)
{
if (node!=null) {
if (node.left == null && node.right == null) {
return 1;
}
else
{
return NoofChildren(node.left) + NoofChildren(node.right);
}
}
return 0;
}
//search
public Node search(Node root, Node data) {
if (root == null) {
return null;
}
if (root.name.equals(data.name) && root.weight == data.weight) {
return root;
}
else if (root.name.compareTo(data.name) > 1 ) {
return search(root.right, data);
}
else {
return search(root.left, data);
}
}
//Determine the lowest weight contained in the tree
//It will not exist in left most node, since we used name as sorting factor
int minWeight = 9999;
public int lowestweight(Node root) {
if (root!= null ){
if (root.weight < minWeight) {
minWeight = root.weight;
}
lowestweight(root.left);
lowestweight(root.right);
}
return minWeight;
}
//Find the first name in alphabetical order
//it will exist in leftmost node since we stored by comparing names
public String lowestname(Node root) {
if (root.left == null) {
return root.name;
}
else {
return lowestname(root.left);
}
}
}
public class BinaryTreeNameWeight {
public static void main(String[] args) {
// TODO Auto-generated method stub
Implementaion impl = new Implementaion();
impl.insert(3, \"c\");
impl.insert(2, \"b\");
impl.insert(1, \"a\");
impl.insert(4, \"d\");
impl.insert(5, \"e\");
System.out.println(\"\ Inorder\");
impl.inorder(impl.root);
System.out.println(\"\ Preorder\");
impl.preorder(impl.root);
System.out.println(\"\ Postorder\");
impl.postorder(impl.root);
System.out.println(\"\ Height\");
System.out.println(impl.maxHeight(impl.root));
System.out.println(\"\ NoofChildren\\t\"+ impl.NoofChildren(impl.root));
System.out.println(\"\ lowestweight\\t\"+ impl.lowestweight(impl.root));
System.out.println(\"\ lowestname\\t\"+ impl.lowestname(impl.root));
Node n = new Node(2, \"b\");
System.out.println(\"Searching for a node\");
Node n1 = impl.search(impl.root, n);
if (n1!=null)
System.out.println(\"Node found\");
else
System.out.println(\"Node not found\");
n.weight=10;
n1 = impl.search(impl.root, n);
if (n1!=null)
System.out.println(\"Node found\");
else
System.out.println(\"Node not found\");
}
}
---------------------
Output
Inorder
a:1 b:2 c:3 d:4 e:5
Preorder
c:3 b:2 a:1 d:4 e:5
Postorder
a:1 b:2 e:5 d:4 c:3
Height
3
NoofChildren 2
lowestweight 1
lowestname a
Searching for a node
Node found
Node not found



