////////////////////////
//Tree
///////////////////////
using System;
using System.Collections;namespace SampleTree
{
public class TreeNode
{
#region The TreeNodeCollection class.
public class TreeNodeCollection:ICollection
{
private TreeNode _owner=null;
private ArrayList _innerList=new ArrayList(); public TreeNodeCollection(TreeNode owner)
{
this._owner=owner;
}
public TreeNode this[int index]
{
get{return (TreeNode)this._innerList[index];}
set
{
if(value ==null)
throw new ArgumentNullException();
(this._innerList[index] as TreeNode)._parent=null;;
value._parent=this._owner;
this._innerList[index]=value;
}
}
public bool IsSynchronized
{
get{ return this._innerList.IsSynchronized;}
}
public int Count
{
get{ return this._innerList.Count;}
}
public void CopyTo(Array array, int index)
{
this._innerList.CopyTo(array,index);
}
public object SyncRoot
{
get
{
return this._innerList.SyncRoot;
}
}
public IEnumerator GetEnumerator()
{
return this._innerList.GetEnumerator();
} public int Add(TreeNode value)
{
if(value ==null)
throw new ArgumentNullException();
value._parent=this._owner;
return this._innerList.Add(value);
} public void Insert(int index, TreeNode value)
{
if(value ==null)
throw new ArgumentNullException();
value._parent=this._owner;
this._innerList.IndexOf(value,index);
} public void RemoveAt(int index)
{
try
{
this[index]._parent=null;
this._innerList.RemoveAt(index);
}
catch
{
}
}
public int IndexOf(TreeNode value)
{
return this._innerList.IndexOf(value);
}
public void Clear()
{
for(int i=0;i<this.Count;i++)
{
this[i]._parent=null;
}
this._innerList.Clear();
}
public bool Contains(TreeNode value)
{
return this._innerList.Contains(value);
}
}
#endregion

private TreeNode _parent=null;
private string _value=null;
private TreeNodeCollection _childNodes=null; public TreeNode(string value)
{
this._value=value;
}
public TreeNode ParentNode
{
get{return this._parent;}
}
public string Value
{
get{return this._value;}
}
public TreeNodeCollection ChildNodes
{
get
{
if(this._childNodes !=null) return this._childNodes;
this._childNodes=new TreeNodeCollection(this);
return this._childNodes;
}
}
public bool IsRoot
{
get{return this.ParentNode==null;}
}
public bool IsLeaf
{
get
{
return this._childNodes==null || this._childNodes.Count==0;
}
}
public string Location
{
get
{
string location=string.Empty;
TreeNode treeNode=this;
while(treeNode.ParentNode!=null)
{
treeNode=treeNode.ParentNode;
location=treeNode.Value+@"|"+location;
}
return this.Location;
}
}
}
public class Tree
{
private TreeNode.TreeNodeCollection  _nodes=new TreeNode.TreeNodeCollection(null); public TreeNode.TreeNodeCollection Nodes
{
get{return this._nodes;}
}
public TreeNode SearchNode(string location)
{
string[] loc=location.Split(new char[]{'|'});
if(loc.Length==0) return null;
TreeNode treeNode=null;
for(int i=0;i<this.Nodes.Count;i++)
{
if(this.Nodes[i].Value==loc[0])
{
treeNode=this.Nodes[i]; break;
}
}
int c=1;
while(treeNode!=null && c<loc.Length)
{
for(int i=0;i<treeNode.ChildNodes.Count;i++)
{
if(treeNode.ChildNodes[i].Value==loc[c])
{
treeNode=treeNode.ChildNodes[i];
c++;
}
}
treeNode=null;
}
return treeNode;
}
}
}