QHEAP1

  • + 0 comments

    C#

    public class MinHeap {
            private HashSet<int> _lookup = new HashSet<int>();
            private List<int> _heap = new List<int>();
            private bool HasParent(int i) => (i - 1) / 2 < _heap.Count();
            private int GetParentIndex(int i) => (i - 1) / 2;
            private int GetParent(int i) => _heap[GetParentIndex(i)];
            
            private bool HasLeftChild(int i) => 2 * i + 1 < _heap.Count();
            private int GetLeftChildIndex(int i) => 2 * i + 1;
            private int GetLeftChild(int i) => _heap[GetLeftChildIndex(i)];
            
            private bool HasRightChild(int i) => 2 * i + 2 < _heap.Count();
            private int GetRightChildIndex(int i) => 2 * i + 2;
            private int GetRightChild(int i) => _heap[GetRightChildIndex(i)];
            
            public void Print() { 
                Delete();
                Console.WriteLine(_heap[0]);
            }
            
            public void Add(int num) {
                _heap.Add(num);
                _lookup.Add(num);
                int lindex = _heap.Count() - 1;
                while (lindex >= 0 && HasParent(lindex) && num < GetParent(lindex)) {
                    // bubbling up...
                    var temp = _heap[lindex];
                    _heap[lindex] = _heap[GetParentIndex(lindex)];
                    _heap[GetParentIndex(lindex)] = temp;
                    lindex = GetParentIndex(lindex);
                }
            }
            
            public void Delete(int num) =>
                _lookup.Remove(num);
            
            private void Delete() {
                while (_heap.Count() > 0 && _lookup.Count() > 0 && !_lookup.Contains(_heap[0])) {
                    int index = 0;
                    _heap[index] = _heap[_heap.Count()-1];
                    _heap.RemoveAt(_heap.Count()-1);
                    if (index == _heap.Count()-1) {
                        return;
                    }
                    
                    int num = _heap[index];
                    int temp = 0;
                    while ((HasLeftChild(index) && GetLeftChild(index) < num) 
                        || (HasRightChild(index) && GetRightChild(index) < num)) {
                        if (HasLeftChild(index) && num > GetLeftChild(index)
                            && (!HasRightChild(index) || GetLeftChild(index) < GetRightChild(index))) {
                            temp = _heap[GetLeftChildIndex(index)];
                            _heap[GetLeftChildIndex(index)] = _heap[index];
                            _heap[index] = temp;
                            index = GetLeftChildIndex(index);
                        } else if (HasRightChild(index) && num > GetRightChild(index)
                            && GetLeftChild(index) > GetRightChild(index)) {
                            temp = _heap[GetRightChildIndex(index)];
                            _heap[GetRightChildIndex(index)] = _heap[index];
                            _heap[index] = temp;
                            index = GetRightChildIndex(index);
                        }
                    }
                }
            }
        }