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