111,098
社区成员




//节点数据//
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;//
}
}
//册除对象的子节点
public bool DeleteChildrens()
{
if (this.current.Childrens == null)
{
return false;
}
else
{
//不知这样是否会有内存泄漏,因为他的字节点可能来用字节点!
this.current.Childrens == null;
GC.Collect();
return true;
}
}
///改
public bool AlterCurrentData(object data)
{
//正常情况下,不会执行下面的代码
if (this.current == null)
{
return false;
}
//
this.current.Data = null;
GC.Collect();
this.current.Data = data;
return true;
}
///查
//查当前指针指向的数据
public object GetCurrentData()
{
if (this.current == null)
{
return null;
}
return this.current.Data;
}
//查当前字节点的数据
public List<object> GetChildrenData()
{
if (this.current.Childrens == null || this.current.Childrens.Count <= 0)
{
return null;
}
else
{
List<object> data = new List<object>();
foreach (TreeContainerNode node in this.current.Childrens)
{
data.Add(node.Data);
}
return data;
}
}
}
//删除当前节点
public bool DeleteCurrentNode()
{
//没有父节点 当前节点就是根节点
//不能删除跟节点
//要删除,可以销毁本对象
if (this.current.Parent == null)
{
return false;
}
else
{
//删除并移到父节点
//记录
TreeContainerNode nodeRecord = this.current.Parent;
//删除 --把 父节点 对此对象的引用 打断
for (int i = 0;i<this.current.Parent.Childrens.Count;i++)// eachChildrenNode in this.current.Parent.Childrens)
{
//他的父节点的子节点集合中的一个 是不是引用了我要删除的对象?
if (Object.ReferenceEquals(this.current.Parent.Childrens[i], this.current))
{
this.current.Parent.Childrens.RemoveAt(i);
}
}
//this.current = null;
//GC.Collect();//回收!
//还原
this.current = nodeRecord;//
}
}
还有一个问题!
现在我有一个引用, 他指向[对象A],
对于[对象A]来说,可能在其它地方, 还有其它引用指向他,
问题,是我不知道在哪里还有引用指向[对象A],
现在,我想删除对象A ,并回收他的内存, 因为暂时用不到,等用到了再重新加载!
(假如 此对象特大,例如他存放的是图片类型的数据--一些图片变换后的数据)
请问:
这该怎么办?