Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 13-2 Chapter Objectives Define a binary search tree abstract.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Balanced Search Trees Chapter Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
1 Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Chapter 20 Binary Search Trees
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Red-Black Trees Definitions and Bottom-Up Insertion.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
Copyright © 2005 Pearson Addison-Wesley. All rights reserved Balancing Binary Trees There are many approaches to balancing binary trees One method.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
AA Trees.
Trees Chapter 11 (continued)
Search Trees.
Trees Chapter 11 (continued)
Binary Search Trees (Continued)
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
(edited by Nadia Al-Ghreimil)
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
Chapter 6 Transform and Conquer.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
(edited by Nadia Al-Ghreimil)
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
Chapter 20 Binary Search Trees
Presentation transcript:

Chapter 13 Binary Search Trees

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 13.1 The operations on a binary search tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 13.1

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 13.1 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 13.2 UML description of the BinarySearchTreeADT

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedBinarySearchTree - Constructors

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedBinarySearchTree - addElement

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedBinarySearchTree - addElement

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedBinarySearchTree - addElement

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 13.3 Adding elements to a binary search tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedBinarySearchTree - removeElement

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedBinarySearchTree - removeElement

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedBinarySearchTree - removeElement

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedBinarySearchTree - replacement

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedBinarySearchTree - replacement

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 13.4 Removing elements from a binary tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedBinarySearchTree - removeAllOccurrences

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 13.5 Removing the minimum element from a binary search tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 13.6 The common operations on a list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 13.7 The operation particular to an ordered list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 13.2

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 13.2 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 13.2 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 13.2 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 13.8 Analysis of linked list and binary search tree implementations of an ordered list

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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:

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 13.9 A degenerate binary tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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))

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE Unbalanced tree and balanced tree after a right rotation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE Unbalanced tree and balanced tree after a left rotation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE A rightleft rotation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE A leftright rotation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE A right rotation in an AVL tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE A rightleft rotation in an AVL tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE Valid red/black trees

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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:

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE Red/black tree after insertion

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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:

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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:

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE Red/black tree after insertion

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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:

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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:

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE Red/black tree after removal

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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:

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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:

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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:

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved 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

Copyright © 2005 Pearson Addison-Wesley. All rights reserved TABLE 13.1 Operations on a TreeSet

Copyright © 2005 Pearson Addison-Wesley. All rights reserved TABLE 13.2 Operations on a TreeMap