lecture-15

# Add them one by one into a binary search tree then do

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: move should delete the ﬁrst occurrence of the key –  Only need to consider the case where the node to remove has two children –  But consider both sub-cases: (a) when the inorder successor has a child, and (b) when it does not CPSC 223  ­ ­ Fall 2010 15 CPSC 223  ­ ­ Fall 2010 16 Treesort 8 10/21/10 Binary Search Trees: Treesort Binary search trees can be used to sort a list of items Given a list of items … –  Add them one by one into a Binary Search Tree –  Then do an inorder tree traversal (print nodes as visited) •  Questions about this approach –  Is this an efﬁcient way to sort a list? –  What is the time complexity (worst case, best case)? •  Worst case is O(n2) •  Best case is when tree is balanced … O(nlog n) •  What does balanced mean for sorting? CPSC 223  ­ ­ Fall 2010 17 Comparison of Sorting Algorithms Best Case Average Case Worst Case SelecLon Sort O(n2) O(n2) O(n2) Bubble Sort O(n) O(n2) O(n2) InserLon Sort O(n) O(n2) O(n2) Mergesort O(nlogn) O(nlogn) O(nlogn) Quicksort O(nlogn) O(nlogn) O(n2) Treesort O(nlogn) O(nlogn) O(n2) Heapsort What does this imply about the average case? CPSC 223  ­ ­ Fall 2010 18 9 10/21/10 Heaps CPSC 223  ­ ­ Fall 2010 19 Heaps •  A heap is another ADT •  In the text … –  Heaps are primarily introduced as a way to implement priority queues –  Also used for heapsort •  We will primarily discuss heaps for heapsort •  But they are also a nice segue into balanced trees … CPSC 223  ­ ­ Fall 2010 20 10 10/21/10 The Heap ADT •  A heap is very similar to a binary search tree –  In fact, we are only going to talk about binary heaps … –  There are a numb...
View Full Document

## This document was uploaded on 03/18/2014 for the course CPSC 223 at Gonzaga.

Ask a homework question - tutors are online