private void Traverse(SortedListNode <T> currNode) { if (currNode == null) { return; } lock (this.locker) { if (currNode.HasLeftNode) { this.Traverse(currNode.LeftNode); } foreach (var data in currNode.Datas) { this.nodes[this.index] = data; this.index++; } if (currNode.HasRightNode) { this.Traverse(currNode.RightNode); } } }
public SortedListNode <T> FindNode(SortedListNode <T> head, T data, ref int level) { // todo: use itiration vs recursion if (head == null) { level++; return(new SortedListNode <T>(data)); } var comparison = head.Data.CompareTo(data); if (comparison < 0) { // go right if (head.HasRightNode) { level++; return(FindNode(head.RightNode, data, ref level)); } } else if (comparison > 0) { // go left if (head.HasLeftNode) { level++; return(FindNode(head.LeftNode, data, ref level)); } } return(head); }
public void AddNode(SortedListNode <T> parent, T data, ref int level) { // todo: rename local head var var head = this.FindNode(parent, data, ref level); var comparison = head.Data.CompareTo(data); var newNode = new SortedListNode <T>(data); if (comparison > 0) { head.LeftNode = newNode; newNode.ParentNode = head; } else if (comparison < 0) { head.RightNode = newNode; newNode.ParentNode = head; } else { head.Data = data; } }
public void Add(T data) { lock (this.locker) { if (this.head == null) { this.head = new SortedListNode <T>(data); this.count++; } else { this.InternalAdd(data); this.count++; if (this.count == 2) { var temp = this.head.RightNode; temp.ParentNode = null; temp.LeftNode = this.head; this.head.ParentNode = temp; this.head.RightNode = null; this.head = temp; } if (this.count > 2) { var comparison = this.head.Data.CompareTo(data); if (comparison > 0) { if (this.head.HasLeftNode) { var temp = this.head.LeftNode; this.head.ParentNode = temp; temp.ParentNode = null; var tempPointer = temp; while (tempPointer.HasRightNode) { tempPointer = tempPointer.RightNode; } this.head.LeftNode = null; tempPointer.RightNode = this.head; this.head = temp; } } else if (comparison < 0) { if (this.head.HasRightNode) { var temp = this.head.RightNode; this.head.ParentNode = temp; temp.ParentNode = null; var tempPointer = temp; while (tempPointer.HasLeftNode) { tempPointer = tempPointer.LeftNode; } this.head.RightNode = null; tempPointer.LeftNode = this.head; this.head = temp; } } } } } }
public void AddNode(SortedListNode <T> parent, T data, ref int level) { }
public SortedListNode <T> FindNode(SortedListNode <T> head, T data, ref int level) { throw new NotImplementedException(); }
public PreOrderTraversal(SortedListNode <T> head, int count) { this.head = head; this.nodes = new T[count]; }
public SortedListNode(T data) { this.internalData.Add(data); this.leftNode = null; this.rightNode = null; }