Trees Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.

Slides:



Advertisements
Similar presentations
Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Merge Sort1 Part-G1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.
CS 171: Introduction to Computer Science II
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
Trees1 Recursion Recursion is a concept of defining a method that makes a call to itself.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Chapter 7: Trees Objectives:
© 2004 Goodrich, Tamassia Trees1 Lecture 04 Trees Topics Trees Binary Trees Binary Search trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
© 2004 Goodrich, Tamassia Trees1 this is a tree. © 2004 Goodrich, Tamassia Trees2 What is a Tree? In computer science, a tree is an abstract model of.
Trees COMP53 Oct 31, What is a Tree? A tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation.
Trees1 Part-C Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
COSC2007 Data Structures II
Trees.
Make Money Fast! Stock Fraud Apply shortcuts Bank Robbery Just For Laughs Trees This one is cool eITnotes.com For more notes and topics visit:
Trees CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Chapter 7:Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures.
Trees. What is a Tree? In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent- child relation.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Lecture11: Tree I Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2013 Goodrich, Tamassia, Goldwasser.
Trees and Traversal CS 244 On a Tree Fallen Across the Road
CH 7. TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
Quicksort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Towers of Hanoi Move n (4) disks from pole A to pole B such that a larger disk is never put on a smaller disk A BC ABC.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.
1 COMP9024: Data Structures and Algorithms Week Five: Trees Hui Wu Session 1, 2015
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Merge Sort 1/12/2018 9:44 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Trees 5/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 5/10/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 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees
Trees 二○一八年八月二十六日 Part-C Trees Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
More on Merge Sort CS 244 This presentation is not given in class
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Week nine-ten: Trees Trees.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees
Trees Palestine Gaza West Bank 48 Stolen Land Trees Trees
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Copyright © Aiman Hanna All rights reserved
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
CS210- Lecture 9 June 20, 2005 Announcements
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
Presentation transcript:

Trees Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content derived/taken from: On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in front of us is not bar Our passage to our journey's end for good, But just to ask us who we think we are Insisting always on our own way so. She likes to halt us in our runner tracks, And make us get down in a foot of snow Debating what to do without an ax. And yet she knows obstruction is in vain: We will not be put off the final goal We have it hidden in us to attain, Not though we have to seize earth by the pole And, tired of aimless circling in one place, Steer straight off after something into space.

Previously Sorting Merge Sort (chapter 11.1) Quicksort (chapter 11.2)

Today MergeSort’s Tree Representation Introduction to Trees General and Binary Review

Merge Sort Merge sort is based on the divide-and-conquer paradigm. It consists of three steps: – Divide: partition input sequence S into two sequences S 1 and S 2 of about n  2 elements each – Recur: recursively sort S 1 and S 2 – Conquer: merge S 1 and S 2 into a unique sorted sequence Algorithm mergeSort(S, C) Input sequence S, comparator C Output sequence S sorted according to C if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Review

The Mystery C R P G B H Q A Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Somehow this:

The Mystery Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Becomes this: C  CR  R C R P G B H Q A  A B C G H P Q R B H  Q A  A B H Q C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A  A Q B  BH  HQ  Q B  H  B H A  A

The Mystery Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) But how? C  CR  R C R P G B H Q A  A B C G H P Q R B H  Q A  A B H Q C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A  A Q B  BH  HQ  Q B  H  B H A  A

Class Exercise C R P G B H Q A Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Begin at the beginning Recursion Depth = RD = 0 RD = 0 What happens next ?

C R  P G B H Q A C R P G  B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 0 RD = 0 Enhanced Version What happens next ?

C R P GB H Q A C R P G B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 0 RD = 0 Enhanced Version What happens next ?

C R  P G B H Q A C  R P G C R P G B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 RD = 1 Enhanced Version What happens next ?

C R  P G B H Q A C  R P G C R P G B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 RD = 1 Enhanced Version What happens next ?

C  R CR C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 RD = 2 Enhanced Version What happens next ?

C  R CR C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 RD = 2 Enhanced Version What happens next ?

