Chapter 20 Binary Search Trees

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
Advertisements

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. 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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 1 The ADT Binary Search Tree Definition The ADT Binary Search Tree is a.
Chapter 23 Multi-Way Search Trees. Chapter Scope Examine 2-3 and 2-4 trees Introduce the concept of a B-tree Example specialized implementations of B-trees.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
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 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 9: Trees Java Software Structures: Designing and Using Data.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Chapter 10 Trees – part B.
Building Java Programs Binary Search Trees; TreeSet.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
Chapter 10-A Trees Modified
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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 and Binary Search Trees Using binary trees Definition of a binary search tree Implementing binary search trees –Add Element –Remove Element Using.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
1 Trees and Binary Search Trees Using binary trees Definition of a binary search tree Implementing binary search trees –Add Element –Remove Element Using.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Trees Chapter 19, 20 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
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.
Binary Search Trees Chapter 7 Objectives
The Tree ADT.
Chapter 25 Binary Search Trees
Recursive Objects (Part 4)
BST Trees
Binary Search Tree (BST)
Trees and Binary Search Trees
Binary Search Tree Chapter 10.
Trees.
Chapter 21 Heaps and Priority Queues
Recall: Nodes and Linked Lists
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Building Java Programs
Lecture 12 CS203 1.
Lecture 21: Binary Search Trees; TreeSet
CHAPTER 11: Priority Queues and Heaps
Trees.
Chapter 20 Binary Search Trees
Chapter 21 Heaps and Priority Queues
Presentation transcript:

Chapter 20 Binary Search Trees

Chapter Scope Binary search tree processing Using BSTs to solve problems BST implementations Strategies for balancing BSTs Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Binary Search Trees A search tree is a tree whose elements are organized to facilitate finding a particular element when needed A binary search tree is a binary tree that, for each node n the left subtree of n contains elements less than the element stored in n the right subtree of n contains elements greater than or equal to the element stored in n Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Binary Search Trees Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Binary Search Trees To determine if a particular value exists in a tree start at the root compare target to element at current node move left from current node if target is less than element in the current node move right from current node if target is greater than element in the current node We eventually find the target or encounter the end of a path (target is not found) Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Binary Search Trees The particular shape of a binary search tree depends on the order in which the elements are added The shape may also be dependant on any additional processing performed on the tree to reshape it Binary search trees can hold any type of data, so long as we have a way to determine relative ordering Objects implementing the Comparable interface provide such capability Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Binary Search Trees Process of adding an element is similar to finding an element New elements are added as leaf nodes Start at the root, follow path dictated by existing elements until you find no child in the desired direction Then add the new element Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Binary Search Trees Adding elements to a BST: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Binary Search Trees BST operations: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

