11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

170 Chap. 5 Binary Treesin the case when this node has two children, the depth of the node with smallestvalue in its right subtree. Thus, in the worst case, the cost for any one of theseoper<strong>at</strong>ions is the depth of the deepest node in the tree. This is why it is desirable tokeep BSTs balanced, th<strong>at</strong> is, with least possible height. If a binary tree is balanced,then the height for a tree of n nodes is approxim<strong>at</strong>ely log n. However, if the treeis completely unbalanced, for example in the shape of a linked list, then the heightfor a tree with n nodes can be as gre<strong>at</strong> as n. Thus, a balanced BST will in theaverage case have oper<strong>at</strong>ions costing Θ(log n), while a badly unbalanced BST canhave oper<strong>at</strong>ions in the worst case costing Θ(n). Consider the situ<strong>at</strong>ion where weconstruct a BST of n nodes by inserting records one <strong>at</strong> a time. If we are fortun<strong>at</strong>eto have them arrive in an order th<strong>at</strong> results in a balanced tree (a “r<strong>and</strong>om” order islikely to be good enough for this purpose), then each insertion will cost on averageΘ(log n), for a total cost of Θ(n log n). However, if the records are inserted inorder of increasing value, then the resulting tree will be a chain of height n. Thecost of insertion in this case will be ∑ ni=1 i = Θ(n2 ).Traversing a BST costs Θ(n) regardless of the shape of the tree. Each node isvisited exactly once, <strong>and</strong> each child pointer is followed exactly once.Below is an example traversal, named printhelp. It performs an inordertraversal on the BST to print the node values in ascending order.priv<strong>at</strong>e void printhelp(BSTNode rt) {if (rt == null) return;printhelp(rt.left());printVisit(rt.element());printhelp(rt.right());}While the BST is simple to implement <strong>and</strong> efficient when the tree is balanced,the possibility of its being unbalanced is a serious liability. There are techniquesfor organizing a BST to guarantee good performance. Two examples are the AVLtree <strong>and</strong> the splay tree of Section 13.2. Other search trees are guaranteed to remainbalanced, such as the 2-3 tree of Section 10.4.5.5 Heaps <strong>and</strong> Priority QueuesThere are many situ<strong>at</strong>ions, both in real life <strong>and</strong> in computing applic<strong>at</strong>ions, wherewe wish to choose the next “most important” from a collection of people, tasks,or objects. For example, doctors in a hospital emergency room often choose tosee next the “most critical” p<strong>at</strong>ient r<strong>at</strong>her than the one who arrived first. Whenscheduling programs for execution in a multitasking oper<strong>at</strong>ing system, <strong>at</strong> any givenmoment there might be several programs (usually called jobs) ready to run. Thenext job selected is the one with the highest priority. Priority is indic<strong>at</strong>ed by aparticular value associ<strong>at</strong>ed with the job (<strong>and</strong> might change while the job remains inthe wait list).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!