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

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

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

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

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

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

Data Structures in Java

CMSC 341. Binary Search Trees CMSC 341 BST

CS350: Data Structures Tree Traversal

CIS 121. Binary Search Trees

Priority Queues. 04/10/03 Lecture 22 1

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

CMSC 341 Lecture 10 Binary Search Trees

Generic BST Interface

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

CE 221 Data Structures and Algorithms

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

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

COMP : Trees. COMP20012 Trees 219

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

Implementations II 1

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

ITI Introduction to Computing II

ITI Introduction to Computing II

Priority Queue. 03/09/04 Lecture 17 1

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

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

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

Binary Search Trees. See Section 11.1 of the text.

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

CS350: Data Structures Binary Search Trees

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

COMS 3137 Class Notes

Lecture 23: Binary Search Trees

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

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

CS 350 : Data Structures Binary Search 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

Implementations II 1

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

CS350: Data Structures AA Trees

Why Do We Need Trees?

Trees, Part 1: Unbalanced Trees

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

Trees, Binary Trees, and Binary Search Trees

Principles of Computer Science

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

Examples

IMPLEMENTING BINARY TREES

The Binary Search Tree ADT

TREES Lecture 10 CS2110 Spring2014

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

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

Priority Queues and Huffman Trees

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

Binary Trees Fall 2018 Margaret Reid-Miller

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

CMSC 341 Lecture 15 Leftist Heaps

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CMSC 341 Lecture 15 Leftist Heaps

CS302 - Data Structures using C++

TREES Lecture 12 CS2110 Spring 2018

AVL Trees. See Section 19.4of the text, p

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

CSE 214 Computer Science II Heaps and Priority Queues

TREES Lecture 12 CS2110 Fall 2016

CS302 - Data Structures using C++

CMPT 225. Binary Search Trees

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

CS24 Week 8 Lecture 1

Priority Queues Heaps Heapsort

COMP 250. Lecture 22. binary search trees. Oct. 30, 2017

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

CMSC 341 Leftist Heaps

If you took your exam home last time, I will still regrade it if you want.

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees!

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

CSE 326: Data Structures Binary Search Trees

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees. Linda Shapiro Spring 2016

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Lecture No.07. // print the final avaerage wait time.

Binary trees. Binary trees. Binary trees

Binary Trees, Binary Search Trees

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

Problem Set 6 Due: 6pm Friday, November 12

Binary Tree Implementation

Outline. Binary Tree

Programming II (CS300)

Data Structures and Algorithms for Engineers

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Binary Tree Applications

Binomial Queue deletemin ADTs Seen So Far

Introduction to Algorithms and Data Structures

Priority Queues (Heaps)

ECE242 Data Structures and Algorithms Fall 2008

Expression Trees and the Heap

Transcription:

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 18.4 A Unix directory 02/10/04 Lecture 9 1

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 18.7 The Unix directory with file sizes 02/10/04 Lecture 9 2

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 18.11 Uses of binary trees: (a) an expression tree and (b) a Huffman coding tree 02/10/04 Lecture 9 3

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 18.1 A tree, with height and depth information 02/10/04 Lecture 9 4

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 18.2 A tree viewed recursively 02/10/04 Lecture 9 5

Binary Trees Constructors Tree Properties TreePrint Get Info Set Info 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 ); Data (private) private Object element; private BinaryNode left; private BinaryNode right; 02/10/04 Lecture 9 6

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/10/04 Lecture 9 7

Binary Trees 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/10/04 Lecture 9 8

Binary Trees 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/10/04 Lecture 9 9

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/10/04 Lecture 9 10

Binary Trees 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; 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/10/04 Lecture 9 11

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/10/04 Lecture 9 12

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/10/04 Lecture 9 13

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( ) / findmax( ) --> Return smallest / 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/10/04 Lecture 9 14

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/10/04 Lecture 9 15

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/10/04 Lecture 9 16

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/10/04 Lecture 9 17