/// <summary> /// 创建并添加一个节点 /// </summary> /// <param name="parent">父节点</param> /// <param name="name">节点名</param> /// <returns>新节点</returns> public node addNode(node parent, string name) { int layerNum = parent.readLayerNum() + 1; //获得这个节点的层数 node node = new node(name); //创建节点 node.setLayerNum(layerNum); //设置层数 node.setParent(parent); //添加父节点 parent.addChildNode(node); //向父节点添加 addNode2Layer(node); //向树表添加 return(node); }
/// <summary> /// 在树表中添加一个节点 /// </summary> /// <param name="node">节点</param> /// <param name="layerNum">层数</param> private void addNode2Layer(node node) { int layerNum = node.readLayerNum(); if (treeList.Count <= layerNum) { //第一次到达该层 treeList.Add(new List <node>()); //新建一个层 treeList[layerNum].Add(node); //向层中添加节点 } else { //已经有该层 treeList[layerNum].Add(node); } }
/// <summary> /// 从树表中删除节点 /// </summary> /// <param name="node">节点</param> /// <param name="layerNum">层数</param> public void deleteNodeInTreeList(node node) { int layerNum = node.readLayerNum(); treeList[layerNum].Remove(node); }