数组打包不同的元素

数组打包不同的元素:

给定指定的数组,将数组打成不同的包,确保每包的元素不一样

public class Node<T> {
	private T data;
	private int parent_id;
	private int self_id;

	public Node() {
	}

	public Node(T data) {
		super();
		this.data = data;
	}

	public Node(T data, int parent_id, int self_id) {
		super();
		this.data = data;
		this.parent_id = parent_id;
		this.self_id = self_id;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public int getParent_id() {
		return parent_id;
	}

	public void setParent_id(int parent_id) {
		this.parent_id = parent_id;
	}

	public int getSelf_id() {
		return self_id;
	}

	public void setSelf_id(int self_id) {
		this.self_id = self_id;
	}

}

public class CustomTree<T> {

	private final int DEFAULT_SIZE = 2;
	private int size;
	private int count;
	private Object[] nodes;

	public CustomTree() {
		this.size = this.DEFAULT_SIZE;
		this.nodes = new Object[this.size];
		this.count = 0;
	}

	public CustomTree(Node<T> root) {
		this();
		this.count = 1;
		this.nodes[0] = root;
	}

	public CustomTree(Node<T> root, int size) {
		this.size = size;
		this.nodes = new Object[this.size];
		this.count = 1;
		this.nodes[0] = root;
	}

	// add node
	public void add(Node<T> node) {
		for (int i = 0; i < this.size; i++) {
			if (this.nodes[i] == null) {
				nodes[i] = node;
				break;
			}
		}
		this.count++;
	}

	// check length
	public void check() {
		if (this.count >= this.size) {
			this.enlarge();
		}
	}

	// add node and point to parent node
	public void add(Node<T> node, Node<T> parentNode) {
		this.check();
		node.setParent_id(this.position(parentNode));
		this.add(node);
		node.setSelf_id(this.position(node));
	}

	// get node position index
	public int position(Node<T> node) {
		for (int i = this.count - 1; i >= 0; i--) {
			if (nodes[i] == node) {
				return i;
			}
		}
		return -1;
	}

	// get the count of node
	public int getSize() {
		return this.count;
	}

	// get root node
	@SuppressWarnings("unchecked")
	public Node<T> getRoot() {
		return (Node<T>) this.nodes[0];
	}

	// get all nodes by list return
	@SuppressWarnings("unchecked")
	public List<Node<T>> getAllNodes() {
		List<Node<T>> list = new ArrayList<Node<T>>(this.size);
		for (int i = 0; i < this.size; i++) {
			if (this.nodes[i] != null) {
				list.add((Node<T>) nodes[i]);
			}
		}
		return list;
	}

	// get tree depth, if only have root node, the return 1
	@SuppressWarnings("unchecked")
	public int getDepth() {
		int max = 1;
		if (this.nodes[0] == null) {
			return 0;
		}

		for (int i = 0; i < this.count; i++) {
			int deep = 1;
			Node<T> element = (Node<T>) this.nodes[i];
			if (element != null) {
				int location = element.getParent_id();
				while (location != -1 && this.nodes[location] != null) {
					location = ((Node<T>) (this.nodes[location])).getParent_id();
					deep++;
				}
				if (max < deep) {
					max = deep;
				}
			}
		}
		return max;
	}

	public List<Node<T>> getChildNodes(Node<T> node) {
		List<Node<T>> resultList = new ArrayList<Node<T>>(this.size);
		if (this.nodes == null || this.nodes.length == 0 || nodes.length == 1) {
			return null;
		}
		for (int i = 1; i < this.count; i++) {
			@SuppressWarnings("unchecked")
			Node<T> element = (Node<T>) this.nodes[i];
			if (element != null) {
				int parentId = element.getParent_id();
				if (parentId == node.getSelf_id()) {
					resultList.add(element);
				}
			}
		}
		return resultList;
	}

	public void removeNode(Node<T> node) {
		if (this.nodes == null || this.nodes.length == 0) {
			return;
		}
		if (node == this.nodes[0]) {
			this.nodes = null;
		} else {
			List<Node<T>> list = this.getChildNodes(node);
			for (Node<T> subNode : list) {
				subNode.setParent_id(node.getParent_id());
			}
			nodes[this.position(node)] = null;
		}
	}

	public void enlarge() {
		this.size = this.size + this.DEFAULT_SIZE;
		this.nodes = Arrays.copyOf(nodes, this.size);
	}
}

public class TestDemo {

	private static final int PACKAGE_SIZE = 5;

	public static void main(String[] args) {
		String[] strArr = new String[] { "B", "A", "A", "A", "B", "B", "C", "E", "F", "F", "G", "A", "A", "A", "B", "B", "C", "E", "F", "F", "H" };
		List<String> list = Arrays.asList(strArr);
		Collections.sort(list);
		System.out.println(list.toString());

		Node<String> rootNode = new Node<String>("root", -1, 0);
		CustomTree<String> myTree = new TestDemo().doSomething2(list, rootNode);
		List<Node<String>> childNodes = myTree.getChildNodes(rootNode);
		List<Node<String>> result = new ArrayList<Node<String>>();
		int i = 0;
		while (i < childNodes.size()) {
			Node<String> elementNode = childNodes.get(i);
			result.add(elementNode);
			if ((i + 1) % PACKAGE_SIZE == 0 || i == (childNodes.size() - 1)) {
				for (Node<String> node : result) {
					System.out.println(node.getData().toString());
					myTree.removeNode(node);
				}
				childNodes = myTree.getChildNodes(rootNode);
				result.clear();
				i = 0;
				System.out.println("-----------");
			} else {
				i++;
			}
		}
	}

	public <T> CustomTree<T> doSomething2(List<T> list, Node<T> rootNode) {
		CustomTree<T> myTree = new CustomTree<T>(rootNode, list.size());
		Node<T> temp = null;
		boolean flag = true;
		for (int i = 0; i < list.size() - 1; i++) {
			T element = list.get(i);
			T nextElement = list.get(i + 1);
			Node<T> subNode = new Node<T>(element);
			if (flag) {
				myTree.add(subNode, rootNode);
				temp = subNode;
				flag = false;
			}
			Node<T> nextSubNode = new Node<T>(nextElement);
			if (element.equals(nextElement)) {
				myTree.add(nextSubNode, temp);
			} else {
				myTree.add(nextSubNode, rootNode);
			}
			temp = nextSubNode;
		}
		return myTree;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值