/// <summary> /// Output: /// Last(source end /// First(destnation end /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> public static LinkedList <HierarchyTreeNode <T> > Path(HierarchyTreeNode <T> source, HierarchyTreeNode <T> destination) { ///collect ancients ,find intersection LinkedList <HierarchyTreeNode <T> > _sourceAncientList = new LinkedList <HierarchyTreeNode <T> >(); LinkedList <HierarchyTreeNode <T> > _destinationAncientList = new LinkedList <HierarchyTreeNode <T> >(); CollectAncients(source, _sourceAncientList); CollectAncients(destination, _destinationAncientList); //find out common ancient LinkedListNode <HierarchyTreeNode <T> > _commonAncientNodeOnSource = _sourceAncientList.Last; while (!_destinationAncientList.Contains(_commonAncientNodeOnSource.Value)) { _sourceAncientList.RemoveLast(); //trim out source list as well _commonAncientNodeOnSource = _sourceAncientList.Last; } //attach destionation list to source list (merge LinkedListNode <HierarchyTreeNode <T> > _nextAttachingNode = _destinationAncientList.Find(_commonAncientNodeOnSource.Value).Previous; while (_nextAttachingNode != null) { _sourceAncientList.AddLast(_nextAttachingNode.Value); // value copy _nextAttachingNode = _nextAttachingNode.Previous; //iterate } return(_sourceAncientList); }
/// <summary> /// Head(First : younger /// End(Last : older /// </summary> /// <param name="start"></param> /// <param name="list"></param> static internal void CollectAncients(HierarchyTreeNode <T> start, LinkedList <HierarchyTreeNode <T> > list) { if (start != null) { list.AddLast(start); CollectAncients(start.__parent, list); } else { //meet null return; } }
/// <summary> /// Member function version /// First : source /// Last : destination /// </summary> /// <param name="destination"></param> /// <returns></returns> public LinkedList <T> Path(HierarchyTreeNode <T> destination) { var __capsuleList = Path(this, destination); var result = new LinkedList <T>(); var __node = __capsuleList.First; while (__node != null) { result.AddLast(__node.Value.Value); __node = __node.Next; } return(result); }
void AddChild(T data) { var __node = new HierarchyTreeNode <T>(data); __node.Parent = this; }