Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

การแทรกข้อมูล vào Max Heap ในโครงสร้างข้อมูล


ต้นไม้เริ่มต้น

การแทรกข้อมูล vào Max Heap ในโครงสร้างข้อมูล

อัลกอริทึมการแทรก (Insertion Algorithm)

insert(heap, n, item) − Begin
    if heap is full, then exit
    else
        n := n + 1
        for i := n, i > 1, set i := i / 2 in each iteration, do
            if item ≤ heap[i/2], then break
            heap[i] = heap[i/2]
        done
    end if
    heap[i] := item
End

ตัวอย่าง

สมมติต้องการแทรก 30 เข้า Heap:

การแทรกข้อมูล vào Max Heap ในโครงสร้างข้อมูล