日期:2014-05-17  浏览次数:20690 次

C# 写的一颗树,(不知有没有内存卸漏?)
请大侠指出下面的代码有没有问题!
   主要是删除节点时内存会不会有卸漏?


//节点数据// 
public class TreeContainerNode
{

object data;
TreeContainerNode parent;
List<TreeContainerNode> childrens;

public object Data
{
get { return data; }
set { data = value; }
}
public TreeContainerNode Parent
{
get { return parent; }
set { parent = value; }
}
public List<TreeContainerNode> Childrens
{
get { return childrens; }
set { childrens = value; }
}

public TreeContainerNode(object data, TreeContainerNode parentNode, List<TreeContainerNode> children)
{
this.data = data;
this.parent = parentNode;
this.childrens = children;
}


}
//树型容器,树型 数据结构
public class TreeContainer
{
//
TreeContainerNode headNode;

public TreeContainerNode HeadNode
{
get { return headNode; }
set { headNode = value; }
}
//指针//
private TreeContainerNode current;//当前节点
//构造 器
public TreeContainer(object data)
{
this.headNode = new TreeContainerNode(data, null, null);
this.current = this.headNode;
}
//移到上一节点
public bool MoveToParent()
{
if (current.Parent == null)
{
return false;
}
else
{
this.current = this.current.Parent;//移动
return true;
}
}
//移到子节点
///添
public bool MoveToChildren(int index)
{
if (this.current.Childrens == null)//没有子节点
{
return false;
}
if (this.current.Childrens.Count <= index)//没有这么多子节点
{
return false;
}
else
{
this.current = this.current.Childrens[index];
}
}
//添加子节点
public bool AddChildrenNode(object data)
{
//设为头节点
if (this.headNode == null)
{
this.headNode = new TreeContainerNode(data, null, null);
return false;
}
else
{
if (this.current.Childrens == null)//表示没有子节点
{
//创建子节点的容器
this.current.Childrens = new List<TreeContainerNode>();
//添加子节点数据
this.current.Childrens.Add(new TreeContainerNode(data, this.current, null));
}
else//表示 有字节点
{
this.current.Childrens.Add(new TreeContainerNode(data, this.current, null));
}
return true;
}
}
///删
//删除当前节点
public bool DeleteCurrentNode()
{
//没有父节点 当前节点就是根节点
//不能删除跟节点
//要删除,可以销毁本对象
if (this.current.Parent == null)
{
return false;
}
else
{
//删除并移到父节点
//记录
TreeContainerNode nodeRecord = this.current.Parent;
//删除
this.current = null;
GC.Collect();//回收!
//还原
this.current = nodeRecord;//
}
}