C  R C  C R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 3 return [ C ] 3 Enhanced Version What happens next ?

C  R C  C R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 RD = 2 Enhanced Version What happens next ?

C  R C  CR  R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 3 return [ R ] 3 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R RD = 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R return [ C R ] RD = 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R RD = 1 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C RP  G PG RD = 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C RP  G PG RD = 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 3 C  R  C RP  G P  P G return [ P ] 3 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C RP  G P  P G RD = 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A P G C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 3 C  R  C RP  G P  PG  G return [ G ] 3 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P RD = 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A C R  P G Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P return [ G P ] RD = 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  ? Details of Merge Already did 2 trivial merges But this is the first that requires some “real thought” RD = 1 Enhanced Version What happens next ? S1S1 S2S2

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  ? Details of Merge Already did 2 trivial merges But this is the first that requires some “real thought” Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A  B S  empty sequence while  A.isEmpty()   B.isEmpty() if A.first() < B.first() S.insertLast(A.removeFirst()) else S.insertLast(B.removeFirst()) while  A.isEmpty() S.insertLast(A.removeFirst()) while  B.isEmpty() S.insertLast(B.removeFirst()) return S RD = 1 Enhanced Version What happens next ? A B

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  Details of Merge Already did 2 trivial merges But this is the first that requires some “real thought” Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A  B S  empty sequence while  A.isEmpty()   B.isEmpty() if A.first() < B.first() S.insertLast(A.removeFirst()) else S.insertLast(B.removeFirst()) while  A.isEmpty() S.insertLast(A.removeFirst()) while  B.isEmpty() S.insertLast(B.removeFirst()) return S RD = 1 Enhanced Version What happens next ? A B S

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C Details of Merge Already did 2 trivial merges But this is the first that requires some “real thought” Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A  B S  empty sequence while  A.isEmpty()   B.isEmpty() if A.first() < B.first() S.insertLast(A.removeFirst()) else S.insertLast(B.removeFirst()) while  A.isEmpty() S.insertLast(A.removeFirst()) while  B.isEmpty() S.insertLast(B.removeFirst()) return S RD = 1 Enhanced Version What happens next ? A B

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G Details of Merge Already did 2 trivial merges But this is the first that requires some “real thought” Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A  B S  empty sequence while  A.isEmpty()   B.isEmpty() if A.first() < B.first() S.insertLast(A.removeFirst()) else S.insertLast(B.removeFirst()) while  A.isEmpty() S.insertLast(A.removeFirst()) while  B.isEmpty() S.insertLast(B.removeFirst()) return S RD = 1 Enhanced Version What happens next ? A B

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G P Details of Merge Already did 2 trivial merges But this is the first that requires some “real thought” Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A  B S  empty sequence while  A.isEmpty()   B.isEmpty() if A.first() < B.first() S.insertLast(A.removeFirst()) else S.insertLast(B.removeFirst()) while  A.isEmpty() S.insertLast(A.removeFirst()) while  B.isEmpty() S.insertLast(B.removeFirst()) return S RD = 1 Enhanced Version What happens next ? A B

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G P Details of Merge Already did 2 trivial merges But this is the first that requires some “real thought” Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A  B S  empty sequence while  A.isEmpty()   B.isEmpty() if A.first() < B.first() S.insertLast(A.removeFirst()) else S.insertLast(B.removeFirst()) while  A.isEmpty() S.insertLast(A.removeFirst()) while  B.isEmpty() S.insertLast(B.removeFirst()) return S RD = 1 Enhanced Version What happens next ? A B

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Details of Merge Already did 2 trivial merges But this is the first that requires some “real thought” Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A  B S  empty sequence while  A.isEmpty()   B.isEmpty() if A.first() < B.first() S.insertLast(A.removeFirst()) else S.insertLast(B.removeFirst()) while  A.isEmpty() S.insertLast(A.removeFirst()) while  B.isEmpty() S.insertLast(B.removeFirst()) return S RD = 1 Enhanced Version What happens next ? A B

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Details of Merge Already did 2 trivial merges But this is the first that requires some “real thought” Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A  B S  empty sequence while  A.isEmpty()   B.isEmpty() if A.first() < B.first() S.insertLast(A.removeFirst()) else S.insertLast(B.removeFirst()) while  A.isEmpty() S.insertLast(A.removeFirst()) while  B.isEmpty() S.insertLast(B.removeFirst()) return S RD = 1 Enhanced Version What happens next ? S

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G P R return [ C G P R ] RD = 1 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 0 C  R  C R P  PG  G P  G  G P C R  P G  C G P R S 1 = [ C G P R ] now call S 2 = mergeSort RD = 0 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G P R B HQ A RD = 1 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G P R B HQ A RD = 1 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P C R  P G  C G P R B  H Q A BH 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P C R  P G  C G P R B  H Q A BH 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 3 C  R  C R P  PG  G P  G  G P C R  P G  C G P R B  H Q A H B  B return [ B ] 3 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P C R  P G  C G P R B  H Q A H B  B 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 3 C  R  C R P  PG  G P  G  G P C R  P G  C G P R B  H Q A B  BH  H return [ H ] 3 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P C R  P G  C G P R B  H Q A B  BH  H 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q A B  BH  H B  H  B H return [ B H ] 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q A B  BH  H B  H  B H RD = 1 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A B  BH  H QA B  H  B H 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A B  BH  H QA B  H  B H 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 3 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A B  BH  HQ  Q A B  H  B H return [ Q ] 3 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A B  BH  HQ  Q A B  H  B H 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 3 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A B  BH  HQ  Q B  H  B H A  A return [ A ] 3 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A B  BH  HQ  Q B  H  B H A  A 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 2 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A  A Q B  BH  HQ  Q B  H  B H A  A return [ A Q ] 2 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A  A Q B  BH  HQ  Q B  H  B H A  A RD = 1 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A B H  Q A  A B H Q Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 1 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A  A Q B  BH  HQ  Q B  H  B H A  A return [ A B H Q ] RD = 1 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A  A B C G H P Q R B H  Q A  A B H Q Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 0 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A  A Q B  BH  HQ  Q B  H  B H A  A merge [ C G P R ] with [ A B H Q ] RD = 0 Enhanced Version What happens next ?

