通用树

阅读更多
package com.test.me;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.locks.ReentrantLock;

public class Tree {
	
	class TreeNode {
		private TreeNode parent;
		private volatile Set children;
		private T attachment;

		public TreeNode(T attach) {
			attachment = attach;
		}

		@Override
		public int hashCode() {
			return attachment.hashCode();
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			TreeNode other = (TreeNode) obj;
			if (attachment == null) {
				if (other.attachment != null)
					return false;
			} else if (!attachment.equals(other.attachment))
				return false;
			return true;
		}
	}

	public Tree(T attach) {
		this.root = new TreeNode(attach);
	}

	private TreeNode root;

	private ReentrantLock lock = new ReentrantLock();

	public T getRoot() {
		return root.attachment;
	}

	/**
	 * 在parent下增加一个子节点,
	 *
	 * @return 只有parent存在于树内,且将要插入的不在tree内,插入成功才返回true
	 */
	public boolean addchild(T parent, T attachment) {
		TreeNode parentNode = findTreeNode(parent);

		// 只有attachment不存的时才加入
		if (parentNode != null) {

			// 新增children的List ,保证线程安全
			if (parentNode.children == null) {
				try {
					lock.lock();
					if (parentNode.children == null) {
						parentNode.children = Collections.synchronizedSet(new HashSet());
					}
				} finally {
					lock.unlock();
				}
			}

			TreeNode node = new TreeNode(attachment);
			node.parent = parentNode;
			
			return parentNode.children.add(node);
		}
		return false;
	}

	/**
	 * 删除对应节点 只有当节点存在才返回true.
	 * 该方法暂时不提供外部使用。所有数据都要从新生成,然后获取
	 * @param 是否级联删除
	 *            ,如果为true执行级连删除;false,如果有child则不执行删除
	 * */
	protected boolean remove(T attachment, boolean cascade) {
		TreeNode node = findTreeNode(attachment);
		if (node != null) {
			// 如果不是级联删除 ,并且 有子级节,则返回false
			if (!cascade && node.children != null && node.children.size() > 0) {
				return false;
			}
			TreeNode parent = node.parent;
			return parent.children.remove(node);
		} else {
			return false;
		}
	}

	/**
	 * 获取node下所有后代节点
	 */
	public List getDescendants(T attachment) {
		TreeNode node = findTreeNode(attachment);
		if (node != null) {
			List list = new ArrayList();
			Stack set = new Stack();
			if(node.children!=null && node.children.size()>0)
			{
				for (TreeNode children : node.children) {
					set.push(children);
				}
			}
			// 开始遍历当前结果下所有 子孙节点
			while (!set.isEmpty()) {
				TreeNode cur = set.pop();
				list.add(cur.attachment);

				if (cur.children != null && cur.children.size() > 0) {
					for (TreeNode children : cur.children) {
						set.push(children);
					}
				}
			}

			return list;
		}
		return null;
	}

	/**
	 * 查询所有父级节点。从root向下排序,第一个是root
	 * */
	public List getAncestors(T attach) {
		TreeNode node = findTreeNode(attach);
		if (node != null && node.parent!=null) {
			List list = new ArrayList();
			TreeNode cur = node.parent;
			while (cur != root && cur != null) {
				list.add(cur.attachment);
				cur = cur.parent;
			}
			//如果是root
			if (cur == root) {
				list.add(root.attachment);
				Collections.reverse(list);
				return list;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 获取子树,不包含孙级节点
	 */
	public List getChild(T attachment) {
		TreeNode node = findTreeNode(attachment);
		if (node != null && node.children!=null) {
			List list = new ArrayList();
			
			for(TreeNode child : node.children)
			{
				list.add(child.attachment);
			}
			return list;
		}
		return null;
		
	}

	/**
	 * 获得父级节点。
	 */
	public T getFather(T attachment) {
		TreeNode node = findTreeNode(attachment);
		if (node != null && node != this.root) {
			return node.parent.attachment;
		}
		return null;
	}
	
	/**
	 * 查找对象,如果存在测返回对象,否则返回null
	 * */
	private TreeNode findTreeNode(T attachment) {

		Stack set = new Stack();
		set.push(root);

		// 下面开始遍历tree
		TreeNode cur = root;
		while ((!set.empty()) && attachment != cur.attachment && (!attachment.equals(cur.attachment))) {
			cur = set.pop();
			if (cur.children != null && cur.children.size() > 0) {
				for (TreeNode children : cur.children) {
					set.push(children);
				}
			}
		}

		if (attachment.equals(cur.attachment)) {
			return cur;
		} else {
			return null;
		}
	}
}

 

你可能感兴趣的:(java,tree)