• No results found

Converting a Number from Decimal to Binary

N/A
N/A
Protected

Academic year: 2022

Share "Converting a Number from Decimal to Binary"

Copied!
46
0
0

Loading.... (view fulltext now)

Full text

(1)

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

(2)

Converting a Number from Decimal to Binary (cont’d.)

• Recursive function implementing algorithm

(3)

Converting a Number from Decimal to Binary (cont’d.)

FIGURE 6-10 Execution of decToBin(13, 2)

(4)

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

(5)

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

(6)

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

(7)

Quicksort: Array-Based Lists (cont’d.)

• Function partition

– Passes starting and ending list indices – Swaps certain elements of the list

(8)

Quicksort: Array-Based Lists (cont’d.)

• Given starting and ending list indices

– Function recQuickSort implements the recursive version of quicksort

• Function quickSort calls recQuickSort

(9)

Analysis: Quicksort

TABLE 10-2 Analysis of quicksort for a list of length n

(10)

Mergesort: Linked List-Based Lists

• Quicksort

– Average-case behavior: O(nlog2n) – Worst-case behavior: O(n2)

• 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

(11)

Mergesort: Linked List-Based Lists (cont’d.)

FIGURE 10-32 Mergesort algorithm

(12)

Mergesort: Linked List-Based Lists (cont’d.)

• Most sorting work done in merging sorted sublists

• General algorithm for mergesort

(13)

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

(14)

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

(15)

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

(16)

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 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)

(17)

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

(18)

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

(19)

Build Heap

(20)

Build Heap (cont’d.)

• Function heapify

– Restores the heap in a subtree

– Implements the buildHeap function

• Converts list into a heap

(21)
(22)

Build Heap (cont’d.)

(23)

Build Heap (cont’d.)

• The heapsort algorithm

FIGURE 10-48 Heapsort

(24)

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

(25)

Data Structures Using C++ 2E

Chapter 11

Binary Trees and B-Trees

(26)

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

(27)

Objectives (cont’d.)

• Explore nonrecursive binary tree traversal algorithms

• Learn about AVL (height-balanced) trees

• Learn about B-trees

(28)

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

(29)

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

(30)

Binary Trees (cont’d.)

FIGURE 11-2 Binary tree with one, two, or three nodes

FIGURE 11-3 Various binary trees with three nodes

(31)

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

(32)

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

(33)

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

(34)

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

(35)

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

(36)

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

(37)

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

(38)

Binary Tree Traversal (cont’d.)

FIGURE 11-5 Binary tree for an inorder traversal

(39)

Binary Tree Traversal (cont’d.)

• Functions to implement the preorder and postorder

traversals

(40)

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

(41)

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

(42)

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

(43)

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

(44)

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

(45)

Binary Search Tree: Analysis (cont’d.)

(46)

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)

References

Related documents

Unit3: Tree: binary tree, Binary Tree representations: node and implicit array representation, internal and external nodes, binary tree traversals, threaded binary

This implementation uses a binary node to represent the root of the binary search tree and implements the methods for insertion, deletion and search recursively.. You find part of

Write a function that is given a pointer to the root of a valid binary search tree with unique elements and prints out a list of all the odd numbers stored in the binary search

Given a binary search tree, write a C function called isTreeSkewed(), whose prototype is shown below, that takes a binary search tree as its first parameter, the number of top

This essay describes the views of Philippines livestock sector stakeholders concerning the events and issues associated with the rapid rise in hog and poultry production, based

Classic problems: Binary Tree Preorder Traversal , Binary Tree Inorder Traversal, Binary Tree Pos- torder Traversal, Word Ladder, Validate Binary Search Tree, Flatten Binary Tree

Given a non-empty binary search tree (an ordered binary tree), return the minimum data value found in that tree.. Note that it is not necessary to search the

Algorithm bstSearch(binarySearchTree, desiredObject) // Searches a binary search tree for a given object. // Returns true if the object