////////////////////////////////////////////////节点类 TreeNode.java //////////////////////////
package com.tree;
class TreeNode {//包访问权限
long data ;
long x,y;
String adrr;
TreeNode left;
TreeNode right;
int size=0;
public TreeNode(){};
public TreeNode(long data,long x,long y,String addr){
size++;
this.data=data;
this.x=x;
this.y=y;
this.adrr=addr;
}
public TreeNode(long data ,TreeNode left ,TreeNode right){
size++;
this.data=data;
this.left=left;
this.right=right;
}
public long getData() {
return data;
}
public void setData(long data) {
this.data = data;
}
public long getX() {
return x;
}
public void setX(long x) {
this.x = x;
}
public long getY() {
return y;
}
public void setY(long y) {
this.y = y;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getAdrr() {
return adrr;
}
public void setAdrr(String adrr) {
this.adrr = adrr;
}
}
///////////////////////////////////////// tree.java /////////////////////////////
package com.tree;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import com.vdoany.db.DbHandle;
public class Tree {
private static TreeNode root;
public static DbHandle dbHandle = new DbHandle();// 用于处理数据库操作
public static ResultSet rs = null;
public static TreeNode tableNumArr[] = new TreeNode[89405];// 用于存ip
public static long randNumArr[] = new long[10000];// 用于存ip
int size = 0;// 统计树节点大小
static int succ = 0;// 找了多少区域
// 插入数据
int count = 0;
/**
* 插入一条数据
*
* @param node
* @param data
* @param x
* @param y
* @param arr
* @return
*/
public TreeNode insert(TreeNode node, long data, long x, long y, String arr) {
if (node == null) {
size++;
return new TreeNode(data, x, y, arr);
} else {
if (data < node.data) {
node.left = insert(node.left, data, x, y, arr);
} else {
node.right = insert(node.right, data, x, y, arr);
}
return node;
}
}
/**
* 插入数据,变成一颗随机树
*
* @param treeNode
* @return
*/
public TreeNode insert(TreeNode treeNode[]) {
if (treeNode == null) {
root = null;
return root;
}
for (int t = 0; t < treeNode.length; t++) {
String arr = treeNode[t].getAdrr();
long x = treeNode[t].getX();
long y = treeNode[t].getY();
root = insert(root, y, x, y, arr);
}
return root;
}
/**
* 中序遍历树
*/
public void inOrder(TreeNode parent) {
if (parent == null) {
return;
}
inOrder(parent.left);
System.out.println(parent.data);
inOrder(parent.right);
}
/**
* 读取节点数
*
* @return
*/
public int size() {
return size;
}
/**
* 将二叉树转为按中序排列的一条链
*/
TreeNode createChain(TreeNode root) {
if (root != null) {
root.right = createChain(root.right);// 对右子树递归
if (root.left != null) {
TreeNode oldNode = root;
TreeNode ref = root = createChain(root.left);// 对左子树递归,根指向最左结点
oldNode.left = null;
while (ref.right != null)
ref = ref.right;
ref.right = oldNode;
}
}
return root;
}
/**
* 将链转为平衡二叉树
*/
TreeNode createTree(TreeNode root, int n) {
if (n > 2) {
int m = (n – 1) / 2;
TreeNode oldRoot = root;
TreeNode ref = root;
for (int i = 1; i < m; i++)
ref = ref.right;
root = ref.right; // 指向第[n/2]个结点,即将根指向链的中间结点
ref.right = null; // 根的左右结点都置空
root.left = createTree(oldRoot, m);
root.right = createTree(root.right, n – m – 1);
}
return root;
}
/**
* 查找数据
*/
public void searchData(TreeNode root, long data) {
if (root == null) {
System.out.println(“找不到您要的IP!”);
return;
}
// 如果小于当时数据,则在但前树中找
if (data < root.data && data < root.x) {
searchData(root.left, data);// 向左查找
} else if (data < root.data && data > root.x) {
System.out.println(root.x + “——–” + root.y + “———“
+ root.adrr);// 打印所在区域
succ = succ + 1; // 找到则加1
} else if (data > root.data) {
searchData(root.right, data);// 向右查找
} else if (data == root.data || data == root.x) {
System.out.println(root.x + “——–” + root.y + “———“
+ root.adrr);// 打印所在区域
succ = succ + 1; // 找到则加1
}
}
/**
* 返回树的层数
*
* @param p
* @return
*/
public int h(TreeNode p) {
if (p == null)
return -1;
return 1 + Math.max(h(p.left), h(p.right));// 返回较大的
}
/**
* 检查是否是平衡树
*
* @param p
* @return
*/
public boolean checkAVL(TreeNode p) {
if (p == null)
return true;
// 左子树与右子树绝对值不能超过 1,并且左右子树也是平衡二叉树
return (Math.abs(h(p.left) – h(p.right)) <= 1 && checkAVL(p.left) && checkAVL(p.right));
}
/**
* 获取表Ip的数据
*/
public void getDataByDB() {
String sql = “select * from IPtest”;
rs = dbHandle.executeQuery(sql);
int i = 0;
try {
while (rs.next()) {
long x = rs.getInt(“X”);
long y = rs.getInt(“Y”);
String adrr = rs.getString(“NAME”);
TreeNode treeNode = new TreeNode();
treeNode.setX(x);
treeNode.setY(y);
treeNode.setAdrr(adrr);
tableNumArr[i++] = treeNode;
// System.out.println(“X= “+adrr+” Y= “+y);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取随机数
*/
public static long[] getRandomNum() {
Random r = new java.util.Random(); // 无种子(根据但前系统时间System.currentTimeMillis();每次产生不同的值
for (int i = 0; i < 10000; i++) {
long randomNUM = r.nextInt(2090000000);
randNumArr[i] = randomNUM;
// System.out.println(“randomNUM[“+i+”]= “+randomNUM);
}
return randNumArr;
}
public static void main(String args[]) {
TreeNode temp = new TreeNode();
Tree treeTemp = new Tree();
TreeNode root1 = null;
treeTemp.getDataByDB(); // 读取ip表数据
root1 = treeTemp.insert(tableNumArr);// 插入数据构建二叉树
temp = treeTemp.createChain(root1);// 将二叉树转为按中序排列的一条链
root = treeTemp.createTree(temp, treeTemp.size() – 1);// 平衡
// ////////////////////////////////////////
long randArr[] = getRandomNum();// 输出随机数
long start = System.currentTimeMillis();// 取当前秒数
for (int r = 0; r < randArr.length; r++) {
treeTemp.searchData(root, randArr[r]);// 根据ip查找区域
}
System.out.println(“随机生成了”+randArr.length+”条————“+”成功找到了” + succ + “条”);
long executeTime = (System.currentTimeMillis() – start);
System.out.println(“该程序执行了:” + executeTime + ” 毫秒”);
System.out.println(“treeTemp.size()=” + treeTemp.size());// 树的节点总数
// ///////////////////////////////////////////////////////
// 测试平衡以后树的效率
/*
* long start2 = System.currentTimeMillis();//取当前秒数 int
* arr2[]={2051244001}; for(int t=0;t<10000;t++){//测试10000条数据
* treeTemp.searchData(root1,arr2[0]); }
* System.out.println(“成功找到了”+succ+”条”); long executeTime2 =
* (System.currentTimeMillis()-start2);
* System.out.println(“该程序执行了222:”+executeTime2+” 毫秒”);
* ////////////////////////////////////////////////////////
*
*/
// temp = treeTemp.createChain(root);//
// boolean ifAVL = treeTemp.checkAVL(root);//验证是否是平衡二叉树
// System.out.println(ifAVL);
// root = treeTemp.createTree(temp,treeTemp.size()-1);//平衡
// boolean ifAVL2 = treeTemp.checkAVL(root);//验证是否是平衡二叉树
// System.out.println(ifAVL2);
// System.out.println(“平衡二叉树的中序遍历如下:”);
// treeTemp.inOrder(root);
}
}