All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anysoft.util.datatree.DataTree Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.anysoft.util.datatree;


public class DataTree implements DataTreeBuilder {
	protected DataTreeNode root;
	
	public DataTree(){
		root = null;
	}
	
	public void scan(DataTreeBuilder scanner){
		if (scanner == null){
			return ;
		}
		
		if (root == null){
			//是一个空树
			return ;
		}
		
		Object cookies = scanner.rootFound(root.getContent());
		if (root.child != null){
			scan(cookies,root.child,scanner);
		}
	}
	
	protected void scan(Object cookies,DataTreeNode node,DataTreeBuilder scanner){
		DataTreeNode temp = node;
		
		while (temp != null){
			Object newCookies = scanner.nodeFound(cookies, temp.getContent());
			if (temp.child != null){
				scan(newCookies,temp.child,scanner);
			}
			temp = temp.brother;
		}
	}

	public Object nodeFound(Object _cookies, object _content) {
		if (_cookies == null) {
			// 插入的是根节点
			root = new DataTreeNode(_content);
			return root;
		}
		DataTreeNode newNode = new DataTreeNode(_content);
		@SuppressWarnings("unchecked")
		DataTreeNode parentNode = (DataTreeNode)_cookies;
		if (parentNode.child == null) {
			parentNode.child = newNode;
			return newNode;
		}

		DataTreeNode temp = parentNode.child;
		while (temp.brother != null) {
			temp = temp.brother;
		}
		temp.brother = newNode;

		return newNode;
	}

	public Object rootFound(object _root) {
		return nodeFound(null,_root);
	}
}