import javax.swing.*;
class Btree
{
Bnode root = new Bnode();
public static void main(String[] args)
{
new Btree();
}
public Btree()
{
int temp;
root = null;
temp = Integer.parseInt(JOptionPane.showInputDialog("Input data:"));
while (temp != -999)
{
Bnode newNode = new Bnode();
newNode.data = temp;
insert(root, newNode);
display(root);
System.out.println("==============================");
temp = Integer.parseInt(JOptionPane.showInputDialog("Input data:"));
}
int find = Integer.parseInt(JOptionPane.showInputDialog("Input data value to find:"));
int found = (sTree(find));
if (found == find)
{
System.out.println(find + " is found.");
}
else
{
System.out.println(find + " is not found.");
}
}
boolean isEmpty()
{
if (root == null)
{
return true;
}
else
{
return false;
}
}
int sTree(int d)
{
int returnFalse = -1;
Bnode current = root;
while(current != null)
{
if (current.data == d)
{
return d;
}
else if (current.data < d)
{
current = current.right;
}
else
{
current = current.left;
}
}
return returnFalse;
}
void display(Bnode current)
{
if (current != null)
{
display(current.left);
System.out.println(current.data);
display(current.right);
}
}
void insert(Bnode current, Bnode newNode)
{
if (root == null)
{
root = newNode;
}
else
{
if (newNode.data < current.data)
{
if (current.left == null)
{
current.left = newNode;
}
else
{
current = current.left;
insert(current, newNode);
}
}
else
{
if (current.right == null)
{
current.right = newNode;
}
else
{
current = current.right;
insert(current, newNode);
}
}
}
}
}
======================================================
class Bnode
{
int data;
Bnode left;
Bnode right;
}