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));
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章