chapter 13 binary search trees. copyright © 2005 pearson addison-wesley. all rights reserved. 13-2...
Post on 21-Dec-2015
222 views
TRANSCRIPT
![Page 1: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/1.jpg)
Chapter 13
Binary Search Trees
![Page 2: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/2.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2
Chapter Objectives
• Define a binary search tree abstract data structure
• Demonstrate how a binary search tree can be used to solve problems
• Examine various binary search tree implementations
• Compare binary search tree implementations
![Page 3: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/3.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-3
Binary Search Trees
• A binary search tree is a binary tree with the added property that for each node, the left child is less than the parent is less than or equal to the right child
• Given this refinement of our earlier definition of a binary tree, we can now include additional operations
![Page 4: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/4.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-4
FIGURE 13.1 The operations on a binary search tree
![Page 5: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/5.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-5
Listing 13.1
![Page 6: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/6.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-6
Listing 13.1 (cont.)
![Page 7: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/7.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-7
FIGURE 13.2 UML description of the BinarySearchTreeADT
![Page 8: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/8.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-8
Implementing Binary Search Trees With Links
• We can simply extend our definition of a LinkedBinaryTree to create a LinkedBinarySearchTree
• This class will provide two constructors, one to create an empty tree and the other to create a one-element binary tree
![Page 9: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/9.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-9
LinkedBinarySearchTree - Constructors
![Page 10: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/10.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-10
Implementing Binary Search Trees With Links
• Now that we know more about how this tree is to be used (and structured) it is possible to define a method to add an element to the tree
• The addElement method finds the proper location for the given element and adds it there as a leaf
![Page 11: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/11.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-11
LinkedBinarySearchTree - addElement
![Page 12: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/12.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-12
LinkedBinarySearchTree - addElement
![Page 13: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/13.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-13
LinkedBinarySearchTree - addElement
![Page 14: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/14.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-14
FIGURE 13.3 Adding elements to a binary search tree
![Page 15: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/15.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-15
Removing Elements
• Removing elements from a binary search tree requires– Finding the element to be removed
– If that element is not a leaf, then replace it with its inorder successor
– Return the removed element
![Page 16: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/16.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-16
Removing Elements
• The removeElement method makes use of a private replacement method to find the proper element to replace a non-leaf element that is removed
![Page 17: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/17.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-17
LinkedBinarySearchTree - removeElement
![Page 18: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/18.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-18
LinkedBinarySearchTree - removeElement
![Page 19: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/19.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-19
LinkedBinarySearchTree - removeElement
![Page 20: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/20.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-20
LinkedBinarySearchTree - replacement
![Page 21: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/21.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-21
LinkedBinarySearchTree - replacement
![Page 22: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/22.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-22
FIGURE 13.4 Removing elements from a binary tree
![Page 23: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/23.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-23
Removing All Occurrences
• The removeAllOccurrences method removes all occurrences of an element from the tree
• This method uses the removeElement method
• This method makes a distinction between the first call and successive calls to the removeElement method
![Page 24: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/24.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-24
LinkedBinarySearchTree - removeAllOccurrences
![Page 25: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/25.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-25
The removeMin Operation
• There are three cases for the location of the minimum element in a binary search tree:– If the root has no left child, then the root is the
minimum element and the right child of the root becomes the new root
– If the leftmost node of the tree is a leaf, then we set its parent’s left child reference to null
– If the leftmost node of the tree is an internal node, then we set its parent’s left child reference to point to the right child of the node to be removed
![Page 26: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/26.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-26
FIGURE 13.5 Removing the minimum element from a binary search tree
![Page 27: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/27.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-27
Using Binary Search Trees: Implementing Ordered Lists
• Lets look at an example using a binary search tree to provide an efficient implementation of an ordered list
• For simplicity, we will implement both the ListADT and the OrderedListADT in the BinarySearchTreeList class
![Page 28: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/28.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-28
FIGURE 13.6 The common operations on a list
![Page 29: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/29.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-29
FIGURE 13.7 The operation particular to an ordered list
![Page 30: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/30.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-30
Listing 13.2
![Page 31: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/31.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-31
Listing 13.2 (cont.)
![Page 32: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/32.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-32
Listing 13.2 (cont.)
![Page 33: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/33.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-33
Listing 13.2 (cont.)
![Page 34: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/34.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-34
FIGURE 13.8 Analysis of linked list and binary search tree implementations of an ordered list
![Page 35: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/35.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-35
Balanced Binary Search Trees
• Why is our balance assumption so important?
• Lets look at what happens if we insert the following numbers in order without rebalancing the tree:
3 5 9 12 18 20
![Page 36: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/36.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-36
FIGURE 13.9 A degenerate binary tree
![Page 37: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/37.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-37
Degenerate Binary Trees
• The resulting tree is called a degenerate binary tree
• Degenerate binary search trees are far less efficient than balanced binary search trees (O(n) on find as opposed to O(logn))
![Page 38: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/38.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-38
Balancing Binary Trees
• There are many approaches to balancing binary trees
• One method is brute force– Write an inorder traversal to a file
– Use a recursive binary search of the file to rebuild the tree
![Page 39: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/39.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-39
Balancing Binary Trees
• Better solutions involve algorithms such as red-black trees and AVL trees that persistently maintain the balance of the tree
• Most all of these algorithms make use of rotations to balance the tree
• Lets examine each of the possible rotations
![Page 40: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/40.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-40
Right Rotation
• Right rotation will solve an imbalance if it is caused by a long path in the left sub-tree of the left child of the root
![Page 41: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/41.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-41
FIGURE 13.10 Unbalanced tree and balanced tree after a right rotation
![Page 42: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/42.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-42
Left Rotation
• Left rotation will solve an imbalance if it is caused by a long path in the right sub-tree of the right child of the root
![Page 43: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/43.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-43
FIGURE 13.11 Unbalanced tree and balanced tree after a left rotation
![Page 44: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/44.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-44
Rightleft Rotation
• Rightleft rotation will solve an imbalance if it is caused by a long path in the left sub-tree of the right child of the root
• Perform a right rotation of the left child of the right child of the root around the right child of the root, and then perform a left rotation of the resulting right child of the root around the root
![Page 45: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/45.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-45
FIGURE 13.12 A rightleft rotation
![Page 46: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/46.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-46
Leftright Rotation
• Leftright rotation will solve an imbalance if it is caused by a long path in the right sub-tree of the left child of the root
• Perform a left rotation of the right child of the left child of the root around the left child of the root, and then perform a right rotation of the resulting left child of the root around the root
![Page 47: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/47.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-47
FIGURE 13.13 A leftright rotation
![Page 48: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/48.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-48
AVL Trees
• AVL trees keep track of the difference in height between the right and left sub-trees for each node
• This difference is called the balance factor
• If the balance factor of any node is less than -1 or greater than 1, then that sub-tree needs to be rebalanced
• The balance factor of any node can only be changed through either insertion or deletion of nodes in the tree
![Page 49: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/49.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-49
AVL Trees
• If the balance factor of a node is -2, this means the left sub-tree has a path that is too long
• If the balance factor of the left child is -1, this means that the long path is the left sub-tree of the left child
• In this case, a simple right rotation of the left child around the original node will solve the imbalance
![Page 50: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/50.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-50
FIGURE 13.14 A right rotation in an AVL tree
![Page 51: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/51.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-51
AVL Trees
• If the balance factor of a node is +2, this means the right sub-tree has a path that is too long
• Then if the balance factor of the right child is +1, this means that the long path is the right sub-tree of the right child
• In this case, a simple left rotation of the right child around the original node will solve the imbalance
![Page 52: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/52.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-52
AVL Trees
• If the balance factor of a node is +2, this means the right sub-tree has a path that is too long
• Then if the balance factor of the right child is -1, this means that the long path is the left sub-tree of the right child
• In this case, a rightleft double rotation will solve the imbalance
![Page 53: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/53.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-53
FIGURE 13.15 A rightleft rotation in an AVL tree
![Page 54: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/54.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-54
AVL Trees
• If the balance factor of a node is -2, this means the right sub-tree has a path that is too long
• Then if the balance factor of the left child is +1, this means that the long path is the right sub-tree of the left child
• In this case, a leftright double rotation will solve the imbalance
![Page 55: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/55.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-55
Red/Black Trees
• Red/Black trees provide another alternative implementation of balanced binary search trees
• A red/black tree is a balanced binary search tree where each node stores a color (usually implemented as a boolean)
• The following rules govern the color of a node:– The root is black
– All children of a red node are black
– Every path from the root to a leaf contains the same number of black nodes
![Page 56: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/56.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-56
FIGURE 13.16 Valid red/black trees
![Page 57: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/57.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-57
Insertion into Red/Black Trees• The color of a new element is set to red
• Once the new element has been inserted, the tree is rebalanced/recolored as needed to to maintain the properties of a red/black tree
• This process is iterative beginning at the point of insertion and working up the tree toward the root
• The process terminates when we reach the root or when the parent of the current element is black
![Page 58: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/58.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-58
Insertion into Red/Black Trees• In each iteration of the rebalancing process, we will
focus on the color of the sibling of the parent of the current node
• There are two possibilities for the parent of the current node:– The parent could be a left child
– The parent could be a right child
• The color of a null node is considered to be black
![Page 59: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/59.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-59
Insertion into Red/Black Trees• In the case where the parent of the current node is a right
child, there are two cases– Leftaunt.color == red
– Leftaunt.color == black
• If leftaunt.color is red then the processing steps are:
![Page 60: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/60.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-60
FIGURE 13.17 Red/black tree after insertion
![Page 61: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/61.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-61
Insertion into Red/Black Trees• If leftaunt.color is black, we first must check to see if current is
a left child or a right child
• If current is is a left child, then we must set current equal to current.parent and then rotate current.left to the right
• The we continue as if current were a right child to begin with:
![Page 62: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/62.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-62
Insertion into Red/Black Trees• In the case where the parent of current is a left child,
there are two cases: either rightuncle.color == red or rightuncle.color == black
• If rightuncle.color == red then the processing steps are:
![Page 63: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/63.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-63
FIGURE 13.18 Red/black tree after insertion
![Page 64: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/64.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-64
Insertion into Red/Black Trees• If rightuncle.color == black then we first need to check to see if
current is a left or right child
• If current is a right child then we set current equal to current.parent then rotate current.right ot the left around current
• We then continue as if current was left child to begin with:
![Page 65: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/65.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-65
Element Removal from Red/Black Trees• As with insertion, the tree will need to be rebalanced/recolored
after the removal of an element
• Again, the process is an iterative one beginning at the point of removal and continuing up the tree toward the root
• This process terminates when we reach the root or when current.color == red
• Like insertion, the cases for removal are symmetrical depending upon whether current is a left or right chid
• In insertion, we focused on the color of the sibling of the parent
• In removal, we focus on the color of the sibling of current keeping in mind that a null node is considered to be black
![Page 66: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/66.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-66
Element Removal from Red/Black Trees• We will only examine the cases where current is a right child,
the other cases are easily derived
• If the sibling’s color is red then we begin with the following steps:
![Page 67: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/67.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-67
FIGURE 13.19 Red/black tree after removal
![Page 68: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/68.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-68
Element Removal from Red/Black Trees• Next our processing continues regardless of whether the
original sibling was red or black
• Now our processing is divided into two cases based upon the color of the children of the sibling
• If both of the children of the sibling are black then:
![Page 69: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/69.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-69
Element Removal from Red/Black Trees• If the children of the sibling are not both black, then we check
to see if the left child of the sibling is black
• If it is, then we must complete the following steps before continuing:
![Page 70: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/70.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-70
Element Removal from Red/Black Trees• Then to complete the process in the case when both of the
children of the sibling are not black:
![Page 71: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/71.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-71
Binary Search Trees in the Java Collections API
• Java provides two implementations of balanced binary search trees– TreeSet
– TreeMap
• In order to understand the difference between these two, we must first discuss the difference between a set and a map
![Page 72: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/72.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-72
Sets and Maps
• In the terminology of the Java Collections API, all of the collections we have discussed thus far would be considered sets
• A set is a collection where all of the data associated with an object is stored in the collection
• A map is a collection where keys that reference an object are stored in the collection but the remaining data is stored separately
![Page 73: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/73.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-73
Sets and Maps
• Maps are useful because they allow us to manipulate keys within a collection rather than the entire object
• This allows collections to be smaller, more efficient, and easier to manage
• This also allows for the same object to be part of multiple collections by having keys in each
![Page 74: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/74.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-74
The TreeSet and TreeMap Classes
• Both the TreeSet and TreeMap classes are red/black tree implementations of a balanced binary search tree
• The operations on both are listed in the following tables
![Page 75: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/75.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-75
TABLE 13.1 Operations on a TreeSet
![Page 76: Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract](https://reader031.vdocuments.us/reader031/viewer/2022013011/56649d615503460f94a42fc4/html5/thumbnails/76.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-76
TABLE 13.2 Operations on a TreeMap