0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Ch13 Binary Search Tree

5,248

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
5,248
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
157
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

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