二叉排序树
树节点
package test.tree;
/***
* ¶þ²æÊ÷½ÚµãÀà
* @author LENOVO
*
* @param <E>
*/
public class TreeNode<E> {
private E node;
private TreeNode<E> left;
private TreeNode<E> right;
public E getNode() {
return node;
}
public void setNode(E node) {
this.node = node;
}
public TreeNode<E> getLeft() {
return left;
}
public void setLeft(TreeNode<E> left) {
this.left = left;
}
public TreeNode<E> getRight() {
return right;
}
public void setRight(TreeNode<E> right) {
this.right = right;
}
public TreeNode() {
super();
// TODO Auto-generated constructor stub
}
public TreeNode(E node) {
super();
this.node = node;
}
}
排序树类:
package test.tree;
import java.lang.reflect.Field;
import java.util.List;
import test.exception.IsNotNumberException;
import test.exception.NotFindFieldException;
/***
* 二叉排序树
* @author LENOVO
*
* @param <E>
*/
public class Tree<E> {
public TreeNode<E> root;
private String field;
/**
* 构造二叉排序树
* @param clazz
* @param field
*/
public Tree(Class<E> clazz,String field){
super();
try {
Field f=clazz.getDeclaredField(field);
Class<?> c=f.getType();
if(c!=Double.class) {
}else {
this.field=field;
}
} catch (NoSuchFieldException e) {
}
}
/**
* 添加节点
* @param e
*/
public void add(E e) {
TreeNode<E> newnode =new TreeNode<E>(e);
if(root==null){
root=newnode;
}else{
TreeNode<E> center = root;
TreeNode<E> parent;
while(true){
parent=center;
Double keydata;
Double keydata2;
try {
Field f=e.getClass().getDeclaredField(this.field);
f.setAccessible(true);
Double n=(Double)f.get(e);
if(n==null) {
keydata=0.0;
}else {
keydata=n;
}
Field f2=center.getNode().getClass().getDeclaredField(this.field);
f2.setAccessible(true);
Double n2=(Double)f2.get(center.getNode());
if(n2==null) {
keydata2=0.0;
}else {
keydata2=n2;
}
} catch (Exception e1) {
}
if(keydata<keydata2){
center=center.getLeft();
if(center==null){
parent.setLeft(newnode);
break;
}
}else{
center=center.getRight();
if(center==null){
parent.setRight(newnode);
break;
}
}
}
}
}
/**
* 先序遍历
* @param node
*/
public void supprint(TreeNode<E> node){
if(node!=null){
Field f = null;
Double n=null;
try {
f = node.getNode().getClass().getDeclaredField(this.field);
f.setAccessible(true);
n=(Double)f.get(node.getNode());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
System.out.print(n+" ");
supprint(node.getLeft());
supprint(node.getRight());
}
}
/**
* 中序遍历
* @param node
*/
public void cenprint(TreeNode<E> node,List<E> list){
if(node!=null){
cenprint(node.getLeft(),list);
list.add(node.getNode());
cenprint(node.getRight(),list);
}
}
/**
* 后续遍历
* @param node
*/
public void subprint(TreeNode<E> node){
if(node!=null){
subprint(node.getLeft());
subprint(node.getRight());
Field f = null;
Integer n=null;
try {
f = node.getNode().getClass().getDeclaredField(this.field);
f.setAccessible(true);
n=(Integer)f.get(node.getNode());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
System.out.print(n+" ");
}
}
}