Podcast Ch22a Title: Array-based Binary Trees

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Chapter 10: Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 22 Heaps.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time.
Binary Trees Michael R. Wick
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
ADSA: Heaps/ Advanced Data Structures and Algorithms Objectives – –implement heaps (a kind of array-based complete binary tree), heap sort,
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 16 Binary.
ADSA: IntroAlgs/ Advanced Data Structures and Algorithms Objective –introduce algorithm design using basic searching and sorting, and remind.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
ADSA: Generics/ Advanced Data Structures and Algorithms Objective –to describe basic forms of generic classes, interfaces, and methods for searching.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Introduction to Analysis of Algorithms CS342 S2004.
Data Structures Using C++1 Chapter 10 Sorting Algorithms.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Data Structures Using Java1 Chapter 9 Sorting Algorithms.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Partially Ordered Data ,Heap,Binary Heap
Podcast Ch23e Title: Implementing Huffman Compression
Podcast Ch17b Title: Iterative Tree Traversal
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Podcast Ch26a Title: Representing Graphs
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Podcast Ch17d Title: Drawing a Binary Tree
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Trees.
Section 8.1 Trees.
Chapter 21 Heaps and Priority Queues
Podcast Ch17a Title: Expression Trees
Chapter 10: Non-linear Data Structures
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Trees Definitions Implementation Traversals K-ary Trees
Podcast Ch18b Title: STree Class
Heap Sort.
Data Structures for Java William H. Ford William R. Topp
Podcast Ch22c Title: Deleting from a Heap
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Podcast Ch23b Title: BitArray Implementation
Podcast Ch18c Title: BST delete operation
CHAPTER 11: Priority Queues and Heaps
Lecture 9 CS2013.
Algorithms: Design and Analysis
Podcast Ch22b Title: Inserting into a Heap
1 Lecture 10 CS2013.
Podcast Ch18a Title: Overview of Binary Search Trees
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21a Title: Hash Functions
Podcast Ch21f Title: HashSet Class
Heaps By JJ Shepherd.
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Sorting and Searching -- Introduction
Chapter 9 The Priority Queue ADT
Podcast Ch21b Title: Collision Resolution
Heapsort.
Podcast Ch23a Title: Bit Arrays
Presentation transcript:

Podcast Ch22a Title: Array-based Binary Trees Description: Representing binary trees in an array; the Comparator interface; sorting using a comparator Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Array-Based Binary Trees A complete binary tree of depth d contains all possible nodes through level d-1 and nodes at level d occupy the leftmost positions in the tree. An array arr can be viewed as a complete binary tree. The root is arr[0], the first-level children are arr[1] and arr[2], the second-level children are arr[3], arr[4], arr[5], arr[6], and so forth.

Array-Based Binary Trees (continued) Integer[] arr = {5, 1, 3, 9, 6, 2, 4, 7, 0, 8};

Array-Based Binary Trees (concluded) For element arr[i] in an n-element array‑based binary tree, the following relationships hold: Left child of arr[i] is arr[2*i + 1] undefined if 2*i + 1  n Right child of arr[i] is arr[2*i + 2] undefined if 2*i + 2  n Parent of arr[i] is arr[(i-1)/2] undefined if i = 0

Which tree corresponds to the array arr? int arr[] = {3, 12, 15, 4, 67, 6, 55, 9};

The children of node arr[i] are found at nodes (i) arr[i+1], arr[i+2] (ii) arr[2*i + 1], arr[2*i + 2] (iii) arr[2*i], arr[2*i+1] (iv) arr[2*(i + 1)], arr[2*(i + 2)] The parent of node arr[i] is found at node (i) arr[i/2] (ii) arr[i % 2] (iii) arr[(i-1)/2] (iv) arr[(i + 1)/2] If an array contains 50 elements, the leaf nodes begin at index _________________

Assume that arr is an array-based tree with 95 members. What is the parent of arr[35]? (i) 22 (ii) 17 (iii) 18 (iv) 10 (b) What is the left child of arr[14]? (i) 15 (ii) 28 (iii) 29 (iv) 30 (c) What is the index of the first leaf node? (i) 46 (ii) 47 (iii) 50 (iv) 94 (d) What is the depth of the tree? (i) 5 (ii) 7 (iii) 6 (iv) 3 (e) How many leaf nodes does the tree have? (i) 47 (ii) 50 (iii) 49 (iv) 48

Complete the implementation of the recursive method preorderHeap() which outputs an array-based tree using a preorder scan? (int i is the root index) public static <T> void preorderHeap(T[] arr, int i){ if (i < arr.length){ System.out.print(arr[i] + " "); preorderHeap (arr, 2 * i + 1); preorderHeap (arr, 2 * i + 2); }

The Comparator Interface The ordering determined when a class implements the Comparable interface is termed the class's natural ordering. The Comparator interface defines objects containing a comparison function which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method to allow precise control over the sort order and can also be used to control the order of certain data structures such as a heap.

The Comparator Interface (continued)

The Comparator Interface (continued) A Comparator object implements the compare() method and acts like a function that compares two objects. Comparator objects are external to the classes that use them and must access data in those classes using public methods.

General Comparison Objects If a class implements the Comparable interface, we can develop generic Comparator classes that compare two objects for either the < or the > relation. The classes implement compare() by using the compareTo() ordering between objects.

The < Comparator import java.util.Comparator; // constructs the < Comparator public class Less<T> implements Comparator<T> { public int compare(T x, T y) return ((Comparable<T>)x).compareTo(y); }

The > Comparator // constructs the > Comparator public class Greater<T> implements Comparator<T> { public int compare(T x, T y) return -((Comparable<T>)x).compareTo(y); }

Complete the implementation of the string comparator class ShorterString which compares the length of the two strings. public class ShorterString implements Comparator<String> { public int compare(String strA, String strB) if (strA.length() < strB.length()) // less than case return -1; else if (strA.length() == strB.length()) // equals case return 0; else return 1; // greater than case }

Now write a version of the comparator class ShorterString that only has a single line return command. public class ShorterString implements Comparator<String> { public int compare(String strA, String strB) return strA.length() - strB.length(); }

Generalized Array Sorting By using a Less or Greater parameter in a sorting method, the method can place an array in ascending or descending order.

Selection Sort with Comparator // new version adds a Comparator parameter public static <T> void selectionSort( T[] arr, Comparator<? super T> comp) { // index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length; T temp; // pass has the range 0 to n-2 for (pass = 0; pass < n-1; pass++) // scan the sublist starting at index pass smallIndex = pass;

Selection Sort with Comparator (concluded) // j traverses the sublist arr[pass+1] // to arr[n-1] for (j = pass+1; j < n; j++) // if smaller element found, assign // smallIndex to that position if (comp.compare(arr[j], arr[smallIndex]) < 0) smallIndex = j; // swap the next smallest // element into arr[pass] temp = arr[pass]; arr[pass] = arr[smallIndex]; arr[smallIndex] = temp; }