PLEASE write the method for the printTree I am having trouble printing the binary search tree and how you know how to put it into the correct order based on the Generated tree please answer the method and then explain it. Thankyou,
PLEASE write the method for the printTree I am having trouble printing the binary search tree and how you know how to put it into the correct order based on the Generated tree please answer the method and then explain it. Thankyou,
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import org.w3c.dom.Node;
public class ICA06_Start {
public static void main(String[] args) {
Random rand = new Random();
// TODO code application logic here
IntBSTNoDup tree = new IntBSTNoDup();
System.out.println("CPS 151 ICA 06 by Josh Neils");
for (int Lcv = 1; Lcv <= 10; Lcv++)
tree.add(rand.nextInt(1000));
System.out.println("\nThe tree generated is\n" + tree);
System.out.println("\nThe tree in preorder:\n" + tree.preOrder());
System.out.println("\nThe tree in inorder:\n" + tree.inOrder());
System.out.println("\nTree printed by treePrint:");
tree.treePrint();
System.out.println("\nThe number of nodes in tree is: " + tree.size());
System.out.println("\nThe number of leaves in tree is: " + tree.leafCount());
System.out.println("\nCPS 151 ICA 06 complete");
} // end main
} // end main class
// ----------------- class IntBSTNoDup
class IntBSTNoDup {
private TreeNode root;
public int counter;
public IntBSTNoDup() {
root = null;
}
public boolean add(int data) {
return add(root, data);
} // end public add
private boolean add(TreeNode r, int data) {
assert root == null || r != null;
if (root == null) { // new node becomes the root
root = new TreeNode(data);
return true;
}
if (data == r.data) // duplicate not allowed
return false;
// find the direction to go
if (data < r.data) { // go left
if (r.left != null) // recurse
return add(r.left, data);
// r.left == null so add a new node as left child of r
r.left = new TreeNode(data);
return true;
} // end if
// data > r.data, so go right
if (r.right != null) // recurse
return add(r.right, data);
// r.right == null so add a new node as right child of r
r.right = new TreeNode(data);
return true;
} // end private add
public String toString() {
if (root == null)
return "()";
return toString(root);
} // end public toString
private String toString(TreeNode r) {
if (r == null) return "";
return "(" + toString(r.left) + r.data + toString(r.right) + ")";
} // end private toString
public String preOrder() {
if (root == null) return "Empty";
return preOrder(root);
} // end public preOrder
private String preOrder(TreeNode n) {
if (n == null) return "";
return " " + n.data + preOrder(n.left) + preOrder(n.right);
} // end private preOrder
public String inOrder() {//
if (root == null) return "Empty";
return inOrder(root);
} // end public inOrder
private String inOrder(TreeNode n) {//
if (n == null) return "";
return inOrder(n.left) + " " + n.data + inOrder(n.right);
} // end private inOrder
int size() {return size(root);}
int leafCount() {
return leafCount(root);
}
private int size(TreeNode n) {
if (n == null) return 0;
return 1 + size(n.left) + size(n.right);
}
private int leafCount(TreeNode n) {
if(n==null) {
return 0;}
else if(n.right==null && n.left==null) {
return 1;}
else{//recursively call leafCount using left and right n.
return leafCount(n.left)+leafCount(n.right);}
} // end private leafCount
public void treePrint() {
treePrint(root, 0);
} // end public treePrint
private void treePrint(TreeNode n, int level) {
return;//ends the method
} // end private treePrint
private static void indent(int level) {
for(int Lcv = 1; Lcv <= 5 * level; Lcv++)
System.out.print(' ');
} // end indent
} // end class
// ------------------- class TreeNode
class TreeNode {
public int data;
public TreeNode left, right;
public TreeNode(int data) {
this(data, null, null);
}
public TreeNode(int data, TreeNode left, TreeNode right) {
this.data = data; this.left = left; this.right = right;
}
} // end class
Step by step
Solved in 3 steps with 2 images