priorityqueues

priorityqueues - Computer Science 211 Data Structures Mount...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science 211 Data Structures Mount Holyoke College Fall 2009 Topic Notes: Priority Queues Before we discuss priority queues, recall the following tree terminology and properties: A full binary tree of height h has all leaves on level h . A complete binary tree of height h is obtained from a full binary tree of height h with 0 or more (but not all) of the rightmost leaves at level h removed. We say T is balanced if it has the minimum possible height for its number of nodes. Lemma : If T is a binary tree, then at level k , T has 2 k nodes. Theorem : If T has height h then n = num nodes in T 2 h +1 - 1 . Equivalently, if T has n nodes, then n - 1 h log( n + 1) - 1 . Array representations of trees Our representation of the tree: 1 8 10 7 9 5 4 2 3 6 actually looks like this: = reference to unique EMPTY tree E E E E E E E = null reference E E E E E 8 4 9 2 5 7 10 1 3 6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 211 Data Structures Fall 2009 That’s a lot of extra references to parents and children, and to empty nodes. So to store 10 actual data values, we need space for 40 references plus the 4 that make up the empty tree instances. The following array contains exactly the same information: 14 6 4 9 2 5 7 10 1 3 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 The array, data[0. .n-1] , holds the values to be stored in the tree. It does not contain explicit references to the left or right subtrees or to parents. Instead the children of node i are stored in positions 2 * i + 1 and 2 * i + 2 , and therefore the parent of a node j , may be found at ( j - 1) / 2 This lets us save space for links, but it is possible that there is a significant waste of storage: Storing a tree of height n requires an array of length 2 ( n +1) - 1 (!), even if the tree only has O ( n ) elements. This makes this representation very expensive if you have a long, skinny tree. However, it is very efficient for holding full or complete trees. For our example, we need 15 references to hold a tree of 10 items, compared to 40 for the fully constructed BinaryTree . Heaps and Priority Queues From here, we will look at some ways that trees are used in other structures. First, we’ll consider a structure that seems somewhat like the ordered structures and somewhat like a queue: A priority queue is a structure where the contents are comparable elements and the elements with “small” values are removed before elements with “larger” values. Waiting at a restaurant can be a priority queue – you get in line but people who are regular cus- tomers or who give a tip to the host or hostess may move ahead of you in line. Same idea when airports are backed up. Planes will get in line for their turn on the runway, but scheduling concerns or fuel issues or whatever else may cause ground control to give a plane which “got in line” later a higher priority and move them up. An operating system may be scheduling processes that are competing to use the CPU in a multi-
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 02/15/2010.

Page1 / 10

priorityqueues - Computer Science 211 Data Structures Mount...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online