Binary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

Similar documents
protected BinaryNode root; } 02/17/04 Lecture 11 1

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

Giri Narasimhan. COT 5993: Introduction to Algorithms. ECS 389; Phone: x3748

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Data Structures in Java

CS350: Data Structures Tree Traversal

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

Final Examination. Algorithms & Data Structures II ( )

CMSC 341. Binary Search Trees CMSC 341 BST

Priority Queues. 04/10/03 Lecture 22 1

Topic 17 Fast Sorting

CIS 121. Binary Search Trees

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

COT 6405: Analysis of Algorithms. Giri Narasimhan. ECS 389; Phone: x3748

COMP : Trees. COMP20012 Trees 219

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

DATA STRUCTURES AND ALGORITHMS

CmpSci 187: Programming with Data Structures Spring 2015

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CE 221 Data Structures and Algorithms

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

CMSC 341 Lecture 10 Binary Search Trees

Implementations II 1

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Generic BST Interface

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

COMS 3137 Class Notes

Priority Queue. 03/09/04 Lecture 17 1

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Second Exam SAMPLE Computer Programming 338 Dr. St. John Lehman College City University of New York Tuesday, 5 April 2011

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Sorting. Task Description. Selection Sort. Should we worry about speed?

CS350: Data Structures AA Trees

Binary Search Trees. See Section 11.1 of the text.

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CS350: Data Structures Binary Search Trees

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Heaps. Heaps. A heap is a complete binary tree.

Motivation of Sorting

Sorting and Searching Algorithms

Principles of Computer Science

Priority Queues (Heaps)

Examples

Implementations II 1

void insert( Type const & ) void push_front( Type const & )

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Priority Queues (Heaps)

CS 350 : Data Structures Binary Search Trees

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CSE 214 Computer Science II Heaps and Priority Queues

10/21/ Linear Search The linearsearch Algorithm Binary Search The binarysearch Algorithm

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Lecture 23: Binary Search Trees

COP 3530, Course Outcomes

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

CMSC 341. Binary Heaps. Priority Queues

CS 151. Binary Search Trees. Wednesday, October 10, 12

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Unit 10: Sorting/Searching/Recursion

ECE242 Data Structures and Algorithms Fall 2008

a graph is a data structure made up of nodes in graph theory the links are normally called edges

CS 151 Name Final Exam December 17, 2014

