//Tree 

var TreeNode = function(childClassName, name, value, childArray) {
	this.childClassName = childClassName;
	this.name = name;
	this.value = value;
	this.children = childArray;
};
TreeNode.prototype.insertSingleNode = function(childNode) {
	if (childNode == null)
		return;
	if (this.children == null)
		this.children = new Array();
	this.children.push(childNode);

};
TreeNode.prototype.insertNodes = function(childNodes) {
	if (this.children == null)
		this.children = new Array();
	if (childNodes == null)
		return;
	for ( var i = 0; i < childNodes.length; i++) {
		this.children.push(childNodes[i]);
	}

};
var Tree = function() {
	this.root = null;
};

Tree.prototype.createRoot = function(rootChildrenClassName,name, value) {
	this.root = new TreeNode(rootChildrenClassName,name, value, null);
};

Tree.prototype.getClondSubTreeByValue = function(value) {
	if (value == null)
		return null;
	if (this.root == null)
		return null;
	var clonedTree = new Tree();
	clonedTree.createRoot(this.root.childClassName, this.root.name,this.root.value);
	var parentNode = this.searchTreeNodeByValue(this.root, value);
	clonedTree.root.childClassName = parentNode.childClassName;
	this.getClonedSubTree(parentNode, clonedTree.root);// 这个就把引用传递过去了，应该没什么影响，我再看看吧
	return clonedTree;
};

Tree.prototype.searchTreeNodeByValue = function(parentNode, value) {

	if (parentNode.children == null)
		return null;

	for ( var i = 0; i < parentNode.children.length; i++) {
		if (value == parentNode.children[i].value) {
			return parentNode.children[i];
		}
		var node = this.searchTreeNodeByValue(parentNode.children[i], value);
		if (node != null)
			return node;
	}

};
Tree.prototype.getCloneTreeFromVlaue = function(pathValues) {

	if (pathValues == null)
		return null;
	if (pathValues.length <= 0)
		return null;
	if (this.root == null)
		return null;
	var clonedTree = new Tree();
	clonedTree.createRoot(this.root.childClassName,this.root.name, this.root.value);
	var currentNode = this.root;
	var clonedTreeCurrentNode = clonedTree.root;
	for ( var i = 0; i < pathValues.length; i++) {
		var flag = false;
		for ( var j = 0; j < currentNode.children.length; j++) {
			if (currentNode.children[j].value == pathValues[i]) {
				var clonedNode = new TreeNode(
						currentNode.children[j].childClassName,currentNode.children[j].name,
						currentNode.children[j].value, null);
				clonedTreeCurrentNode.insertSingleNode(clonedNode);
				clonedTreeCurrentNode = clonedNode;
				currentNode = currentNode.children[j];
				flag = true;
				break;
			}
		}
		if (flag == false) {
			return null;
		}

	}
	if (currentNode != null)
		this.getClonedSubTree(currentNode, clonedTreeCurrentNode);// 这个就把引用传递过去了，应该没什么影响，我再看看吧
	return clonedTree;
};

// no problem to copy the tree
Tree.prototype.getClonedSubTree = function(startNode, parentNode) {
	if (startNode == null)
		return null;
	if (startNode.children == null)
		return null;
	if (startNode.children.length <= 0)
		return null;
	for ( var i = 0; i < startNode.children.length; i++) {
		var node = new TreeNode(startNode.children[i].childClassName,startNode.children[i].name,
				startNode.children[i].value, null);
		parentNode.insertSingleNode(node);
		this.getClonedSubTree(startNode.children[i], node);
	}
};

Tree.prototype.insertToTree = function(insertedTree, singlePathTree) {
	if (singlePathTree == null)
		return;

	if (singlePathTree.children == null)
		return;
	if (singlePathTree.children.length < 1)
		return;
	if (insertedTree.children == null) {
		insertedTree.insertSingleNode(singlePathTree.children[0]);
	}
	var children = insertedTree.children;
	var flag = false;
	for ( var i = 0; i < children.length; i++) {
		if (children[i].value == singlePathTree.children[0].value) {

			this.insertToTree(children[i], singlePathTree.children[0]);
			flag = true;
			break;
		}
	}
	if (!flag)
		insertedTree.insertSingleNode(singlePathTree.children[0]);
};

Tree.prototype.deleteFromTree = function(pathValues) {
	if (pathValues == null)
		return;
	if (this.root == null)
		return;
	var nodeArray = this.getTreePathNodes(pathValues);
	var currentNode = nodeArray.pop();
	var length = nodeArray.length;
	for ( var i = 0; i < length; i++) {
		var parent = nodeArray.pop();
		if (parent.treeNode.children.length == 1
				&& currentNode.treeNode.children == null)
			parent.treeNode.children = null;
		else if (parent.treeNode.children.length > 1
				&& currentNode.treeNode.children == null) {
			parent.treeNode.children.splice(currentNode.index, 1);
		} else
			return;
		currentNode = parent;
	}
	;
};

Tree.prototype.getTreePathNodes = function(pathValues) {

	var DeleteNode = function(treeNode, index) {
		this.treeNode = treeNode;
		this.index = index;
	};
	if (pathValues == null)
		return;
	if (this.root == null) {
		information("error");
		return;
	}
	var currentNode = this.root;

	var nodeArray = new Array();
	nodeArray.push(new DeleteNode(this.root, 0));
	for ( var i = 0; i < pathValues.length; i++) {
		if (currentNode.children == null) {
			information("error");
			return null;
		}
		for ( var j = 0; j < currentNode.children.length; j++)
			if (currentNode.children[j].value == pathValues[i]) {
				nodeArray.push(new DeleteNode(currentNode.children[j], j));
				currentNode = currentNode.children[j];
				break;
			}

	}
	return nodeArray;
};
Tree.prototype.getClonedTree = function(tree) {
	var clonedTree = new Tree();
	clonedTree.root = this.getClonedNode(tree.root);
	return clonedTree;

};
Tree.prototype.getClonedNode = function(node) {
	if (node == null)
		return null;
	var clonedNode = new TreeNode(node.childClassName, node.name,node.value, null);
	if (node.children != null && node.children.length > 0) {
		for ( var i = 0; i < node.children.length; i++) {
			var childClonedNode = this.getClonedNode(node.children[i]);
			if (childClonedNode != null)
				clonedNode.insertSingleNode(childClonedNode);
		}
	}
	return clonedNode;
};
function getClonedSubTree(startNode, parentNode) {
	if (startNode == null)
		return null;
	if (startNode.children == null)
		return null;
	if (startNode.children.length <= 0)
		return null;
	for ( var i = 0; i < startNode.children.length; i++) {
		var node = new TreeNode(startNode.children[i].childClassName,startNode.children[i].name,
				startNode.children[i].value, null);
		parentNode.insertSingleNode(node);
		this.getClonedSubTree(startNode.children[i], node);
	}
};
