/// <summary> /// return LinkedList of generic class in pre order /// </summary> /// <param name="iterate"></param> /// <returns></returns> public LinkedList <T> PreOrder(Iterate iterate = null) { LinkedList <T> ret = new LinkedList <T>(); if (this.Root == null) { return(ret); } Stack s = new Stack(this.Count / 2 + 1); // cannot be more than half of the nodes in the stack Node <T> act = this.Root; s.Push(act); int count = 0; while (count < this.Count) // go through all items in the tree { act = (Node <T>)s.Pop(); // get last added to print ret.AddLast(act.Data); iterate?.Invoke(act.Data); count++; if (act.Right != null) { s.Push(act.Right); } if (act.Left != null) { s.Push(act.Left); } } return(ret); }
/// <summary> /// returns LinkedList of generic class in order /// </summary> /// <param name="iterate"></param> /// <returns></returns> public LinkedList <T> InOrder(Iterate iterate = null) { LinkedList <T> ret = new LinkedList <T>(); if (this.Root == null) { return(ret); } Stack s = new Stack(this.Count / 2); Node <T> act = Root; // start at root int count = 0; while (count < this.Count) // go through all items in the tree { this.FindLeftLeaf(act, s); // save path to from act to the left leaf into stack act = (Node <T>)s.Pop(); // get last added to print ret.AddLast(act.Data); iterate?.Invoke(act.Data); count++; act = act.Right; // don't forget about right side } return(ret); }
/// <summary> /// return LinkedList of generic class post order /// </summary> /// <param name="iterate"></param> /// <returns></returns> public LinkedList <T> PostOrder(Iterate iterate = null) { LinkedList <T> ret = new LinkedList <T>(); if (this.Root == null) { return(ret); } Stack s = new Stack(this.Count); Node <T> act = this.Root; int count = 0; while (this.Count > count) { this.FindLeftLeaf(act, s, 2); if (s.Count == 0) { break; } act = (Node <T>)s.Pop(); if (s.Count > 0 && (Node <T>)s.Peek() == act) { act = act.Right; } else { ret.AddLast(act.Data); iterate?.Invoke(act.Data); act = null; } } return(ret); }
protected virtual void OnIterate() { // it is not usually necessary to override this; generally alternative functionality can be put in a second function which calls this Debug.WriteLine("Iterate at " + Environment.TickCount); IsAtScanStart = false; Iterate?.Invoke(this, m_Direction); if (IterateCanRepeat && ScanTime > 0) { m_Timer.Start(ScanTime, OnIterate, Timings.ScanTime); } }
public LinkedList <T> GetAll <T>(T Record, Iterate <T> iterate = null) where T : IRecord <T> { LinkedList <T> ret = new LinkedList <T>(); int position = 0; byte[] arr = new byte[Record.GetSize()]; while (position < this.LastAddress) { Record = Record.Clone(); br.BaseStream.Seek(position, SeekOrigin.Begin); br.Read(arr, 0, Record.GetSize()); Record.FromByteArray(arr); ret.AddLast(Record); iterate?.Invoke(Record); position += Record.GetSize(); } return(ret); }