Download presentation
Presentation is loading. Please wait.
1
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall
2
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
3
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
4
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
5
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.
6
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 SearchTreeInterfaceView source code
7
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 →
8
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.
9
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.
10
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 BinarySearchTree Note Derived from BinaryTree Call by constructor to protected method setRootNode (inherited from BinaryTree ) Other methods contains getEntry add remove
11
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
12
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 algorithmsearch algorithm Root node of tree or subtree enables manipulation of descendant nodes Note implementation in methodsimplementation getEntry findEntry Method contains simply calls getEntry
13
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
14
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.
15
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 →
16
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 iterative method addEntry
17
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
18
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.
19
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.
20
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.
21
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.
22
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.
23
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;
24
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.
25
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.
26
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 implementationsource code for recursive
27
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 implementationsource code for iterative
28
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
29
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)
30
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
31
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.
32
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
33
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)interface for a dictionary Consider an implementation of the ADT dictionary that uses a binary search treeimplementation of the ADT dictionary Note methodsmethods add remove Use of iterator
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.