java实现树(链式存储)
来源:互联网 发布:七日杀不让刷js 编辑:程序博客网 时间:2024/06/05 11:56
java实现树,采用链式存储,父节点记录子节点的存储位置。
首先定义一个用于存储子节点位置的节点类
- package my.tree.link;
- public class SubNode {
- private int location;
- private SubNode next;
- public SubNode(){
- this.location = 0;
- this.next = null;
- }
- public SubNode(int location){
- this.location = location;
- this.next = null;
- }
- public SubNode(int location, SubNode next){
- this.location = location;
- this.next = next;
- }
- public void setLocation(int location){
- this.location = location;
- }
- public int getLocation(){
- return this.location;
- }
- public void setNext(SubNode next){
- this.next = next;
- }
- public SubNode getNext(){
- return this.next;
- }
- }
然后定义一个用于存储节点信息的节点类
- package my.tree.link;
- public class Node<T> {
- private T data;
- private SubNode son;
- public Node(){
- }
- public Node(T data){
- this.data = data;
- this.son = null;
- }
- public Node(T data, SubNode son){
- this.data = data;
- this.son = son;
- }
- public void setData(T data){
- this.data = data;
- }
- public T getData(){
- return this.data;
- }
- public void setSon(SubNode son){
- this.son = son;
- }
- public SubNode getSon(){
- return this.son;
- }
- @Override
- public String toString(){
- return "节点:" + this.data;
- }
- }
编写链式存储的树类,这里采用递归求解树的深度(貌似有问题,在求树的深度是,很迷糊)
- package my.tree.link;
- import java.util.LinkedList;
- import java.util.List;
- public class MyLinkTree<T> {
- private final int DEFAUL_SIZE = 10;
- private int size;
- private int count;
- private Node<T>[] nodes;
- @SuppressWarnings("unchecked")
- public MyLinkTree() {
- this.size = this.DEFAUL_SIZE;
- this.nodes = new Node[this.size];
- this.count = 0;
- }
- @SuppressWarnings("unchecked")
- public MyLinkTree(int size) {
- this.size = size;
- this.nodes = new Node[this.size];
- this.count = 0;
- }
- public MyLinkTree(T data) {
- this();
- Node<T> node = new Node<T>(data);
- this.nodes[0] = node;
- this.count++;
- }
- public MyLinkTree(Node<T> root) {
- this();
- this.nodes[0] = root;
- this.count++;
- }
- public void add(Node<T> node, Node<T> parent) {
- SubNode son = new SubNode();
- for (int i = 0; i < this.size; i++) {
- if (this.nodes[i] == null) {
- this.nodes[i] = node;
- son.setLocation(i);
- break;
- }
- }
- // 往链表中添加子节点位置
- SubNode next = parent.getSon();
- if (next != null) {
- while (next.getNext() != null) {
- next = next.getNext();
- }
- next.setNext(son);
- } else {
- parent.setSon(son);
- }
- this.count++;
- }
- public int size() {
- return this.count;
- }
- public Node<T> getRoot() {
- return this.nodes[0];
- }
- // 获取指定节点的子节点
- public List<Node<T>> getSon(Node<T> parent) {
- List<Node<T>> list = new LinkedList<Node<T>>();
- SubNode son = parent.getSon();
- while (son != null) {
- list.add(this.nodes[son.getLocation()]);
- son = son.getNext();
- }
- return list;
- }
- // 获取树的深度,通过递归的方式来解决
- public int getDepth(Node<T> node) {
- SubNode son = node.getSon();
- if(son == null){
- return 1;
- }else{
- int max = 0;
- while(son != null){
- int temp = this.getDepth(this.nodes[son.getLocation()]);
- max = temp > max ? temp : max;
- son = son.getNext();
- }
- //为什么要max+1?
- return max+1;
- }
- }
- public int deep() {
- int max = 0;
- for (int i = 0; i < this.count; i++) {
- int temp = this.getDepth(this.nodes[i]);
- max = max > temp ? max : temp;
- }
- return max;
- }
- }
最后编写一个测试端,用来测试功能是否基本实现
- package my.tree.link;
- public class MyLinkTreeClient {
- public static void main(String[] args) {
- Node<string> root = new Node<string>("A");
- Node<string> b = new Node<string>("B");
- Node<string> c = new Node<string>("C");
- Node<string> d = new Node<string>("D");
- Node<string> e = new Node<string>("E");
- Node<string> f = new Node<string>("F");
- Node<string> g = new Node<string>("G");
- Node<string> h = new Node<string>("H");
- MyLinkTree<string> tree = new MyLinkTree<string>(root);
- tree.add(b, root);
- // tree.add(c, root);
- // tree.add(d, root);
- // tree.add(e, b);
- // tree.add(f, b);
- // tree.add(g, f);
- // tree.add(h, g);
- // System.out.println(tree.size());
- System.out.println(tree.deep());
- // System.out.println(tree.getSon(b));
- }
- }</string></string></string></string></string></string></string></string></string></string></string></string></string></string></string></string></string></
0 0
- java实现树(链式存储)
- java实现树(链式存储)
- 队列(链式存储) java实现
- 二叉树的顺序存储和链式存储结构(java实现)
- 使用java实现链式存储的二叉树
- 数据结构(java语言描述)-- 二叉查找树的链式存储结构的实现
- Java实现链式存储的线性表
- java实现线性表中的链式存储
- 栈的Java实现(顺序存储实现与链式存储实现)
- 二叉树的链式存储与实现(C++)
- 线性表 顺序存储 链式存储 ---java实现
- 二叉树链式存储的实现
- hdu-4217-线段树-链式存储实现
- 二叉树链式存储的实现
- 数据结构:二叉树的链式存储实现
- 二叉树链式存储的C实现
- 二叉树的链式存储实现
- 二叉树的链式存储实现
- Android四大组件之Service
- Objective-C规范注释心得——同时兼容appledoc(docset、html)与doxygen(html、pdf)的文档生成
- UI01-UIView(示例代码)
- C++ 策略模式
- mysql密码mac
- java实现树(链式存储)
- 清空联系人数据
- Objective-C内存管理第三弹:@property的使用
- 一些和图像图形有关的资源
- C程序练习
- 利用Socket建立网络连接的步骤
- 如何使用ShoeBox和PhotoShop制作出漂亮的Fnt字体
- 几个私有代码库托管网站
- 深入学习JavaScript对象(转+学)