C  CR  R C R P G B H Q A  A B C G H P Q R B H  Q A  A B H Q Class Exercise Algorithm mergeSort(S, C) if S.size() > 1 { (S 1, S 2 ) := partition(S, S.size()/2) S 1 := mergeSort(S 1, C) S 2 := mergeSort(S 2, C) S := merge(S 1, S 2 ) } return(S) Recursion Depth = RD = 0 C  R  C R P  PG  G P  G  G P C R  P G  C G P R Q  A  A Q B  BH  HQ  Q B  H  B H A  A return [ A B C G H P Q R ] RD = 0 Enhanced Version And we return out

MergeSort Take-Aways The execution results of a MergeSort can be represented as a binary tree And you now know how to fill that tree out MergeSort uses a Divide and Conquer method The height of the tree is lg N, where N is the number of items being sorted How do we “know” that ? [ pause for students answer here ] =)

MergeSort Take-Aways The execution results of a MergeSort can be represented as a binary tree And you now know how to fill that tree out MergeSort uses a Divide and Conquer method The height of the tree is O(lg N), where N is the number of items being sorted How do we “know that?” lg N is the number of times N can be divided by 2 (integer-wise) Enhanced Version

Runtime of Merge Sort Knowing the height of the representative tree is O(lg N) is important why? or rather knowing lg N is the number of times N can be divided by 2 is important to understanding Merge Sort, why? [ more pausing ]

Runtime of Merge Sort Knowing the height of the representative tree is O(lg N) is important why? or rather knowing lg N is the number of times N can be divided by 2 is important to understanding Merge Sort, why? It is the “key concept” in showing/proving the runtime of Merge Sort is O(N lg N) Enhanced Version

