Computer Science CS50 Lecture Notes - Priority Queue, Binary Tree, Symbol Table

74 views12 pages

Document Summary

Empty or node with links to left and right binary trees. Height of complete tree with n nodes is lg n . Array representation of a heap-ordered complete binary tree. Largest key is a[1], which is root of binary tree. Can use array indices to move through tree. Parent of node at k is at k/2. Children of node at k are at 2k and 2k+1. https://cs. ericyy. me/algorithms-1/week-4/index. html. Add node at end, then swim it up. 9 private void swim(int k) { while (k > 1 && less(k/2, k)) { exch(k, k/2); k = k/2; public void insert(key x) { pq[++n] = x; swim(n); Exchange root with node at end, then sink it down. 16 private void sink(int k) { while(2*k <= n) { int j = 2*k; if(j < n && less(j, j+1)) j++; if(!less(k, j)) break; exch(k, j); k = j; public key delmax() { Key max = pq[1]; exch(1, n-1); sink(1); pq[n+1] = null; return max; https://cs. ericyy. me/algorithms-1/week-4/index. html.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions