多叉树的建立以及其他的一些操作

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class TreeNode {
	public int id;
	List <TreeNode>childList;
	String nodeName;
	double nodeValue;



	int ParentId;

	public void initChildTree(){
		if(this.getChildList()==null){
			this.childList = new ArrayList();
		}
	}
	//向当前节点插入node
	public void insertTree(TreeNode node){
		initChildTree();
		node.setId(++selfid);
		this.childList.add(node);
	}

	//根据节点的ParentId插入树种对应的ParentId的子节点中
	public void insertTreeByParentId(TreeNode node){
		if(node.ParentId==this.ParentId){
			node.setId(++selfid);
			this.childList.add(node);
			
		}else{
			List<TreeNode>child = this.getChildList();
			for(int i = 0;i<child.size();i++){
				child.get(i).insertTreeByParentId(node);
			}
		}

	}
      //删除最近的节点 public void delectNow(){ delectById(selfid); selfid = selfid-1; } //根据id删除节点 public boolean delectById(int id){ if(this.childList==null){ return false; } int childNum = this.getChildList().size(); for(int i = 0;i<childNum;i++){ if(this.getChildList().get(i).getId()==id){ this.getChildList().remove(i); return true; }else{ this.getChildList().get(i).delectById(id); } } return false; } //根据id找到树种某一个节点 public TreeNode findTreeNodeById(int id) { TreeNode t =new TreeNode(); if(this.id==id){ return this; }else{ List <TreeNode>child = this.getChildList(); for(int i = 0;i<child.size();i++){ t=child.get(i).findTreeNodeById(id); if(t.nodeName!=null){ return t; } } } return t; } //将数组中每一组数据作为一列,添加到data中,attribute表示属性列 static int selfid = 0; public TreeNode creatTree(TreeNode tree,List<ArrayList<Double>>data,List<String>attribute){ if(tree==null){ tree = new TreeNode(); tree.setId(selfid); tree.setNodeName("start"); tree.setNodeValue(0); tree.setChildList(new ArrayList()) ; } if(attribute.size()==0){ return tree; } List<Double>value = new ArrayList();//用于记录data第0列中不重复的属性 List<List<ArrayList<Double>>>childD = new ArrayList();//用于记录data第0列中不重复的属性所对应的数据列 for(int i = 0;i<data.size();i++){ if(!value.contains(data.get(i).get(0))){ value.add(data.get(i).get(0)); } } //针对每一个value值,找到对应的childD for(int i = 0;i<value.size();i++){ List<ArrayList<Double>> temp = new ArrayList<ArrayList<Double>>(); for(int j = 0;j<data.size();j++){ if(data.get(j).get(0).equals(value.get(i))){ temp.add(data.get(j)); } } // for(int k = 0;k<temp.size();k++){ // for(int j = 0;j<temp.get(k).size();j++){ // System.out.print(temp.get(k).get(j)); // } // System.out.println(); // } childD.add(temp); } String NodeName = attribute.get(0); for(int i = 0;i<value.size();i++){ TreeNode leafNode = new TreeNode(); leafNode.setParentId(tree.getId()); leafNode.setId(++selfid); leafNode.setNodeName(NodeName); leafNode.setNodeValue(value.get(i)); leafNode.setChildList(new ArrayList()); tree.getChildList().add(leafNode); for(int j = 0;j<childD.get(i).size();j++){ childD.get(i).get(j).remove(0); } ArrayList<String> resultAttr = new ArrayList<String>(attribute); resultAttr.remove(0); creatTree(leafNode,childD.get(i),resultAttr); } return tree; } //遍历整个树 public void traverse(){ System.out.println(this.getId()+"---"+this.getNodeName()+": "+this.getNodeValue()); if(this.childList==null){ return; } int childNum = this.getChildList().size(); System.out.println(childNum); if(childNum==0){ return; }else{ for(int i = 0;i<childNum;i++){ TreeNode childNode = this.getChildList().get(i); childNode.traverse(); } } } //求两个节点最近的公共祖先 public TreeNode FindParent(TreeNode node1,TreeNode node2){ List <TreeNode>list1 = new ArrayList(); List <TreeNode>list2 = new ArrayList(); list1 = getAllParentNode(node1); list2 = getAllParentNode(node2); TreeNode result = new TreeNode(); boolean flag = false; for(int i = 0;i<list1.size();i++){ TreeNode temp1 = (TreeNode) list1.get(i); int temp1Id = temp1.getId(); for(int j = 0;j<list2.size();j++){ TreeNode temp2 = (TreeNode) list2.get(j); int temp2Id = temp2.getId(); if(temp1Id==temp2Id){ flag = true; break; } } if(flag){ result = temp1; break; } } return result; } //得到所有父节点 public List getAllParentNode(TreeNode node){ List<TreeNode> Parent = new ArrayList(); if(node.getId()==0){ return Parent; }else{ int ParentId = node.getParentId(); TreeNode P = findTreeNodeById(ParentId); Parent.add(P); Parent.addAll(getAllParentNode(P)); return Parent; } } //得到所有子节点 public List getAllChildNode(TreeNode node){ List<TreeNode> Child = new ArrayList(); if(node.childList==null){ return Child; }else{ int childNum = node.getChildList().size(); for(int i = 0;i<childNum;i++){ Child.add(node.getChildList().get(i)); Child.addAll(getAllChildNode(node.getChildList().get(i))); } return Child; } } //求树的深度 public int DepthOfTree(TreeNode node){ int childNum1 = node.getChildList().size(); System.out.println(node.id+": "+childNum1+" ,"+node.getNodeName()); if(childNum1==0){ return 1; }else{ int childNum = node.getChildList().size(); int []Count = new int [childNum]; for(int i = 0;i<childNum;i++){ Count[i] = DepthOfTree(node.getChildList().get(i))+1; } int maxCount = Count[0]; for(int i = 1;i<Count.length;i++){ if(maxCount<Count[i]){ maxCount = Count[i]; } } return maxCount; } } public double getNodeValue() { return nodeValue; } public void setNodeValue(double nodeValue) { this.nodeValue = nodeValue; } public int getId() { return id; } public void setId(int id) { this.id = id; } public List<TreeNode> getChildList() { return childList; } public void setChildList(List<TreeNode> childList) { this.childList = childList; } public String getNodeName() { return nodeName; } public void setNodeName(String nodeName) { this.nodeName = nodeName; } public int getParentId() { return ParentId; } public void setParentId(int parentId) { ParentId = parentId; } }
原文地址:https://www.cnblogs.com/yunerlalala/p/6129402.html