Summary of Sorting Algorithms (so far) AlgorithmTimeNotes Selection SortO(n 2 )Slow, in-place For small data sets Insertion SortO(n 2 )Slow, in-place For small data sets Bubble SortO(n 2 )Slow, in-place For small data sets Heap SortO(nlog n)Fast, in-place For large data sets Merge SortO(nlogn)Fast, sequential data access For huge data sets Quick SortO(nlogn)Assumes key values are random and uniformly distributed Place holder for others we have not yet seen

Continuing with Trees Tree Definitions and ADT (§7.1) Tree Traversal Algorithms for General Trees (preorder and postorder) (§7.2) BinaryTrees (§7.3) Data structures for trees (§7.1.4 and §7.3.4) Traversals of Binary Trees (preorder, inorder, postorder) (§7.3.6) Euler Tours (§7.3.7) Location in the book

What is a Tree In Computer Science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation Applications: Organization charts File systems Many others Computers”R”Us SalesR&DManufacturing LaptopsDesktops US International EuropeAsiaCanada

General Tree Terminology Root: node without parent (A) Internal node: node with at least one child (A, B, C, F) Leaf (aka External node): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, great-grandparent, etc. Depth of a node: number of ancestors (root has depth 0) Height of a tree: maximum depth of any node (3) Descendant of a node: child, grandchild, great-grandchild, etc. A B D C G H E F IJ K Size: number of nodes in a tree Subtree: tree consisting of a node and its descendants Subtree More detail on depth and height follow on next slide

Definitions Related to Trees For clarity (from previous class) Depth of a node p: number of ancestors of p So the root has depth 0 Height of a node p: the length of the longest downward path to a leaf from p. So if p is a leaf height is 0 Examples: Height of A is 3, Depth of A is 0 Height if E is 0, Depth of E is 2 Height of F is 1, Depth of F is 2 Height of K is 0, Depth of K is 3 Most often we speak of the depth of nodes and the height of the tree A B D C GH E F IJ K

Exercise: Trees Answer the following questions about the tree shown on the right: What is the size of the tree to the right? What nodes are leaves? What node is the root? What nodes are internal nodes? List the ancestors of node F. List the descendants of node B. What is the depth of A? What is the height of the tree? How many leaves does a tree with exactly 1 node have? A B D C GH E F IJ K Take 5 minutes (or less) to answer the following. You will be randomly selected to share your answers shortly. Be prepared. Recall Definitions: Leaf : node without children Internal node: node with at least one child

Tree ADT We use positions to abstract nodes Generic methods: integer size() boolean isEmpty() objectIterator elements() positionIterator positions() Accessor methods: position root() position parent(p) positionIterator children(p) Query methods: boolean isInternal(p) boolean isLeaf (p) boolean isRoot(p) Update methods: swapElements(p, q) object replaceElement(p, o) Additional update methods may be defined by data structures implementing the Tree ADT Context and relation to other ADTs

 A Linked Structure for General Trees A node is represented by an object storing Element Parent node Sequence of children nodes Node objects implement the Position ADT B  ADF  C  E B D A CE F

 A Linked Structure for General Trees A node is represented by an object storing Element Parent node Sequence of children nodes Node objects implement the Position ADT B  ADF  C  E B D A CE F

 A Linked Structure for General Trees A node is represented by an object storing Element Parent node Sequence of children nodes Node objects implement the Position ADT B  ADF  C  E B D A CE F

 A Linked Structure for General Trees A node is represented by an object storing Element Parent node Sequence of children nodes Node objects implement the Position ADT B D A CE F B  ADF  C  E

Binary Tree A binary tree is a tree with the following properties: Each internal node has two children The children of a node are an ordered pair We call the children of an internal node left child and right child A BC F G D E H I

BinaryTree ADT The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods: position leftChild(p) position rightChild(p) position sibling(p) Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node Right child node Node objects implement the Position ADT B D A CE   B AD CE  We want to represent this tree This is what it would look like using a linked structure

Alternate Picture of a binary tree a bc d e ghi l f jk

