a binary search tree implementation chapter 27 slides by steve armstrong letourneau university...

33
A Binary Search Tree Implementati on Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall

Post on 22-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

A Binary Search Tree

Implementation

Chapter 27

Slides by Steve ArmstrongLeTourneau University

Longview, TX2007,Prentice Hall

Page 2: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Chapter Contents

• Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning the Class Definition

• Searching and Retrieving• Traversing• Adding an Entry

Recursive Implementation Iterative Implementation

Page 3: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Chapter Contents

• Removing an Entry … Whose Node is a leaf Whose Node Has One Child Whose Node has Two Children In the Root Recursive Implementation Iterative Implementation

• Efficiency of Operations Importance of Balance Order in Which Nodes Are Added

• Implementation of the ADT Dictionary

Page 4: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Getting Started

• A binary search tree is a binary tree Nodes contain Comparable objects

• For each node in the tree The data in a node is greater than the data in

the node's left subtree The data in a node is less than the data in the

node's right subtree

Page 5: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Getting Started

Fig. 27-1 A binary search tree of names.

Page 6: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

An Interface for the Binary Search Tree

• Includes common operations of a tree• Also includes basic database operations

Search Retrieve Add Remove Traverse

• View source code for SearchTreeInterface

Page 7: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

An Interface for the Binary Search Tree

Fig. 27-2 Adding an entry that matches an entry already in a binary tree … continued →

Page 8: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

An Interface for the Binary Search Tree

Fig. 27-2 Adding an entry that matches an entry already in a binary tree.

Page 9: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Duplicate Entries

Fig. 27-3 A binary search tree with duplicate entries.

If duplicates are allowed, place the duplicate in the

entry's right subtree.

If duplicates are allowed, place the duplicate in the

entry's right subtree.

Page 10: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Beginning the Class Definition

• View source code of BinarySearchTree• Note

Derived from BinaryTree Call by constructor to protected method setRootNode (inherited from BinaryTree)

• Other methods contains getEntry add remove

Page 11: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Searching and Retrieving

• Like performing a binary search of an array

• For a binary array Search one of two halves of the array

• For the binary search tree You search one of two subtrees of the binary

search tree

Page 12: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Searching and Retrieving

• View search algorithm Root node of tree or subtree enables

manipulation of descendant nodes

• Note implementation in methods getEntry findEntry

• Method contains simply calls getEntry

Page 13: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Traversing

• The SearchTreeInterface provides the method getInorderIterator Returns an inorder iterator

• Our class is a subclass of BinaryTree It inherits getInorderIterator This iterator traverses entries in ascending

order Uses the entries' method compareTo

Page 14: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Adding an Entry

Fig. 27-4 (a) A binary search tree; (b) the same tree

after adding Chad.

Page 15: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Adding an Entry Recursively

Fig. 27-5 Recursively adding Chad to smaller subtrees of a binary search tree … continued →

Page 16: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Adding an Entry Recursively

Fig. 27-5 (ctd.) Recursively adding

Chad to smaller subtrees of a binary

search tree.

Click to view recursive method addEntry

Click to view recursive method addEntry

Click to view iterative method addEntry

Click to view iterative method addEntry

Page 17: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Removing an Entry

• The remove method must receive an entry to be matched in the tree If found, it is removed Otherwise the method returns null

• Three cases The node has no children, it is a leaf (simplest

case) The node has one child The node has two children

Page 18: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Removing an Entry, Node a Leaf

Fig. 27-6 (a) Two possible configurations of leaf node N; (b) the resulting two possible configurations after removing node N.

Page 19: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Removing an Entry, Node Has One Child

Fig. 27-6 (a) Two possible configurations of leaf node N; (b) the resulting two possible configurations

after removing node N.

Page 20: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Removing an Entry, Node Has Two Children

Fig. 27-8 Two possible configurations of node N that has two children.

Page 21: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Removing an Entry, Node Has Two Children

Fig. 27-9 Node N and its subtrees; (a) entry a is immediately before e, b is immediately after e; (b) after

deleting the node that contained a and replacing e with a.

Page 22: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Removing an Entry, Node Has Two Children

Fig. 27-10 The largest entry a in node N's left subtree occurs in the subtree's rightmost node R.

Page 23: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Removing an Entry, Node Has Two Children

Fig. 27-11 (a) A binary search tree; (b) after removing Chad;

Page 24: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Removing an Entry, Node Has Two Children

Fig. 27-11 (c) after removing Sean; (d) after removing Kathy.

Page 25: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Removing an Entry in the Root

Fig. 27-12 (a) Two possible configurations of a root that has one child; (b) after removing the root.

Page 26: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Recursive Implementation

• Details similar to adding an entry• The public remove method calls a private

recursive remove method The public remove returns removed entry Private remove must return root of revised tree Thus use parameter that is an instance of ReturnObject to return the value the public remove needs

• View source code for recursive implementation

Page 27: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Iterative Implementation

• To locate the desired entry The remove method given entry to be

matched If remove finds the entry, it returns the entry If not, it returns null

• The compareTo method used to make comparisons with the entries in the tree

• View source code for iterative implementation

Page 28: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Efficiency of Operations

• Operations add, remove, getEntry require a search that begins at the root

• Maximum number of comparisons is directly proportional to the height, h of the tree

• These operations are O(h)

• Thus we desire the shortest binary search tree we can create from the data

Page 29: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Efficiency of Operations

Fig. 27-13 Two binary search trees

that contain the same data.

Shorter tree has efficiency O(log n)

Shorter tree has efficiency O(log n)

Page 30: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Importance of Balance

• Completely balanced Subtrees of each node have exactly same

height

• Height balanced Subtrees of each node in the tree differ in

height by no more than 1

• Completely balanced or height balanced trees are balanced

Page 31: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Importance of Balance

Fig. 27-14 Some binary trees that are height balanced.

Page 32: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Importance of Balance

• The order in which entries are added affect the shape of the tree

• If entries are added to an empty binary tree Best not to have them sorted first Tree is more balanced if entries are in random

order

Page 33: A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Implementation of the ADT Dictionary

• Recall interface for a dictionary (Ch. 17)

• Consider an implementation of the ADT dictionary that uses a binary search tree

• Note methods add remove Use of iterator