public void Add(T item) { if (head._right == null) { var newNode = new Skiplist <T> .Node <T>(item); head._right = newNode; newNode._left = head; newNode._right = tail; tail._left = newNode; } else { var newNode = AddNodeToBottomLevel(item); int curHeight = 1; while (WeakRandom.XorShift128(2) % 4 == 0) { curHeight++; if (curHeight > maxHeight) //need to add a new head/tail level { AddLevel(); } AddUpperNode(item, newNode); } } }
private Node <T> AddNodeToBottomLevel(T item) { var bookmarkNode = this.FindEntry(item); var newNode = new Skiplist <T> .Node <T>(item) { _right = bookmarkNode._right, _left = bookmarkNode }; bookmarkNode._right = newNode; newNode._right._left = newNode; return(newNode); }
private void AddLevel() { var newHead = new Skiplist <T> .Node <T>(); var newTail = new Skiplist <T> .Node <T>(); head._up = newHead; tail._up = newTail; newHead._right = newTail; newTail._left = newHead; newHead._down = head; newTail._down = tail; head = newHead; tail = newTail; maxHeight++; }