Some More Definitions Balanced Tree A binary tree is balanced if every level above the lowest is “ full ” (contains 2 level nodes) Is the tree to the right balanced? a bc defg hij [ Pause for Student answer ]

Some More Definitions Balanced Tree A binary tree is balanced if every level above the lowest is “ full ” (contains 2 level nodes) Is the tree to the right balanced? a bc defg hij This is the lowest level Examine the ones above it Enhanced Version

Some More Definitions Balanced Tree A binary tree is balanced if every level above the lowest is “ full ” (contains 2 level nodes) Is the tree to the right balanced? a bc defg hij Level 2 has 4 nodes, 4 = 2 2 Enhanced Version

Some More Definitions Balanced Tree A binary tree is balanced if every level above the lowest is “ full ” (contains 2 level nodes) Is the tree to the right balanced? a bc defg hij Level 1 has 2 nodes, 2 = 2 1 Enhanced Version

Some More Definitions Balanced Tree A binary tree is balanced if every level above the lowest is “ full ” (contains 2 level nodes) Is the tree to the right balanced? a bc defg hij Level 0 has 1 node, 1 = 2 0 Enhanced Version

Some More Definitions Balanced Tree A binary tree is balanced if every level above the lowest is “ full ” (contains 2 level nodes) Is the tree to the right balanced? a bc defg hij YES – tree is Balanced Enhanced Version

Some More Definitions Balanced Tree A binary tree is balanced if every level above the lowest is “ full ” (contains 2 level nodes) Is the tree to the right balanced? a b c d e f gh ij

Some More Definitions Balanced Tree A binary tree is balanced if every level above the lowest is “ full ” (contains 2 level nodes) Is the tree to the right balanced? a b c d e f gh ij NO – tree is not Balanced 2 != 2 4 Enhanced Version

Some More Definitions Balanced Tree A binary tree is balanced if every level above the lowest is “ full ” (contains 2 level nodes) Is the tree to the right balanced?

Some More Definitions Balanced Tree A binary tree is balanced if every level above the lowest is “ full ” (contains 2 level nodes) Is the tree to the right balanced? Insufficient information to tell

Some More Definitions A Complete Binary Tree with height h, is a tree such that level i has 2 i nodes, for 0 ≤ i ≤ h-1 i.e. balanced Nodes at level h fill up left to right Is this tree complete ?

Some More Definitions A Complete Binary Tree with height h, is a tree such that level i has 2 i nodes, for 0 ≤ i ≤ h-1 i.e. balanced Nodes at level h fill up left to right Is this tree complete ? YES – tree is Complete Enhanced Version

Some More Definitions A Complete Binary Tree with height h, is a tree such that level i has 2 i nodes, for 0 ≤ i ≤ h-1 i.e. balanced Nodes at level h fill up left to right Is this tree complete ? 

Some More Definitions A Complete Binary Tree with height h, is a tree such that level i has 2 i nodes, for 0 ≤ i ≤ h-1 i.e. balanced Nodes at level h fill up left to right YES – tree is Complete Enhanced Version Is this tree complete ? 

Some More Definitions A Complete Binary Tree with height h, is a tree such that level i has 2 i nodes, for 0 ≤ i ≤ h-1 i.e. balanced Nodes at level h fill up left to right Is this tree complete ? 

Some More Definitions A Complete Binary Tree with height h, is a tree such that level i has 2 i nodes, for 0 ≤ i ≤ h-1 i.e. balanced Nodes at level h fill up left to right NO – tree is NOT complete Enhanced Version Is this tree complete ? 

Exercise: Binary Tree Are the two trees below the same? Why or why not? A B A B

Sorted binary trees A binary tree is sorted if every node in the tree is larger than (or equal to) its left descendants, and smaller than (or equal to) its right descendants Equal nodes can go either on the left or the right (but it has to be consistent)

Binary Search in a sorted array Look at array location (lo + hi)/ Searching for 5: (0+6)/2 = 3 hi = 2; (0 + 2)/2 = 1 lo = 2; (2+2)/2= Using a binary search tree Review and Re-Apply

The End of this part