Question: Write JAVA code for a Binary Search Tree that contains integers for its data. You can use BinarySearchTreeEx ample2 as the starting code. Once you

Write JAVA code for a Binary Search Tree that contains integers for its data. You can use BinarySearchTreeExample2 as the starting code. Once you have this code working, add code to do a level-order traversal of the tree. It should print out the data from level 1, the data from level 2, data from level n of the BST.

HINT: Consider using another data structure which we previously covered as a helper.

this is the BinarySearchTreeExample2
public class Tree {
// define the root node
private TreeNode root;
/*
* constructor for Tree class
*/
public Tree() {root = null;}
/*
* inserts a node in the Binary Serach Tree
*
*/
public synchronized void insertNode(int d)
{
// if the root node is null, insert data in the root
// ptherwise, call the insert method from the root node
if (root == null)
root = new TreeNode (d);
else
root.insert(d);
}
/**
* This is a recursive method for an inorder traversal of the tree
*/
public synchronized void inorderTraversal()
{
inorderHelper(root);
}
/**
* recursive method to do inorder traversal of tree
*/
private void inorderHelper(TreeNode node)
{
if (node == null)
return;
inorderHelper(node.left);
System.out.print(node.data + "");
inorderHelper(node.helper);
}
/**
* recursive method to do preorderTraversal of tree
*/
public synchronized void preorderTraversal()
{
preorderHelper(root);
}
/**
* recursive method to do preorder traversal of tree
*/
private void preorderHelper(TreeNode node)
{
if (node == null)
return;
System.out.print(node.data + "");
preorderHelper(node.left);
preorderHelper(node.right);
}
/**
* Recursive method to do postorderTraversal of tree
*/
public synchronized void postorderTraversal()
{
postorderHelper(root);
}
/**
* Recursive method to do postorder traversal of tree
*/
private void postorderHelper(Treenode node)
{
postorderHelper(node.left);
postorderHelper(node.right);
System.out.print(node.data + "");
}
}



public class TreeTest{
/**
* Test the tree class
*/
public static void main(String[]args)
{
//Define the tree
Tree tree = new Tree();
//define variable to hold data
int intVal;
//define an array of unique positive integers
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!