package net.oicp.shuangquan;
import java.io.*;
class Node{
int data;
Node lchild;
Node rchild;
}
/*class Tree{
}*/
public class BinaryTree {
/**
* @param args
*/
private Node root;
public BinaryTree()
{
root=null;
}
public void insert(int data)
{
Node tree=new Node();
tree.data=data;
//Node current=new Node();
//current=tree;
if(root==null)
{
root=tree;
//current=root;
}
else
{
Node current,parent;
current=root;
while(true)
{
parent=current;
if(data<current.data)
{
current=current.lchild;
if(current==null)
{
parent.lchild=tree;
return;
}
/*else
{
current=current.lchild;
}*/
}
else
{
current=current.rchild;
if(current==null)
{
parent.rchild=tree;
return;
}
}
}
}
}
public Node find(int a)
{
Node current=root;
while(a!=current.data)
{
current=a<current.data?current.lchild:current.rchild;
}
return current;
}
public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
BinaryTree tree=new BinaryTree();
System.out.println("Please input the value: ");
int j=Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
while(j!=0)
{
tree.insert(j);
j=Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
}
System.out.println("Please input the value what you want find: ");
int i=Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
System.out.println(tree.find(i).data);
}
}