package jsjf; /** * BinarySearchTreeADT defines the interface to a binary search tree. * * @author Java Foundations * @version 4.0 */ public interface BinarySearchTreeADT<T> extends BinaryTreeADT<T> { * Adds the specified element to the proper location in this tree. * @param element the element to be added to this tree public void addElement(T element); * Removes and returns the specified element from this tree. * @param targetElement the element to be removed from the tree * @return the element to be removed from the tree public T removeElement(T targetElement); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/. Removes all occurences of the specified element from this tree /** * Removes all occurences of the specified element from this tree. * * @param targetElement the element to be removed from the tree */ public void removeAllOccurrences(T targetElement); * Removes and returns the smallest element from this tree. * @return the smallest element from the tree. public T removeMin(); * Removes and returns the largest element from this tree. * @return the largest element from the tree public T removeMax(); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/. Returns the smallest element in this tree without removing it /** * Returns the smallest element in this tree without removing it. * * @return the smallest element in the tree */ public T findMin(); * Returns the largest element in this tree without removing it. * @return the largest element in the tree public T findMax(); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Binary Search Trees Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

package jsjf; import jsjf. exceptions. ; import jsjf. ; / package jsjf; import jsjf.exceptions.*; import jsjf.*; /** * LinkedBinarySearchTree implements the BinarySearchTreeADT interface * with links. * * @author Java Foundations * @version 4.0 */ public class LinkedBinarySearchTree<T> extends LinkedBinaryTree<T> implements BinarySearchTreeADT<T> { * Creates an empty binary search tree. public LinkedBinarySearchTree() super(); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/. Creates a binary search with the specified element as its root /** * Creates a binary search with the specified element as its root. * * @param element the element that will be the root of the new binary * search tree */ public LinkedBinarySearchTree(T element) { super(element); if (!(element instanceof Comparable)) throw new NonComparableElementException("LinkedBinarySearchTree"); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/. Adds the specified object to the binary search tree in the /** * Adds the specified object to the binary search tree in the * appropriate position according to its natural order. Note that * equal elements are added to the right. * * @param element the element to be added to the binary search tree */ public void addElement(T element) { if (!(element instanceof Comparable)) throw new NonComparableElementException("LinkedBinarySearchTree"); Comparable<T> comparableElement = (Comparable<T>)element; if (isEmpty()) root = new BinaryTreeNode<T>(element); else if (comparableElement.compareTo(root.getElement()) < 0) if (root.getLeft() == null) this.getRootNode().setLeft(new BinaryTreeNode<T>(element)); addElement(element, root.getLeft()); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

else { if (root. getRight() == null) this. getRootNode() else { if (root.getRight() == null) this.getRootNode().setRight(new BinaryTreeNode<T>(element)); addElement(element, root.getRight()); } modCount++; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/. Adds the specified object to the binary search tree in the /** * Adds the specified object to the binary search tree in the * appropriate position according to its natural order. Note that * equal elements are added to the right. * * @param element the element to be added to the binary search tree */ private void addElement(T element, BinaryTreeNode<T> node) { Comparable<T> comparableElement = (Comparable<T>)element; if (comparableElement.compareTo(node.getElement()) < 0) if (node.getLeft() == null) node.setLeft(new BinaryTreeNode<T>(element)); else addElement(element, node.getLeft()); } if (node.getRight() == null) node.setRight(new BinaryTreeNode<T>(element)); addElement(element, node.getRight()); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

BST Element Removal Removing a target in a BST is not as simple as that for linear data structures After removing the element, the resulting tree must still be valid Three distinct situations must be considered when removing an element The node to remove is a leaf The node to remove has one child The node to remove has two children Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

BST Element Removal Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

BST Element Removal Dealing with the situations Node is a leaf: it can simply be deleted Node has one child: the deleted node is replaced by the child Node has two children: an appropriate node is found lower in the tree and used to replace the node Good choice: inorder successor (node that would follow the removed node in an inorder traversal) The inorder successor is guaranteed not to have a left child Thus, removing the inorder successor to replace the deleted node will result in one of the first two situations (it’s a leaf or has one child) Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/. Removes the first element that matches the specified target /** * Removes the first element that matches the specified target * element from the binary search tree and returns a reference to * it. Throws a ElementNotFoundException if the specified target * element is not found in the binary search tree. * * @param targetElement the element being sought in the binary search tree * @throws ElementNotFoundException if the target element is not found */ public T removeElement(T targetElement) throws ElementNotFoundException { T result = null; if (isEmpty()) throw new ElementNotFoundException("LinkedBinarySearchTree"); else BinaryTreeNode<T> parent = null; if (((Comparable<T>)targetElement).equals(root.element)) result = root.element; BinaryTreeNode<T> temp = replacement(root); if (temp == null) root = null; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

else { root.element = temp.element; root.setRight(temp.right); root.setLeft(temp.left); } modCount--; parent = root; if (((Comparable)targetElement).compareTo(root.element) < 0) result = removeElement(targetElement, root.getLeft(), parent); result = removeElement(targetElement, root.getRight(), parent); return result; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/. Removes the first element that matches the specified target /** * Removes the first element that matches the specified target * element from the binary search tree and returns a reference to * it. Throws a ElementNotFoundException if the specified target * element is not found in the binary search tree. * * @param targetElement the element being sought in the binary search tree * @param node the node from which to search * @param parent the parent of the node from which to search * @throws ElementNotFoundException if the target element is not found */ private T removeElement(T targetElement, BinaryTreeNode<T> node, BinaryTreeNode<T> parent) throws ElementNotFoundException { T result = null; if (node == null) throw new ElementNotFoundException("LinkedBinarySearchTree"); else if (((Comparable<T>)targetElement).equals(node.element)) result = node.element; BinaryTreeNode<T> temp = replacement(node); if (parent.right == node) parent.right = temp; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

else parent.left = temp; modCount--; } { parent = node; if (((Comparable)targetElement).compareTo(node.element) < 0) result = removeElement(targetElement, node.getLeft(), parent); result = removeElement(targetElement, node.getRight(), parent); return result; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/. Returns a reference to a node that will replace the one /** * Returns a reference to a node that will replace the one * specified for removal. In the case where the removed node has * two children, the inorder successor is used as its replacement. * * @param node the node to be removed * @return a reference to the replacing node */ private BinaryTreeNode<T> replacement(BinaryTreeNode<T> node) { BinaryTreeNode<T> result = null; if ((node.left == null) && (node.right == null)) result = null; else if ((node.left != null) && (node.right == null)) result = node.left; else if ((node.left == null) && (node.right != null)) result = node.right; else BinaryTreeNode<T> current = node.right; BinaryTreeNode<T> parent = node; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

while (current. left. = null) { parent = current; current = current while (current.left != null) { parent = current; current = current.left; } current.left = node.left; if (node.right != current) parent.left = current.right; current.right = node.right; result = current; return result; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

BST Element Removal Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/. Removes elements that match the specified target element from /** * Removes elements that match the specified target element from * the binary search tree. Throws a ElementNotFoundException if * the sepcified target element is not found in this tree. * * @param targetElement the element being sought in the binary search tree * @throws ElementNotFoundException if the target element is not found */ public void removeAllOccurrences(T targetElement) throws ElementNotFoundException { removeElement(targetElement); try while (contains((T)targetElement)) } catch (Exception ElementNotFoundException) Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/. Removes the node with the least value from the binary search /** * Removes the node with the least value from the binary search * tree and returns a reference to its element. Throws an * EmptyCollectionException if this tree is empty. * * @return a reference to the node with the least value * @throws EmptyCollectionException if the tree is empty */ public T removeMin() throws EmptyCollectionException { T result = null; if (isEmpty()) throw new EmptyCollectionException("LinkedBinarySearchTree"); else if (root.left == null) result = root.element; root = root.right; } BinaryTreeNode<T> parent = root; BinaryTreeNode<T> current = root.left; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

while (current. left. = null) { parent = current; current = current while (current.left != null) { parent = current; current = current.left; } result = current.element; parent.left = current.right; modCount--; return result; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

BST Element Removal Removing the minimum element: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Using BSTs to Implement Ordered Lists Recall the list operations: And specifically for ordered lists: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase package jsjf; import jsjf.exceptions.*; import java.util.Iterator; /** * BinarySearchTreeList represents an ordered list implemented using a binary * search tree. * * @author Java Foundations * @version 4.0 */ public class BinarySearchTreeList<T> extends LinkedBinarySearchTree<T> implements ListADT<T>, OrderedListADT<T>, Iterable<T> { * Creates an empty BinarySearchTreeList. public BinarySearchTreeList() super(); } * Adds the given element to this list. * @param element the element to be added to the list public void add(T element) addElement(element); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase /** * Removes and returns the first element from this list. * * @return the first element in the list */ public T removeFirst() { return removeMin(); } * Removes and returns the last element from this list. * @return the last element from the list public T removeLast() return removeMax(); * Removes and returns the specified element from this list. * @param element the element being sought in the list * @return the element from the list that matches the target public T remove(T element) return removeElement(element); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase /** * Returns a reference to the first element on this list. * * @return a reference to the first element in the list */ public T first() { return findMin(); } * Returns a reference to the last element on this list. * @return a reference to the last element in the list public T last() return findMax(); * Returns an iterator for the list. * @return an iterator over the elements in the list public Iterator<T> iterator() return iteratorInOrder(); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Ordered List Analysis Comparing operations for both implementations of ordered lists: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balancing BSTs As operations are performed on a BST, it could become highly unbalanced (a degenerate tree) Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balancing BSTs Our implementation does not ensure the BST stays balanced Other approaches do, such as AVL trees and red/black trees We will explore rotations – operations on binary search trees to assist in the process of keeping a tree balanced Rotations do not solve all problems created by unbalanced trees, but show the basic algorithmic processes that are used to manipulate trees Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balancing BSTs A right rotation can be performed at any level of a tree, around the root of any subtree Corrects an imbalance caused by a long path in the left subtree of the left child of the root Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balancing BSTs A right rotation: To correct the imbalance make the left child element of the root the new root element make the former root element the right child element of the new root make the right child of what was the left child of the former root the new left child of the former root Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balancing BSTs A left rotation can be performed at any level of a tree, around the root of any subtree Corrects an imbalance caused by a long path in the right subtree of the right child of the root Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balancing BSTs A left rotation: To correct the imbalance make the right child element of the root the new root element make the former root element the left child element of the new root make the left child of what was the right child of the former root the new right child of the former root Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balancing BSTs If the imbalance is caused by a long path in the left subtree of the right child of the root we can address it by performing a right - left rotation: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balancing BSTs A right-left rotation: Perform a right rotation around the offending subtree and then perform a left rotation around the root Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balancing BSTs If the imbalance is caused by a long path in the right subtree of the left child of the root we can address it by performing a left-right rotation: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balancing BSTs A left-right rotation: Perform a left rotation around the offending subtree and then performing a right rotation around the root Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

AVL Trees An AVL tree (named after the creators) ensures a BST stays balanced For each node in the tree, balance factor is calculated as the difference between the height of the right subtree minus the height of the left subtree If the balance factor is greater than 1 or less than -1, then the subtree with that node as the root is rebalanced. After each add or removal, the balance factors are checked, and rotations performed as needed Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

AVL Trees A right rotation in an AVL tree: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

A right-left rotation in an ALV tree: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase