主类:
import bst.*;
public class BinarySearch
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
List<BST> list = new ArrayList<BST>();
BST<Integer,Integer> bst = new BST<Integer,Integer>();
while(scan.hasNext())
{
int number = scan.nextInt();
for(int i=0;i<number;i++)
{
bst.put(scan.nextInt(),number);
}
list.add(bst);
}
//bst.inOrderTraverse();
for(int i=0;i<list.size();i++)
{
list.get(i).preOrderTraverse();
}
}
sort包中的BST类:
public class BST<Key extends Comparable<Key>,Value>
{
private Node root;
public Key getRoot(){
return root.getKey();
}
//BST插入算法
public void put(Key key,Value val)
{
root = put(root,key,val);
}
public Node put(Node x,Key key,Value val)
{
if(x==null) return new Node(key,val,1);
int cmp = key.compareTo(x.key);
if(cmp<0)
x.left = put(x.left,key,val);
else if(cmp>0)
x.right = put(x.right,key,val);
else
x.val = val;
x.N =size(x.left)+size(x.right)+1;
return x;
}
//BST查找算法
public Value get(Key key)
{
Node x =root;
while(x!=null)
{
int cmp = key.compareTo(x.key);
if(cmp<0) x= x.left;
else if(cmp>0) x = x.right;
else return x.val;
}
return null;
}
//BST删除算法
public void delete(Key key)
{
root = delete(root,key);
}
public Node delete(Node x,Key key)
{
if(x==null) return null;
int cmp = key.compareTo(x.key);
if (cmp < 0) x.left = delete(x.left,key);
else if(cmp>0) x.right = delete(x.right,key);
else
{
if(x.right == null) return x.left;
if(x.left == null) return x.right;
Node t = x;
x = min(t.right);
x.right = deleteMin(t.right);
x.left = t.left;
}
x.N = size(x.left) + size(x.right) + 1;
return x;
}
//BST最小键求法
public Key min()
{
return min(root).key;
}
public Node min(Node x)
{
if(x.left == null) return x;
return min(x.left);
}
//BST删除最小值算法
public void deleteMin()
{
root = deleteMin(root);
}
private Node deleteMin(Node x)
{
if(x.left==null) return x.right;
x.left = deleteMin(x.left);
x.N = size(x.left) + size(x.right) + 1;
return x;
}
//先根遍历算法,寻找父亲结点
public void preOrderTraverse()
{
preOrderTraverse(root);
}
private void preOrderTraverse(Node x)
{
Node T = x;
if(T==null) return;
if(x.left!=null) x.left.setParent(x);
if(x.right!=null) x.right.setParent(x);
if(T==this.root) System.out.println("-1"+" ");
else
System.out.println(x.getParent().getKey()+" ");
preOrderTraverse(x.left);
preOrderTraverse(x.right);
}
//中序遍历算法
public void inOrderTraverse()
{
inOrderTraverse(root);
}
private void inOrderTraverse(Node root)
{
Node T= root;
if(T==null) return;
inOrderTraverse(root.left);
System.out.print(root.key+" ");
inOrderTraverse(root.right);
}
//返回总结点个数
public int size()
{
return size(root);
}
public int size(Node x)
{
if(x == null) return 0;
else return x.N;
}
private class Node
{
private Key key;
private Value val;
private Node parent,left,right;
private int N;
public Node(Key key,Value val,int N)
{
this.key = key;
this.val = val;
this.N = N;
}
public Node getParent()
{
return this.parent;
}
public void setParent(Node parent)
{
this.parent = parent;
}
public Key getKey()
{
return this.key;
}
public void setKey(Key key)
{
this.key = key;
}
}
}