Beruflich Dokumente
Kultur Dokumente
1
i) if the tree is empty ii) while new item is not at the root and new item
(1) return null (target is not found) is smaller than its parent
ii) else if the target matches the root node's data (1) swap the new item with its parent, moving
(1) return the data stored at the root node the new item up the heap
iii) else if the target is less than the root node's c) Removing an item from a heap
data i) Remove the item in the root node by replacing it
(1) return the result of searching the left with the last item in the heap (LIH)
subtree of the root ii) While item LIH has children and item LIH is
iv) else larger than either of its children
(1) return the result of searching the right (1) Swap item LIH with its smaller child, moving
subtree of the root LIH down the heap
i) Full binary tree: a binary tree where all nodes have d) Because a heap is a complete binary tree, it is
either 2 children or 0 children efficiently implemented using an array, rather than a
j) Perfect binary tree: is a full binary tree of height n linked data structure
with exactly 2n -1 nodes e) Finding the value in the heap from the array in which
it is stored.
i) For a node at position p
(1) L child position = 2p+1
Figure 2: Example of a perfect binary tree: n=3 and (2) R child position = 2p+2
2n-1 = 7 ii) For a child to find its parent (position c)
k) Complete binary tree: is a perfect binary tree (1) Position of parent = (c-1)/2
through level n-1 with some extra lead nodes at level f) The insertion and removal are O(log n)
n (the tree height), all toward the left 6) Priority Queue: a data structure in which only the
highest-priority item is accessible
a) In a priority queue, the smallest item always is
removed first
Figure 3: example of a complete binary tree i) A priority queue implements the characteristics
i) Tree Traversal: determining the nodes of a tree of a heap, and therefore yields that the insertion
and their relationship by walking through the and removal of a priority queue is O(log n)
tree in a prescribed order and visiting the nodes
as they are encountered
(1) Three types of traversals:
(a) Inorder: traverse TL, visit root node,
traverse TL
(b) Presorder: visit root node, Traverse TL,
traverse TR
(c) Postorder: Traverse TL, traverse TR, visit
root node