二叉排序树(Java版)

二叉排序树

树节点

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+" ");
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值