思路与步骤
先做hashMap(ip,count)统计出现的次数。
遍历hashMap,根据次数为权值插入二叉树。
中序遍历树。
注:
前序(根左右),
中序(左根右),
后序(左右根)
节点定义
package tree;
public class Node {
/**
*
*/
private static final long serialVersionUID = 1L;
Node parent;
Node left;
Node right;
long value;
Object content;
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public void setRight(Node right) {
this.right = right;
}
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
public void setContent(Object content) {
this.content = content;
}
public Object getContent() {
return content;
}
}
树的定义
package tree;
/**
*
* @author xiaofancn@gmail.com
*
* @deprecated 二叉树是左边为权值最大。
*
*/
public class BinaryTree {
CallBack callBack = null;
/**
*
*/
private static final long serialVersionUID = 1L;
private Node root;
long size = 0;
public void addNode(Node node) {
if (node == null)
return;
if (root == null) {
root = node;
size++;
return;
}
Node point = root;
do {
if (point.value < node.value) {
if (point.left == null) {
node.setParent(point);
point.left = node;
size++;
break;
}
point = point.left;
} else {
if (point.right == null) {
node.setParent(point);
point.right = node;
size++;
break;
}
point = point.right;
}
} while (point != null);
}
public Node getLeftLast() {
if (root == null) {
return null;
}
Node point = root;
while (point.left != null) {
point = point.left;
}
return point;
}
public long size() {
return size;
}
public Node getRoot() {
return root;
}
public Node getRightLast() {
Node point = root;
while (point.right != null) {
point = point.right;
}
return point;
}
public void setCallBack(CallBack callBack) {
this.callBack = callBack;
}
/**
*
* 中序遍历
*
* @param node
*/
public void inItertor(Node node) {
if (node.getLeft() != null) {
inItertor(node.getLeft());
}
callBack.doThing(node);
if (node.getRight() != null) {
inItertor(node.getRight());
}
}
public interface CallBack {
void doThing(Node node);
}
}
测试代码
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import tree.BinaryTree;
import tree.BinaryTree.CallBack;
import tree.Node;
public class ClientMain {
static int index = 0;
static int top = 15;
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
Map<String, CountIP> map = new HashMap<String, CountIP>();
for (int i = 0; i < 10000 * 10000; i++) {
String ip = "192.168." + (int) (Math.random() * 254 + 1) + "."
+ (int) (Math.random() * 254 + 1);
if (map.get(ip) == null) {
CountIP cip = new CountIP();
cip.setCount(1);
cip.setIp(ip);
map.put(ip, cip);
} else {
CountIP cip = map.get(ip);
cip.setCount(cip.getCount() + 1);
map.put(ip, cip);
}
}
Set<String> set = map.keySet();
for (String ipkey : set) {
Node node = new Node();
node.setValue(map.get(ipkey).getCount());
node.setContent(map.get(ipkey).getIp());
binaryTree.addNode(node);
}
System.out.println(binaryTree.size());
Node point = binaryTree.getRoot();
binaryTree.setCallBack(new CallBack() {
@Override
public void doThing(Node node) {
if (index < top) {
index++;
System.out.println(node.getContent() + "===="
+ node.getValue());
}
}
});
binaryTree.inItertor(point);
}
private static class CountIP {
String ip;
long count;
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
}
分享到:
相关推荐
海量数据是发展趋势,对数据分析和挖掘也越来越重要,从海量数据中提取有用信息重要而紧迫,这便要求处理要准确,精度要高,而且处理时间要短,得到有价值信息要快,所以,对海量数据的研究很有前途,也很值得进行...
从海量素剧中查找中位数,从海量数据中查找一个数,海量数据问题
海量数据 优化 SQL海量数据 优化 SQL海量数据 优化 SQL海量数据 优化 SQL海量数据 优化 SQL海量数据 优化 SQL海量数据 优化 SQL海量数据 优化 SQL海量数据 优化 SQL海量数据 优化 SQL海量数据 优化 SQL海量数据 优化...
海量数据 海量数据 海量数据
西电海量数据管理大作业,有图,有设计思路
基于openlayers和canvas绘制海量数据的实现
常用大数据量,海量数据处理方法,算法总结,非常好的书。
在sqlserver数据库中,本例采用TOP语句分页,100W条数据为例,效率还不错,有测试的屏幕录像程序,有相关代码。
随着北部湾海洋生态资源的开发和利用,海量海洋科学数据飞速涌现出来,利用海量数据存储平台合理管理和存储这些科学数据显得极为重要.这里提出了一种基于分布式计算技术进行管理和存储海量海洋科学数据方法,构建了海量...
海量数据面试题整理海量数据面试题整理海量数据面试题整理海量数据面试题整理
涉及到海量数据的公司经常会问到。 下面的方法是我对海量数据的处理方法进行了一个一般性的总结, 当然这些方法可能并不能 完全覆盖所有的问题,但是这样的一些方法也基本可以处理绝大多数遇到的问题。 下面的一 些...
包含各种不常见的海量数据处理算法和相应的数据结构。确实是一本好资料啊
这是用python 来做海量数据处理,也许对喜欢python的人会有用。算法比较基本,大家可以参考
海量数据处理 !!!!!
海量数据处理方法
将海量数据导入到sql中将海量数据导入到sql中
[HiC2011]DISQL 2.0_一种海量数据处理语言(陈晓鸣).pdf ) .Google内部使用的海量数据处理语言
● 海量数据分库分表+文件存储:Mysql8.0+ShardingSphere多维度分库分表 + 阿里云OSS ● 实时计算+数据处理+存储可视化:Flink1.13 + ClickHouse + HDFS + 数据清洗分层 + Echart可视化数据 ● 分布式链路追踪+监控+...
西电海量数据管理实验报告
Java海量数据分页Bean, 适用于Oracle(适当修改,适用于任何数据库).功能描述:传入到达页码(具有容错性)、每页记录数、Select查询语句,返回该页所有的记录(整页是List集合,每条记录是一个 HashMap)、总行数、总...