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

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;
	}
}
时间: 2024-08-01 22:47:32

多叉树的建立以及其他的一些操作的相关文章

【转】linux建立软链接

实例:ln -s /home/gamestat    /gamestat linux下的软链接类似于windows下的快捷方式 ln -s a b 中的 a 就是源文件,b是链接文件名,其作用是当进入b目录,实际上是链接进入了a目录 如上面的示例,当我们执行命令   cd /gamestat/的时候  实际上是进入了 /home/gamestat/ 值得注意的是执行命令的时候,应该是a目录已经建立,目录b没有建立.我最开始操作的是也把b目录给建立了,结果就不对了 删除软链接:    rm -rf

Linux下建立软链接

实例:ln -s /home/gamestat    /gamestat Linux下的软链接类似于windows下的快捷方式 ln -s a b 中的 a 就是源文件,b是链接文件名,其作用是当进入b目录,实际上是链接进入了a目录 如上面的示例,当我们执行命令   cd /gamestat/的时候  实际上是进入了 /home/gamestat/ 值得注意的是执行命令的时候,应该是a目录已经建立,目录b没有建立.我最开始操作的是也把b目录给建立了,结果就不对了. 删除软链接: rm -rf  

sysfs文件系统的建立【转】

http://blog.csdn.net/dndxhej/article/details/7434615 对sysfs和设备模型有了解的都会知道sysfs实际是为了将设备模型导出到用户空间的一个内存文件系统. 设备模型的关键结构体kobject会组成设备模型的树形结构,而sysfs的关键结构体sysfs_dirent也是类似的树形的结构,vfs中的dentry同样是类似的树形结构. sysfs目录文件的创建都是由设备模型的上层构件(bus device driver class)在注册的时候调用

SQL Server 不同数据间建立链接服务器进行连接查询

    在平时查询以及导数据时,经常会遇到需要使用两个数据库里数据的情况,这时就会用到在两个服务器之间建立一个链接,进行操作,脚本语句如下: 举例:例如你在测试服务器上想要查询业务库里的数据信息,此脚本就需要在测试服务器上执行,输入业务服务器的IP地址.业务服务器的账户.密码,然后执行语句即可:反之,如果你需要将测试数据库的数据导入正式库内,就需要在正式库内建立可以连接到测试库的链接. --创建链接服务器 exec sp_addlinkedserver 'ITSV' , '' , 'SQLOLE

配置采用手工方式建立IPSec隧道

微信公众号:网络民工组网需求 如图1所示,RouterA为企业分支网关,RouterB为企业总部网关,分支与总部通过公网建立通信.分支子网为10.1.1.0/24,总部子网为10.1.2.0/24. 企业希望对分支子网与总部子网之间相互访问的流量进行安全保护.分支与总部通过公网建立通信,可以在分支网关与总部网关之间建立一个IPSec隧道来实施安全保护.由于维护网关较少,可以考虑采用手工方式建立IPSec隧道. 图1 配置采用手工方式建立IPSec隧道组网图 操作步骤 RouterA的配置 # s

【数据结构】6. 查找

目录 6.1 查找的基本概念 6.2 顺序查找和折半查找 6.2.1 顺序查找 1. 一般线性表的顺序查找 2. 有序表的顺序查找 6.2.2 折半查找 6.2.3 分块查找 6.3 B 树和 B+ 树 6.3.1 B 树及其基本操作 1. B 树的高度(磁盘存取次数) 2. B 树的查找 3. B 树的接入 4. B 树的删除 6.3.2 B+ 树基本概念 6.4 散列( Hash) 表 6.4.1 散列表的基本概念 6.4.2 散列函数的构造方法 6.4.3 处理冲突的方法 1. 开放定址法

web新特性 之 WebSocket

详情参见:你真的了解WebSocket吗?     WebSocket系列教程   HTML5新特性之WebSocket WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实现了浏览器与服务器全双工(full-duplex)通信.其本质是保持TCP连接,在浏览器和服务端通过Socket进行通信. 服务端与客户端的连接不断开,实现全双工的操作.及服务端或是客户端都会给对方发送消息. WebSocke

Nodejs + MongoDb

一.搭建开发环境 进入 http://nodejs.org 下载开发环境 http://Expressjs.com 下载安装Express  npm install -g express 继续安装ejs:npm install ejs 如果要想运行Node.js程序,则现在只能够使用“node app.js”,而这样的运行方式,如果在app.js文件修改之后往往需要重新启动才可以加载新的内容,这对于开发是非常不方便的, 为此,可以使用一个supervisor组件包,它可以动态的加载修改之后的开发

[转]表变量和临时表的比较

本文转自;http://www.cnblogs.com/CareySon/archive/2012/06/11/TableVariableAndTempTable.html 关于表变量是什么(和表变量不是什么),以及和临时表的比较让很多人非常困惑.虽然网上已经有了很多关于它们的文章,但我并没有发现一篇比较全面的.在本篇文章中,我们将探索表变量和临时表是什么(以及不是什么),然后我们通过使用临时表和表变量对其解密. 表变量 表变量在SQL Server 2000中首次被引入,那么,什么是表变量呢?