BST IMPLEMENTATION CSC248
2) BST WITH ADT
a) Class Student
public class Student
{
private int id;
private String name;
private int part;
private double cgpa;
public Student()
{
id = -1;
name = "";
part = -1;
cgpa = -1.0;
}
public Student(int id, String name, int part, double cgpa)
{
this.id = id;
this.name = name;
this.part = part;
this.cgpa = cgpa;
}
public void setStudent(int i, String n, int p, double c)
{
id = i;
name = n;
part = p;
cgpa = c;
}
public int getId()
{ return id; }
public String getName()
{ return name; }
public int getPart()
{ return part; }
public double getCgpa()
{ return cgpa; }
public String toString()
{ return("Id : " + id + " Name : " + name + " Part : " + part + " Cgpa : " + cgpa);
}
}
BST IMPLEMENTATION CSC248
b) Class TreeNode
class TreeNode {
// package access members
TreeNode left; //left node
Student data; // data item
TreeNode right; // right node
// Constructor: initialize data to d and make this a leaf node
public TreeNode( Student d )
{
data = d;
left = right = null; // this node has no children
}
// Insert a TreeNode into a Tree that contains nodes based on student id.
// Ignore duplicate values.
public void insert( Student d )
{ if ( d.getId() < data.getId() )
{ if ( left == null )
left = new TreeNode( d );
else
left.insert( d );
}
else if ( d.getId() >= data.getId() )
{
if ( right == null )
right = new TreeNode( d );
else
right.insert( d );
}
}
}
c) Class BSTree
class BSTree {
private TreeNode root;
// Construct an empty Tree of integers
public BSTree() { root = null; }
// Insert a new node in the binary search tree.
// If the root node is null, create the root node here.
// Otherwise, call the insert method of class TreeNode.
public void insertNode( Student d )
{
if ( root == null )
root = new TreeNode( d );
else
root.insert( d );
}
BST IMPLEMENTATION CSC248
//to display all records
public void display ()
{ print (root);}
private void print (TreeNode node)
{
if (node == null)
return;
else
System.out.println(node.data.toString());
print( node.left );
print( node.right);
}
//to count students from part 4
public int countStudPart4()
{ return countSP4( root ); }
// Recursive method to perform counting with condition
private int countSP4( TreeNode node )
{
if ( node == null )
return 0;
else
if (node.data.getPart()== 4)
return 1 + countSP4( node.left )+ countSP4( node.right );
else
return countSP4 (node.left) + countSP4(node.right);
}
//to count student who got dean list
public int countStudC3()
{ return countSCgpa3( root ); }
// Recursive method to perform counting with condition
private int countSCgpa3( TreeNode node )
{
if ( node == null )
return 0;
if (node.data.getCgpa() > 3.5)
return 1 + countSCgpa3( node.left )+ countSCgpa3( node.right );
else
return countSCgpa3 (node.left) + countSCgpa3(node.right);
}
public void printcat()
{
System.out.println("\nNumber of students with cgpa >= 2 and < 3 :" +
countcategory(root,2,3));
System.out.println("\nNumber of students with cgpa >= 3 and < 4 :" +
countcategory(root,3,4));
System.out.println("\nNumber of students with cgpa >= 1 and < 2 :" +
countcategory(root,1,2)); }
BST IMPLEMENTATION CSC248
// Recursive method to perform calcalution with condition
private int countcategory( TreeNode node, int low, int high)
{ if ( node == null )
return 0;
if (node.data.getCgpa() >= low && node.data.getCgpa() < high)
return 1+countcategory( node.left,low, high ) +
countcategory( node.right,low,high );
else
return countcategory (node.left,low,high) +
countcategory(node.right,low,high);
}
// Preorder Traversal
public void preorderTraversal()
{ preorderHelper( root ); }
// Recursive method to perform preorder traversal
private void preorderHelper( TreeNode node )
{
if ( node == null )
return;
System.out.println( node.data.toString());
preorderHelper( node.left );
preorderHelper( node.right );
}
// Inorder Traversal
public void inorderTraversal()
{ inorderHelper( root ); }
// Recursive method to perform inorder traversal
private void inorderHelper( TreeNode node )
{
if ( node == null )
return;
inorderHelper( node.left );
System.out.println( node.data.toString());
inorderHelper( node.right );
}
// Postorder Traversal
public void postorderTraversal()
{ postorderHelper( root ); }
// Recursive method to perform postorder traversal
private void postorderHelper( TreeNode node )
{
if ( node == null )
return;
postorderHelper( node.left );
postorderHelper( node.right );
System.out.println( node.data.toString());
}
BST IMPLEMENTATION CSC248
// Descending order based on student id
public void descending()
{des(root);}
private void des(TreeNode node)
{
if ( node == null )
return;
else
des(node.right);
System.out.println(node.data.toString());
des(node.left);
}
}
d) Class BSTApp
import javax.swing.JOptionPane;
public class BSTApp
{
public static void main(String [] args)
{
BSTree tree = new BSTree ();
// to input 5 students into the list
for (int i=0; i<5; i++)
{
String sIdStd = JOptionPane.showInputDialog("Enter student id");
String nameStd = JOptionPane.showInputDialog("Enter name");
String sPart = JOptionPane.showInputDialog("Enter part");
String sCgpa = JOptionPane.showInputDialog("Enter cgpa");
int iIdStd = Integer.parseInt(sIdStd);
int iPart = Integer.parseInt(sPart);
double dCgpa = Double.parseDouble(sCgpa);
Student stud = new Student(iIdStd, nameStd, iPart, dCgpa);
tree.insertNode(stud); //insert data
}
System.out.println("All students : ");
tree.display();
System.out.println("\nNumber of student from part 4 :"+ tree.countStudPart4());
System.out.println ("\nNumber of student who got dean list : " +
tree.countStudC3());
tree.printcat();
System.out.println ( "\nPreorder traversal :" );
tree.preorderTraversal();
System.out.println ( "\nInorder traversal :" );
tree.inorderTraversal();
System.out.println ( "\nPostorder traversal :" );
tree.postorderTraversal();
System.out.println ( "\nDecending order :" );
tree.descending();
} // end main
} // end class