for (int outercounter = nums.length - 1; outercounter > 0 && swappedthatturn; outercounter --

QuickSort

Computer Science 1 Ah

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Priority queues. Priority queues. Priority queue operations

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

IMPLEMENTING BINARY TREES

The Binary Search Tree ADT

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

Trees. (Trees) Data Structures and Programming Spring / 28

Priority Queues Heaps Heapsort

CS 310 Advanced Data Structures and Algorithms

CS302 - Data Structures using C++

Lecture 5: Sorting Part A

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

AVL Trees. See Section 19.4of the text, p

We can use a max-heap to sort data.

Binary Trees. Quicksort. SorKng process with quicksort. University of Massachuse5s Amherst ECE 242 Data Structures and Algorithms Lecture 18

Better sorting algorithms (Weiss chapter )

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

COMP Data Structures

Data Structures Brett Bernstein

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

Transcription:

Binary Node class BinaryNode public BinaryNode( ) this( null, null, null ); public BinaryNode( Object theelement,binarynode lt,binarynode rt); public static int size( BinaryNode t ); // size of subtree rooted at t public static int height( BinaryNode t ); public void printpreorder( ); public void printpostorder( ); public void printinorder( ); public BinaryNode duplicate( ); // make a duplicate tree and return root public Object getelement( ); public BinaryNode getleft( ); public BinaryNode getright( ); public void setelement( Object x ); public void setleft( BinaryNode t ); public void setright( BinaryNode t ); private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

Binary Trees public class BinaryTree public BinaryTree( ); public BinaryTree( Object rootitem ); public void printpreorder( ); public void printinorder( ); public void printpostorder( ); public void makeempty( ); public boolean isempty( ); /** Forms a new tree from rootitem, t1 and t2. t1 not equal to t2. */ public void merge( Object rootitem, BinaryTree t1, BinaryTree t2 ); public int size( ); public int height( ); public BinaryNode getroot( ); private BinaryNode root; 02/18/03 Lecture 12 2

Binary Trees Cont d public class BinaryTree static public void main( String [ ] args ) BinaryTree t1 = new BinaryTree( "1" ); BinaryTree t3 = new BinaryTree( "3" ); BinaryTree t5 = new BinaryTree( "5" ); BinaryTree t7 = new BinaryTree( "7" ); BinaryTree t2 = new BinaryTree( ); BinaryTree t4 = new BinaryTree( ); BinaryTree t6 = new BinaryTree( ); t2.merge( "2", t1, t3 ); t6.merge( "6", t5, t7 ); t4.merge( "4", t2, t6 ); System.out.println( "t4 should be perfect 1-7; t2 empty" ); System.out.println( "----------------" ); System.out.println( "t4" ); t4.printinorder( ); System.out.println( "----------------" ); System.out.println( "t2" ); t2.printinorder( ); System.out.println( "----------------" ); System.out.println( "t4 size: " + t4.size( ) ); System.out.println( "t4 height: " + t4.height( ) ); 02/18/03 Lecture 12 3

Binary Trees Cont d public void printpreorder( ) System.out.println( element ); // Node if( left!= null ) left.printpreorder( ); // Left if( right!= null ) right.printpreorder( ); // Right public void printpostorder( ) if( left!= null ) left.printpostorder( ); // Left if( right!= null ) right.printpostorder( ); // Right System.out.println( element ); // Node public void printinorder( ) if( left!= null ) left.printinorder( ); // Left System.out.println( element ); // Node if( right!= null ) right.printinorder( ); // Right 02/18/03 Lecture 12 4

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 18.14 Result of a naive merge operation: Subtrees are shared. 02/18/03 Lecture 12 5

Binary Trees Cont d public void merge( Object rootitem, BinaryTree t1, BinaryTree t2 ) if( t1.root == t2.root && t1.root!= null ) System.err.println( "lefttree==righttree; merge aborted" ); return; root = new BinaryNode( rootitem, t1.root, t2.root ); if( this!= t1 ) t1.root = null; if( this!= t2 ) t2.root = null; 02/18/03 Lecture 12 6

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 18.15 Aliasing problems in the merge operation; t1 is also the current object. Consider these! t1.merge(x,t1,t2); t1.merge(x,t1,t2); t1.merge(x,t3,t3); 02/18/03 Lecture 12 7

Binary Trees Cont d public BinaryNode duplicate( ) BinaryNode root = new BinaryNode( element, null, null ); if( left!= null ) root.left = left.duplicate( ); if( right!= null ) root.right = right.duplicate( ); return root; // Return resulting tree 02/18/03 Lecture 12 8

Binary Search Trees Values in the left subtree are smaller than the value stored at root. Values in the right subtree are larger than the value stored at root. Figure 19.1 Two binary trees: (a) a search tree; (b) not a search tree 02/18/03 Lecture 12 9

Binary Search Trees // BinarySearchTree class // // void insert( x ) --> Insert x // void remove( x ) --> Remove x // void removemin( ) --> Remove minimum item // Comparable find( x ) --> Return item that matches x // Comparable findmin( ) --> Return smallest item // Comparable findmax( ) --> Return largest item // boolean isempty( ) --> Return true if empty; else false // void makeempty( ) --> Remove all items public class BinarySearchTree private Comparable elementat( BinaryNode t ) return t == null? null : t.element; protected BinaryNode insert( Comparable x, BinaryNode t ) protected BinaryNode remove( Comparable x, BinaryNode t ) protected BinaryNode removemin( BinaryNode t ) protected BinaryNode findmin( BinaryNode t ) private BinaryNode findmax( BinaryNode t ) private BinaryNode find( Comparable x, BinaryNode t ) protected BinaryNode root; 02/18/03 Lecture 12 10

Binary Search Trees public static void main( String [ ] args ) BinarySearchTree t = new BinarySearchTree( ); final int NUMS = 4000; final int GAP = 37; System.out.println( "Checking... (no more output means success)" ); for( int i = GAP; i!= 0; i = ( i + GAP ) % NUMS ) t.insert( new Integer( i ) ); for( int i = 1; i < NUMS; i+= 2 ) t.remove( new Integer( i ) ); if( ((Integer)(t.findMin( ))).intvalue( )!= 2 ((Integer)(t.findMax( ))).intvalue( )!= NUMS - 2 ) System.out.println( "FindMin or FindMax error!" ); for( int i = 2; i < NUMS; i+=2 ) if( ((Integer)(t.find( new Integer( i ) ))).intvalue( )!= i ) System.out.println( "Find error1!" ); for( int i = 1; i < NUMS; i+=2 ) if( t.find( new Integer( i ) )!= null ) System.out.println( "Find error2!" ); 02/18/03 Lecture 12 11

Binary Search Trees protected BinaryNode insert( Comparable x, BinaryNode t ) if( t == null ) t = new BinaryNode( x ); else if( x.compareto( t.element ) < 0 ) t.left = insert( x, t.left ); else if( x.compareto( t.element ) > 0 ) t.right = insert( x, t.right ); else throw new DuplicateItemException( x.tostring( ) ); // Duplicate return t; protected BinaryNode remove( Comparable x, BinaryNode t ) if( t == null ) throw new ItemNotFoundException( x.tostring( ) ); if( x.compareto( t.element ) < 0 ) t.left = remove( x, t.left ); else if( x.compareto( t.element ) > 0 ) t.right = remove( x, t.right ); else if( t.left!= null && t.right!= null ) t.element = findmin( t.right ).element; t.right = removemin( t.right ); else t = ( t.left!= null )? t.left : t.right; return t; 02/18/03 Lecture 12 12

Binary Search Trees protected BinaryNode removemin( BinaryNode t ) if( t == null ) throw new ItemNotFoundException( ); else if( t.left!= null ) t.left = removemin( t.left ); return t; else return t.right; protected BinaryNode findmin( BinaryNode t ) if( t!= null ) while( t.left!= null ) t = t.left; return t; private BinaryNode find( Comparable x, BinaryNode t ) while( t!= null ) if( x.compareto( t.element ) < 0 ) t = t.left; else if( x.compareto( t.element ) > 0 ) t = t.right; else return t; // Match return null; // Not found 02/18/03 Lecture 12 13

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 19.1 Two binary trees: (a) a search tree; (b) not a search tree Figure 19.2 Binary search trees (a) before and (b) after the insertion of 6 Figure 19.3 Deletion of node 5 with one child: (a) before and (b) after Figure 19.4 Deletion of node 2 with two children: (a) before and (b) after 02/18/03 Lecture 12 14

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 8.3 Basic action of insertion sort (the shaded part is sorted) 02/18/03 Lecture 12 15

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 8.4 A closer look at the action of insertion sort (the dark shading indicates the sorted area; the light shading is where the new element was placed). 02/18/03 Lecture 12 16

Insertion Sort public static void insertionsort( Comparable [ ] a ) for( int p = 1; p < a.length; p++ ) Comparable tmp = a[ p ]; int j = p; for( ; j > 0 && tmp.compareto( a[ j - 1 ] ) < 0; j-- ) a[ j ] = a[ j - 1 ]; a[ j ] = tmp; 02/18/03 Lecture 12 17

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 8.5 Shellsort after each pass if the increment sequence is 1, 3, 5 02/18/03 Lecture 12 18

ShellSort public static void shellsort( Comparable [ ] a ) for( int gap = a.length / 2; gap > 0; gap = gap == 2? 1 : (int) ( gap / 2.2 ) ) for( int i = gap; i < a.length; i++ ) Comparable tmp = a[ i ]; int j = i; for( ; j >= gap && tmp.compareto( a[ j - gap ] ) < 0; j -= gap ) a[ j ] = a[ j - gap ]; a[ j ] = tmp; 02/18/03 Lecture 12 19

Merge Sort public static void mergesort( Comparable [ ] a ) Comparable [ ] tmparray = new Comparable[ a.length ]; mergesort( a, tmparray, 0, a.length - 1 ); private static void mergesort( Comparable [ ] a, Comparable [ ] tmparray, int left, int right ) if( left < right ) int center = ( left + right ) / 2; mergesort( a, tmparray, left, center ); mergesort( a, tmparray, center + 1, right ); merge( a, tmparray, left, center + 1, right ); 02/18/03 Lecture 12 20

Merge in Merge Sort private static void merge( Comparable [ ] a, Comparable [ ] tmparray, int leftpos, int rightpos, int rightend ) int leftend = rightpos - 1; int tmppos = leftpos; int numelements = rightend - leftpos + 1; while( leftpos <= leftend && rightpos <= rightend ) if( a[ leftpos ].compareto( a[ rightpos ] ) < 0 ) tmparray[ tmppos++ ] = a[ leftpos++ ]; else tmparray[ tmppos++ ] = a[ rightpos++ ]; while( leftpos <= leftend ) // Copy rest of first half tmparray[ tmppos++ ] = a[ leftpos++ ]; while( rightpos <= rightend ) // Copy rest of right half tmparray[ tmppos++ ] = a[ rightpos++ ]; for( int i = 0; i < numelements; i++, rightend-- ) a[ rightend ] = tmparray[ rightend ]; 02/18/03 Lecture 12 21

Figure 8.10 Quicksort 02/18/03 Lecture 12 22 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley

Figure 8.11 Partitioning algorithm: Pivot element 6 is placed at the end. Figure 8.12 Partitioning algorithm: i stops at large element 8; j stops at small element 2. Figure 8.13 Partitioning algorithm: The out-of-order elements 8 and 2 are swapped. Figure 8.14 Partitioning algorithm: i stops at large element 9; j stops at small element 5. Figure 8.15 Partitioning algorithm: The out-of-order elements 9 and 5 are swapped. 02/18/03 Lecture 12 23

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 8.16 Partitioning algorithm: i stops at large element 9; j stops at small element 3. Figure 8.17 Partitioning algorithm: Swap pivot and element in position i. Figure 8.18 Original array Figure 8.19 Result of sorting three elements (first, middle, and last) Figure 8.20 Result of swapping the pivot with the next-to-last element 02/18/03 Lecture 12 24

Quicksort public static void quicksort( Comparable [ ] a ) quicksort( a, 0, a.length - 1 ); private static void quicksort( Comparable [ ] a, int low, int high ) if( low + CUTOFF > high ) insertionsort( a, low, high ); else // Sort low, middle, high int middle = ( low + high ) / 2; if( a[ middle ].compareto( a[ low ] ) < 0 ) swapreferences( a, low, middle ); if( a[ high ].compareto( a[ low ] ) < 0 ) swapreferences( a, low, high ); if( a[ high ].compareto( a[ middle ] ) < 0 ) swapreferences( a, middle, high ); swapreferences( a, middle, high - 1 ); // Place pivot at position high - 1 Comparable pivot = a[ high - 1 ]; int i, j; // Begin partitioning for( i = low, j = high - 1; ; ) while( a[ ++i ].compareto( pivot ) < 0 ) /* Do nothing */ ; while( pivot.compareto( a[ --j ] ) < 0 ) /* Do nothing */ ; if( i >= j ) break; swapreferences( a, i, j ); swapreferences( a, i, high - 1 ); quicksort( a, low, i - 1 ); // Sort small elements quicksort( a, i + 1, high ); // Sort large elements 02/18/03 Lecture 12 25