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 Data Structures Using C++ 2E 1
Converting a Number from Decimal to Binary (cont d.) Recursive function implementing algorithm Data Structures Using C++ 2E 2
Converting a Number from Decimal to Binary (cont d.) FIGURE 6-10 Execution of dectobin(13, 2) Data Structures Using C++ 2E 3
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 Data Structures Using C++ 2E 4
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 Data Structures Using C++ 2E 5
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 Data Structures Using C++ 2E 6
Quicksort: Array-Based Lists (cont d.) Function partition Passes starting and ending list indices Swaps certain elements of the list Data Structures Using C++ 2E 7
Quicksort: Array-Based Lists (cont d.) Given starting and ending list indices Function recquicksort implements the recursive version of quicksort Function quicksort calls recquicksort Data Structures Using C++ 2E 8
Analysis: Quicksort TABLE 10-2 Analysis of quicksort for a list of length n Data Structures Using C++ 2E 9
Mergesort: Linked List-Based Lists Quicksort Average-case behavior: O(nlog 2 n) Worst-case behavior: O(n 2 ) Mergesort behavior: always O(nlog 2 n) 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 Data Structures Using C++ 2E 10
Mergesort: Linked List-Based Lists (cont d.) FIGURE 10-32 Mergesort algorithm Data Structures Using C++ 2E 11
Mergesort: Linked List-Based Lists (cont d.) Most sorting work done in merging sorted sublists General algorithm for mergesort Data Structures Using C++ 2E 12
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 Data Structures Using C++ 2E 13
FIGURE 10-33 Unsorted linked list FIGURE 10-34 middle and current before traversing the list FIGURE 10-35 middle after traversing the list FIGURE 10-36 List after dividing it into two lists Data Structures Using C++ 2E 14
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 Data Structures Using C++ 2E 15
Analysis: Mergesort Maximum number of comparisons made by mergesort: O(n log 2 n) If W(n) denotes number of key comparisons Worst case to sort L: W(n) = O(n log 2 n) 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 log 2 n - 1.25n = O(n log 2 n) Data Structures Using C++ 2E 16
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 Data Structures Using C++ 2E 17
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 corresponding to the list in Figure 10-41 Data Structures Using C++ 2E 18
Build Heap Data Structures Using C++ 2E 19
Build Heap (cont d.) Function heapify Restores the heap in a subtree Implements the buildheap function Converts list into a heap Data Structures Using C++ 2E 20
Data Structures Using C++ 2E 21
Build Heap (cont d.) Data Structures Using C++ 2E 22
Build Heap (cont d.) The heapsort algorithm FIGURE 10-48 Heapsort Data Structures Using C++ 2E 23
Analysis: Heapsort Given L a list of n elements where n > 0 Worst case Number of key comparisons to sort L 2nlog 2 n + O(n) Number of item assignments to sort L nlog 2 n + O(n) Average number of comparisons to sort L O(nlog 2 n) Heapsort takes twice as long as quicksort Avoids the slight possibility of poor performance Data Structures Using C++ 2E 24
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 Data Structures Using C++ 2E 26
Objectives (cont d.) Explore nonrecursive binary tree traversal algorithms Learn about AVL (height-balanced) trees Learn about B-trees Data Structures Using C++ 2E 27
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, L T and R T, called the left subtree and right subtree of T, respectively L T and R T are binary trees Can be shown pictorially Parent, left child, right child Node represented as a circle Circle labeled by the node Data Structures Using C++ 2E 28
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 Data Structures Using C++ 2E 29
Binary Trees (cont d.) FIGURE 11-2 Binary tree with one, two, or three nodes FIGURE 11-3 Various binary trees with three nodes Data Structures Using C++ 2E 30
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 Data Structures Using C++ 2E 31
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 Data Structures Using C++ 2E 32
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 Data Structures Using C++ 2E 33
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 Data Structures Using C++ 2E 34
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 Data Structures Using C++ 2E 35
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 Data Structures Using C++ 2E 36
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 Data Structures Using C++ 2E 37
Binary Tree Traversal (cont d.) FIGURE 11-5 Binary tree for an inorder traversal Data Structures Using C++ 2E 38
Binary Tree Traversal (cont d.) Functions to implement the preorder and postorder traversals Data Structures Using C++ 2E 39
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 Data Structures Using C++ 2E 40
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-6 Arbitrary binary tree FIGURE 11-7 Binary search tree Data Structures Using C++ 2E 41
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 Data Structures Using C++ 2E 42
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 Data Structures Using C++ 2E 43
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 Data Structures Using C++ 2E 44
Binary Search Tree: Analysis (cont d.) Data Structures Using C++ 2E 45
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.39log 2 n =O(log 2 n) The number of key comparisons is approximately 2.77 log 2 n = O(log 2 n) Data Structures Using C++ 2E 46