怎样把树储存在内存里 如题求储存算法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没太明白lz的意思直接用cache存 树是由节点组成,给个节点类class Note { private Object element; private List<Note> childList; public Note(Object element, List<Note> childList) { this.element = element; this.childList = childList; } public Object getElement(){ return element; } public List<Note> getChildList(){ return childList; }} 有一棵树结构如下 root |_child1 |_child2 |_child21 . .~~~~ |~~~~ |_child2.. |_childxx |_childxx..从数据库里查询出childxx并迭代出如上所示的树,怎么才能在内存里把它表示出来呢求此树在内存里的存储算法 不建议LZ将整颗树存入内存中,如果小一些还好,要是数据量大一些的话可就够你喝一壶的了.通常的缓存可以通过cache来实现. 有一棵树结构如下 root |_child1 |_child2 |_child21 . .~~~~ |~~~~ |_child2.. |_childxx |_childxx.. 通过cache如何实现呢 import java.io.*; import java.util.Stack; public class myTest { private myTree tree; /** *二叉树的插入,参数为(关键字,数据) * **/ public void insert(int key, int data) { if(tree == null) { tree = new myTree(); tree.key = key; tree.data = data; } else { myTree newTree = new myTree(); newTree.key = key; newTree.data = data; myTree parent = tree; while(true) { if( newTree.key < parent.key) { if( parent.leftChild == null) { parent.leftChild = newTree; return; } else { parent = parent.leftChild; } } else if( newTree.key > parent.key) { if(parent.rightChild == null) { parent.rightChild = newTree; return; } else { parent = parent.rightChild; } } } } } /** * 二叉树的查找,参数为(关键字),返回值为 myTree的一个实例 * * **/ public myTree find(int key) { if( tree == null ) return null; myTree curr = new myTree(); curr.key = key; myTree parent = tree; while(true) { if( parent == null) { return null; } else if( curr.key == parent.key) { return parent; } else if( curr.key > parent.key) { parent = parent.rightChild; } else if( curr.key < parent.key) { parent = parent.leftChild; } } } /* * * 递归的二叉树中序遍历 * * */ private static void midOrder(myTree tree) { if(tree != null ) { midOrder(tree.leftChild); System.out.println(tree+","+tree.key+","+tree.data); midOrder(tree.rightChild); } } /* * 前序遍历 */ private static void frontOrder(myTree tree) { if(tree != null) { System.out.println(""+tree.key+" , "+tree.data); frontOrder(tree.leftChild); frontOrder(tree.rightChild); } } public static void main(String[] args) { System.out.println("Tree view Begin"); myTest t1 = new myTest(); t1.insert(8,25); t1.insert(5,9); t1.insert(58,87); t1.insert(13,82); t1.insert(4,8); t1.insert(12,54); t1.insert(53,123); t1.insert(56,47); t1.insert(2,75); t1.insert(34,5); t1.insert(6,23); System.out.println("现在开始遍历:"); midOrder2(t1.tree); midOrder(t1.tree); } } java二叉树算法,实现添加数据形成二叉树功能,并以先序的方式打印出来. jsp页面中获取分页查询记录的序号问题 急急急 关于Hibernate逆向工程中主外键的问题 神仙们请给我指点迷津! struts类包的简介 大家说webservice会不会代替ejb? 求助:struts和extssl的设置问题 拜托那位大虾给讲讲HIBERNATE与EJB之间的不同点及在一个项目中如何选择为好 小弟我刚开始学习struts,有问题请教。。。。。。。。。。。。。 小白求教 字符编码已经都一样了,怎么还。。。 请问大虾们一个javaee入门问题
private Object element;
private List<Note> childList; public Note(Object element, List<Note> childList) {
this.element = element;
this.childList = childList;
}
public Object getElement(){
return element;
}
public List<Note> getChildList(){
return childList;
}
}
|_child1
|_child2
|_child21
.
.~~~~
|~~~~
|_child2..
|_childxx
|_childxx..
从数据库里查询出childxx并迭代出如上所示的树,怎么才能在内存里把它表示出来呢
求此树在内存里的存储算法
不建议LZ将整颗树存入内存中,如果小一些还好,要是数据量大一些的话可就够你喝一壶的了.
通常的缓存可以通过cache来实现.
|_child1
|_child2
|_child21
.
.~~~~
|~~~~
|_child2..
|_childxx
|_childxx..
通过cache如何实现呢
import java.io.*;
import java.util.Stack;
public class myTest {
private myTree tree;
/**
*二叉树的插入,参数为(关键字,数据)
*
**/
public void insert(int key, int data)
{
if(tree == null)
{
tree = new myTree();
tree.key = key;
tree.data = data;
}
else
{
myTree newTree = new myTree();
newTree.key = key;
newTree.data = data;
myTree parent = tree;
while(true)
{
if( newTree.key < parent.key)
{
if( parent.leftChild == null)
{
parent.leftChild = newTree;
return;
}
else
{
parent = parent.leftChild;
}
}
else if( newTree.key > parent.key)
{
if(parent.rightChild == null)
{
parent.rightChild = newTree;
return;
}
else
{
parent = parent.rightChild;
}
}
}
}
}
/**
* 二叉树的查找,参数为(关键字),返回值为 myTree的一个实例
*
* **/
public myTree find(int key)
{
if( tree == null ) return null;
myTree curr = new myTree();
curr.key = key;
myTree parent = tree;
while(true)
{
if( parent == null)
{
return null;
}
else if( curr.key == parent.key)
{
return parent;
}
else if( curr.key > parent.key)
{
parent = parent.rightChild;
}
else if( curr.key < parent.key)
{
parent = parent.leftChild;
}
}
}
/*
*
* 递归的二叉树中序遍历
*
*
*/
private static void midOrder(myTree tree)
{
if(tree != null )
{
midOrder(tree.leftChild);
System.out.println(tree+","+tree.key+","+tree.data);
midOrder(tree.rightChild);
}
}
/*
* 前序遍历
*/
private static void frontOrder(myTree tree)
{
if(tree != null)
{
System.out.println(""+tree.key+" , "+tree.data);
frontOrder(tree.leftChild);
frontOrder(tree.rightChild);
}
}
public static void main(String[] args)
{
System.out.println("Tree view Begin");
myTest t1 = new myTest();
t1.insert(8,25);
t1.insert(5,9);
t1.insert(58,87);
t1.insert(13,82);
t1.insert(4,8);
t1.insert(12,54);
t1.insert(53,123);
t1.insert(56,47);
t1.insert(2,75);
t1.insert(34,5);
t1.insert(6,23);
System.out.println("现在开始遍历:");
midOrder2(t1.tree);
midOrder(t1.tree);
}
}
java二叉树算法,实现添加数据形成二叉树功能,并以先序的方式打印出来.