Converting a Number from Decimal to Binary



Similar documents
Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Search Trees CMPSC 122

Binary Heap Algorithms

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Binary Search Trees (BST)

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

6 March Array Implementation of Binary Trees

Ordered Lists and Binary Trees

Data Structure [Question Bank]

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

TREE BASIC TERMINOLOGIES

Heaps & Priority Queues in the C++ STL 2-3 Trees

Exam study sheet for CS2711. List of topics

Data Structures and Algorithms

From Last Time: Remove (Delete) Operation

Binary Trees and Huffman Encoding Binary Search Trees

Analysis of Algorithms I: Binary Search Trees

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

How To Create A Tree From A Tree In Runtime (For A Tree)

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Chapter 14 The Binary Search Tree

DATA STRUCTURES USING C

CS711008Z Algorithm Design and Analysis

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Data Structures Fibonacci Heaps, Amortized Analysis

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structures. Level 6 C Module Descriptor

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structure with C

Cpt S 223. School of EECS, WSU

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Algorithms and Data Structures

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Binary Search Trees 3/20/14

Sample Questions Csci 1112 A. Bellaachia

Binary Heaps. CSE 373 Data Structures

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

1/1 7/4 2/2 12/7 10/30 12/25

6. Standard Algorithms

Linked List as an ADT (cont d.)

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

10CS35: Data Structures Using C

Full and Complete Binary Trees

Unordered Linked Lists

Algorithms Chapter 12 Binary Search Trees

Data Structures and Algorithms(5)

CSE 326: Data Structures B-Trees and B+ Trees

APP INVENTOR. Test Review

Algorithms. Margaret M. Fleck. 18 October 2010

PES Institute of Technology-BSC QUESTION BANK

Symbol Tables. Introduction

Big Data and Scripting. Part 4: Memory Hierarchies

Data Structures and Data Manipulation

Algorithms and Data Structures

The ADT Binary Search Tree

Persistent Binary Search Trees

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

OPTIMAL BINARY SEARCH TREES

A COMPARATIVE STUDY OF LINKED LIST SORTING ALGORITHMS

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Review of Hashing: Integer Keys

THIS CHAPTER studies several important methods for sorting lists, both contiguous

AP Computer Science AB Syllabus 1

Analysis of Algorithms I: Optimal Binary Search Trees

Data Structures and Algorithms Written Examination

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Lecture Notes on Binary Search Trees

Chapter 13: Query Processing. Basic Steps in Query Processing

Why Use Binary Trees?

The Goldberg Rao Algorithm for the Maximum Flow Problem

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

Fundamental Algorithms

External Memory Geometric Data Structures

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Complexity of Union-Split-Find Problems. Katherine Jane Lai

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Transcription:

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