Converting a Number from Decimal to Binary
• Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2)
• Rightmost bit of x
– Remainder of x after division by two
• Recursive algorithm pseudocode
– Binary(num) denotes binary representation of num
Converting a Number from Decimal to Binary (cont’d.)
• Recursive function implementing algorithm
Converting a Number from Decimal to Binary (cont’d.)
FIGURE 6-10 Execution of decToBin(13, 2)
Quicksort: Array-Based Lists
• Uses the divide-and-conquer technique to sort a list
– List partitioned into two sublists
• Two sublists sorted and combined into one list
• Combined list then sorted using quicksort (recursion)
• Trivial to combine sorted lowerSublist and upperSublist
• All sorting work done in partitioning the list
Quicksort: Array-Based Lists (cont’d.)
• Pivot divides list into two sublists
– lowerSublist: elements smaller than pivot – upperSublist: elements greater than pivot
• Choosing the pivot
– lowerSublist and upperSublist nearly equal
FIGURE 10-21 List before the partition
FIGURE 10-22 List after the partition
Quicksort: Array-Based Lists (cont’d.)
• Partition algorithm
– Determine pivot; swap pivot with first list element
• Suppose index smallIndex points to last element smaller than pivot. smallIndex initialized to first list element
– For the remaining list elements (starting at second element):
If current element smaller than pivot
• Increment smallIndex
• Swap current element with array element pointed to by smallIndex
– Swap first element (pivot) with array element pointed to by smallIndex
Quicksort: Array-Based Lists (cont’d.)
• Function partition
– Passes starting and ending list indices – Swaps certain elements of the list
Quicksort: Array-Based Lists (cont’d.)
• Given starting and ending list indices
– Function recQuickSort implements the recursive version of quicksort
• Function quickSort calls recQuickSort
Analysis: Quicksort
TABLE 10-2 Analysis of quicksort for a list of length n
Mergesort: Linked List-Based Lists
• Quicksort
– Average-case behavior: O(nlog2n) – Worst-case behavior: O(n2)
• Mergesort behavior: always O(nlog
2n)
– Uses divide-and-conquer technique to sort a list – Partitions list into two sublists
• Sorts sublists
• Combines sorted sublists into one sorted list
• Difference between mergesort and quicksort
– How list is partitioned
Mergesort: Linked List-Based Lists (cont’d.)
FIGURE 10-32 Mergesort algorithm
Mergesort: Linked List-Based Lists (cont’d.)
• Most sorting work done in merging sorted sublists
• General algorithm for mergesort
Divide
• To divide list into two sublists
– Need to find middle node
– Use two pointers: middle and current
• Advance middle by one node, advance current by one node
• current becomes NULL; middle points to last node
– Divide list into two sublists
• Using the link of middle: assign pointer to node following middle
• Set link of middle to NULL
• See function divideList on page 561
FIGURE 10-33 Unsorted linked list
FIGURE 10-36 List after dividing it into two lists FIGURE 10-35 middle after traversing the list
FIGURE 10-34 middle and current before traversing the list
Merge
• Once sublists sorted
– Next step: merge the sorted sublists
• Merge process
– Compare elements of the sublists
– Adjust references of nodes with smaller info
• See code on page 564 and 565
Analysis: Mergesort
• Maximum number of comparisons made by mergesort:
O(n log
2n)
• If W(n) denotes number of key comparisons
– Worst case to sort L: W(n) = O(n log2n)
• Let A(n) denote number of key comparisons in the average case
– Average number of comparisons for mergesort – If n is a power of 2
• A(n) = n log2n - 1.25n = O(n log2n)
Heapsort: Array-Based Lists
• Overcomes quicksort worst case
• Heap: list in which each element contains a key
– Key in the element at position k in the list
• At least as large as the key in the element at position 2k + 1 (if it exists) and 2k + 2 (if it exists)
• C++ array index starts at zero
– Element at position k
• k + 1th element of the list
FIGURE 10-41 A heap
Heapsort: Array-Based Lists (cont’d.)
• Data given in Figure 10-41
– Can be viewed in a complete binary tree
• Heapsort
– First step: convert list into a heap
• Called buildHeap
– After converting the array into a heap
• Sorting phase begins
FIGURE 10-42 Complete binary tree
Build Heap
Build Heap (cont’d.)
• Function heapify
– Restores the heap in a subtree
– Implements the buildHeap function
• Converts list into a heap
Build Heap (cont’d.)
Build Heap (cont’d.)
• The heapsort algorithm
FIGURE 10-48 Heapsort
Analysis: Heapsort
• Given L a list of n elements where n > 0
• Worst case
– Number of key comparisons to sort L
• 2nlog2n + O(n)
– Number of item assignments to sort L
• nlog2n + O(n)
• Average number of comparisons to sort L
– O(nlog2n)
• Heapsort takes twice as long as quicksort
– Avoids the slight possibility of poor performance
Data Structures Using C++ 2E
Chapter 11
Binary Trees and B-Trees
Objectives
• Learn about binary trees
• Explore various binary tree traversal algorithms
• Learn how to organize data in a binary search tree
• Discover how to insert and delete items in a binary search
tree
Objectives (cont’d.)
• Explore nonrecursive binary tree traversal algorithms
• Learn about AVL (height-balanced) trees
• Learn about B-trees
Binary Trees
• Definition: a binary tree, T, is either empty or such that
– T has a special node called the root node
– T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively
– LT and RT are binary trees
• Can be shown pictorially
– Parent, left child, right child
• Node represented as a circle
– Circle labeled by the node
Binary Trees (cont’d.)
• Root node drawn at the top
– Left child of the root node (if any)
• Drawn below and to the left of the root node
– Right child of the root node (if any)
• Drawn below and to the right of the root node
• Directed edge (directed branch): arrow
FIGURE 11-1 Binary tree
Binary Trees (cont’d.)
FIGURE 11-2 Binary tree with one, two, or three nodes
FIGURE 11-3 Various binary trees with three nodes
Binary Trees (cont’d.)
• Every node in a binary tree
– Has at most two children
• struct defining node of a binary tree
– For each node
• The data stored in info
• A pointer to the left child stored in llink
• A pointer to the right child stored in rlink
Binary Trees (cont’d.)
• Pointer to root node is stored outside the binary tree
– In pointer variable called the root
• Of type binaryTreeNode
FIGURE 11-4 Binary tree
Binary Trees (cont’d.)
• Level of a node
– Number of branches on the path
• Height of a binary tree
– Number of nodes on the longest path from the root to a leaf – See code on page 604
Copy Tree
• Shallow copy of the data
– Obtained when value of the pointer of the root node used to make a copy of a binary tree
• Identical copy of a binary tree
– Need to create as many nodes as there are in the binary tree to be copied
– Nodes must appear in the same order as in the original binary tree
• Function copyTree
– Makes a copy of a given binary tree – See code on pages 604-605
Binary Tree Traversal
• Must start with the root, and then
– Visit the node first or – Visit the subtrees first
• Three different traversals
– Inorder – Preorder – Postorder
Binary Tree Traversal (cont’d.)
• Inorder traversal
– Traverse the left subtree – Visit the node
– Traverse the right subtree
• Preorder traversal
– Visit the node
– Traverse the left subtree – Traverse the right subtree
Binary Tree Traversal (cont’d.)
• Postorder traversal
– Traverse the left subtree – Traverse the right subtree – Visit the node
• Each traversal algorithm: recursive
• Listing of nodes
– Inorder sequence – Preorder sequence – Postorder sequence
Binary Tree Traversal (cont’d.)
FIGURE 11-5 Binary tree for an inorder traversal
Binary Tree Traversal (cont’d.)
• Functions to implement the preorder and postorder
traversals
Implementing Binary Trees (cont’d.)
• Default constructor
– Initializes binary tree to an empty state – See code on page 612
• Other functions for binary trees
– See code on pages 612-613
• Functions: copyTree, destroy, destroyTree
– See code on page 614
• Copy constructor, destructor, and overloaded assignment operator
– See code on page 615
Binary Search Trees
• Data in each node
– Larger than the data in its left child – Smaller than the data in its right child
FIGURE 11-7 Binary search tree FIGURE 11-6 Arbitrary binary tree
Binary Search Trees (cont’d.)
• class bSearchTreeType
– Illustrates basic operations to implement a binary search tree
– See code on page 618
• Function search
• Function insert
• Function delete
Binary Search Tree: Analysis
• Worst case
– T: linear
– Successful case
• Algorithm makes (n + 1) / 2 key comparisons (average)
– Unsuccessful case: makes n comparisons
FIGURE 11-10 Linear binary trees
Binary Search Tree: Analysis (cont’d.)
• Average-case behavior
– Successful case
• Search would end at a node
• n items exist, providing n! possible orderings of the keys
– Number of comparisons required to determine whether x is in T
• One more than the number of comparisons required to insert x in T
– Number of comparisons required to insert x in T
• Same as number of comparisons made in the unsuccessful search reflecting that x is not in T
Binary Search Tree: Analysis (cont’d.)
Binary Search Tree: Analysis (cont’d.)
• Theorem: let T be a binary search tree with n nodes, where n> 0
– The average number of nodes visited in a search of T is approximately 1.39log2n =O(log2n)
– The number of key comparisons is approximately 2.77 log2n
= O(log2n)