java实现树(链式存储)

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
  
  
   
    root = new Node
   
   
    
    ("A");
		Node
    
    
     
      b = new Node
     
     
      
      ("B");
		Node
      
      
        c = new Node 
       
         ("C"); Node 
        
          d = new Node 
         
           ("D"); Node 
          
            e = new Node 
           
             ("E"); Node 
            
              f = new Node 
             
               ("F"); Node 
              
                g = new Node 
               
                 ("G"); Node 
                
                  h = new Node 
                 
                   ("H"); MyLinkTree 
                  
                    tree = new MyLinkTree 
                   
                     (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)); } } 
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
      
     
     
    
    
   
   
  
  
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值