1 CSC 321: Data Structures Fall 2012 Binary Search Trees  BST property  override binary tree methods: add, contains  search efficiency  balanced trees:

Slides:



Advertisements
Similar presentations
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 Divide & conquer  divide-and-conquer approach  familiar examples: binary search, merge sort,
Advertisements

1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Inheritance and efficiency  ArrayList  SortedArrayList  tradeoffs with adding/searching.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2004 Heaps and heap sort  complete tree, heap property  min-heaps & max-heaps  heap operations:
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
© 2006 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 Tables and Priority Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
1 CSC 421: Algorithm Design Analysis Spring 2014 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
1 CSC 421: Algorithm Design and Analysis Spring /222/321 review  object-oriented design  cohesion, coupling  interfaces, inheritance, polymorphism.
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
1 CSC 421: Algorithm Design & Analysis Spring 2013 Divide & conquer  divide-and-conquer approach  familiar examples: merge sort, quick sort  other examples:
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Applications:  scheduling applications priority queue, java.util.PriorityQueue min-heaps &
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Dictionaries CS /02/05 L7: Dictionaries Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 Java Collections & List implementations  Collection classes: ─List (ArrayList, LinkedList),
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 TreeSets and TreeMaps  tree structure, root, leaves  recursive tree algorithms: counting,
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2004 Trees and nonlinear structures  tree structure, root, leaves  recursive tree algorithms:
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
CS 367 Introduction to Data Structures Lecture 8.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Tables and Priority Queues
CSC 427: Data Structures and Algorithm Analysis
CSC 421: Algorithm Design Analysis
CSC 421: Algorithm Design Analysis
CSC 321: Data Structures Fall 2016 Trees & recursion
CSC 321: Data Structures Fall 2016 Balanced and other trees
CSC 427: Data Structures and Algorithm Analysis
CSC 427: Data Structures and Algorithm Analysis
CSC 427: Data Structures and Algorithm Analysis
CSC 222: Object-Oriented Programming
Efficiency of in Binary Trees
CSC 321: Data Structures Fall 2015 Binary Search Trees BST property
CSC 321: Data Structures Fall 2017 Trees & recursion
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CSE 373: Data Structures and Algorithms
Lecture 12 CS203 1.
CSC 321: Data Structures Fall 2018 Trees & recursion
CSE 373 Data Structures and Algorithms
Heaps and Priority Queues
CSC 143 Binary Search Trees.
Data Structures II AP Computer Science
Trees.
CSC 321: Data Structures Fall 2018 Balanced and other trees
Presentation transcript:

1 CSC 321: Data Structures Fall 2012 Binary Search Trees  BST property  override binary tree methods: add, contains  search efficiency  balanced trees: AVL, red-black  heaps, priority queues, heap sort

2 Searching linked lists recall: a (linear) linked list only provides sequential access  O(N) searches it is possible to obtain O(log N) searches using a tree structure in order to perform binary search efficiently, must be able to  access the middle element of the list in O(1)  divide the list into halves in O(1) and recurse HOW CAN WE GET THIS FUNCTIONALITY FROM A TREE?

3 Binary search trees a binary search tree is a binary tree in which, for every node:  the item stored at the node is ≥ all items stored in its left subtree  the item stored at the node is < all items stored in its right subtree in a (balanced) binary search tree: middle element = root 1 st half of list = left subtree 2 nd half of list = right subtree furthermore, these properties hold for each subtree

BinarySearchTree class public class BinarySearchTree > extends BinaryTree { public BinarySearchTree() { super(); } public void add(E value) { // OVERRIDE TO MAINTAIN BINARY SEARCH TREE PROPERTY } public void CONTAINS(E value) { // OVERRIDE TO TAKE ADVANTAGE OF BINARY SEARCH TREE PROPERTY } public void remove(E value) { // DOES THIS NEED TO BE OVERRIDDEN? } 4 can use inheritance to derive BinarySearchTree from BinaryTree

5 Binary search in BSTs to search a binary search tree: 1.if the tree is empty, NOT FOUND 2.if desired item is at root, FOUND 3.if desired item < item at root, then recursively search the left subtree 4.if desired item > item at root, then recursively search the right subtree public boolean contains(E value) { return this.contains(this.root, value); } private boolean contains(TreeNode current, E value) { if (current == null) { return false; } else if (value.equals(current.getData())) { return true; } else if (value.compareTo(current.getData()) < 0) { return this.contains(current.getLeft(), value); } else { return this.contains(current.getRight(), value); }

6 Search efficiency how efficient is search on a BST?  in the best case? O(1)if desired item is at the root  in the worst case? O(height of the tree) if item is leaf on the longest path from the root in order to optimize worst-case behavior, want a (relatively) balanced tree  otherwise, don't get binary reduction  e.g., consider two trees, each with 7 nodes

7 Search efficiency (cont.) we showed that N nodes can be stored in a binary tree of height  log 2 (N+1)  so, in a balanced binary search tree, searching is O(log N) N nodes  height of  log2(N+1)   in worst case, have to traverse  log2(N+1)  nodes what about the average-case efficiency of searching a binary search tree?  assume that a search for each item in the tree is equally likely  take the cost of searching for each item and average those costs costs of search  17/7  2.42 define the weight of a tree to be the sum of all node depths (root = 1, …) average cost of searching a BST = weight of tree / number of nodes in tree

8 Search efficiency (cont.) costs of search  17/7  2.42 ~log N costs of search  28/7  4.00 ~N/2

9 Inserting an item inserting into a BST 1.traverse edges as in a search 2.when you reach a leaf, add the new node below it public void add(E value) { this.root = this.add(this.root, value); } private TreeNode add(TreeNode current, E value) { if (current == null) { return new TreeNode (value, null, null); } if (value.compareTo(current.getData()) <= 0) { current.setLeft(this.add(current.getLeft(), value)); } else { current.setRight(this.add(current.getRight(), value)); } return current; }

10 Removing an item recall BinaryTree remove 1.find node (as in search) 2.if a leaf, simply remove it 3.if no left subtree, reroute parent pointer to right subtree 4.otherwise, replace current value with a leaf value from the left subtree (and remove the leaf node) CLAIM: as long as you select the rightmost (i.e., maximum) value in the left subtree, this remove algorithm maintains the BST property WHY? so, no need to override remove

11 Maintaining balance PROBLEM: random insertions (and removals) do not guarantee balance  e.g., suppose you started with an empty tree & added words in alphabetical order braves, cubs, giants, phillies, pirates, reds, rockies, … with repeated insertions/removals, can degenerate so that height is O(N)  specialized algorithms exist to maintain balance & ensure O(log N) height  or take your chances braves cubs giants phillies

12 Balancing trees on average, N random insertions into a BST yields O(log N) height  however, degenerative cases exist (e.g., if data is close to ordered) we can ensure logarithmic depth by maintaining balance maintaining full balance can be costly  however, full balance is not needed to ensure O(log N) operations

13 AVL trees an AVL tree is a binary search tree where  for every node, the heights of the left and right subtrees differ by at most 1  first self-balancing binary search tree variant  named after Adelson-Velskii & Landis (1962) AVL tree not an AVL tree – WHY?

14 AVL trees and balance the AVL property is weaker than full balance, but sufficient to ensure logarithmic height  height of AVL tree with N nodes < 2 log(N+2)  searching is O(log N)

15 Inserting/removing from AVL tree when you insert or remove from an AVL tree, imbalances can occur  if an imbalance occurs, must rotate subtrees to retain the AVL property  see

16 AVL tree rotations worst case, inserting/removing requires traversing the path back to the root and rotating at each level  each rotation is a constant amount of work  inserting/removing is O(log N) there are two possible types of rotations, depending upon the imbalance caused by the insertion/removal

17 Red-black trees a red-black tree is a binary search tree in which each node is assigned a color (either red or black) such that 1.the root is black 2.a red node never has a red child 3.every path from root to leaf has the same number of black nodes  add & remove preserve these properties (complex, but still O(log N))  red-black properties ensure that tree height < 2 log(N+1)  O(log N) search see a demo at gauss.ececs.uc.edu/RedBlack/redblack.htmlgauss.ececs.uc.edu/RedBlack/redblack.html

18 Java Collection classes recall the Java Collection Framework  defined using interfaces abstract classes, and inheritance in some languages, a Map is referred to as an "associative list" or "dictionary" arraydoubly- linked list red-black tree hash table

19 Sets java.util.Set interface: an unordered collection of items, with no duplicates public interface Set extends Collection { boolean add(E o);// adds o to this Set boolean remove(Object o);// removes o from this Set boolean contains(Object o);// returns true if o in this Set boolean isEmpty();// returns true if empty Set int size();// returns number of elements void clear();// removes all elements Iterator iterator();// returns iterator... } implemented by TreeSet and TreeMap classes TreeSet implementation implemented using a red-black tree; items stored in the nodes (must be Comparable) provides O(log N) add, remove, and contains (guaranteed) iteration over a TreeSet accesses the items in order (based on compareTo) HashSet implementation HashSet utlizes a hash table data structure LATER HashSet provides O(1) add, remove, and contains (on average, but can degrade)

20 Dictionary revisited note: our Dictionary class could have been implemented using a Set  Strings are Comparable, so could use either implementation  TreeSet has the advantage that iterating over the Set elements gives them in order (here, alphabetical order) import java.util.Set; import java.util.TreeSet; import java.util.Scanner; import java.io.File; public class Dictionary { private Set words; public Dictionary() { this.words = new TreeSet (); } public Dictionary(String filename) { this(); try { Scanner infile = new Scanner(new File(filename)); while (infile.hasNext()) { String nextWord = infile.next(); this.add(nextWord); } catch (java.io.FileNotFoundException e) { System.out.println("FILE NOT FOUND"); } public void add(String newWord) { this.words.add(newWord.toLowerCase()); } public void remove(String oldWord) { this.words.remove(oldWord.toLowerCase()); } public boolean contains(String testWord) { return this.words.contains(testWord.toLowerCase()); }

21 Maps java.util.Map interface: a collection of key  value mappings public interface Map { boolean put(K key, V value);// adds key  value to Map V remove(Object key);// removes key  ? entry from Map V get(Object key);// returns true if o in this Set boolean containsKey(Object key); // returns true if key is stored boolean containsValue(Object value); // returns true if value is stored boolean isEmpty();// returns true if empty Set int size();// returns number of elements void clear();// removes all elements Set keySet();// returns set of all keys... } implemented by TreeMap and HashMap classes TreeMap implementation utilizes a red-black tree to store key/value pairs; ordered by the (Comparable) keys provides O(log N) put, get, and containsKey (guaranteed) keySet() returns a TreeSet, so iteration over the keySet accesses the key in order HashMap implementation HashSet utlizes a HashSet to store key/value pairsLATER HashSet provides O(1) put, get, and containsKey (on average, but can degrade)

22 Word frequencies a variant of Dictionary is WordFreq  stores words & their frequencies (number of times they occur)  can represent the word  counter pairs in a Map  again, could utilize either Map implementation  since TreeMap is used, showAll displays words + counts in alphabetical order import java.util.Map; import java.util.TreeMap; import java.util.Scanner; import java.io.File; public class WordFreq { private Map words; public WordFreq() { words = new TreeMap (); } public WordFreq(String filename) { this(); try { Scanner infile = new Scanner(new File(filename)); while (infile.hasNext()) { String nextWord = infile.next(); this.add(nextWord); } catch (java.io.FileNotFoundException e) { System.out.println("FILE NOT FOUND"); } public void add(String newWord) { String cleanWord = newWord.toLowerCase(); if (words.containsKey(cleanWord)) { words.put(cleanWord, words.get(cleanWord)+1); } else { words.put(cleanWord, 1); } public void showAll() { for (String str : words.keySet()) { System.out.println(str + ": " + words.get(str)); }

Other tree structures a heap is a common tree structure that:  can efficiently implement a priority queue (a list of items that are accessed based on some ranking or priority as opposed to FIFO/LIFO)  can also be used to implement another O(N log N) sort 23 motivation: many real-world applications involve optimal scheduling  choosing the next in line at the deli  prioritizing a list of chores  balancing transmission of multiple signals over limited bandwidth  selecting a job from a printer queue  multiprogramming/multitasking all these applications require  storing a collection of prioritizable items, and  selecting and/or removing the highest priority item

24 Priority queue priority queue is the ADT that encapsulates these 3 operations: add item (with a given priority) find highest priority item remove highest priority item e.g., assume printer jobs are given a priority 1-5, with 1 being the most urgent a priority queue can be implemented in a variety of ways  unsorted list efficiency of add? efficiency of find? efficiency of remove?  sorted list (sorted by priority) efficiency of add? efficiency of find? efficiency of remove?  others? job1 3 job 2 4 job 3 1 job 4 4 job 5 2 job4 4 job 2 4 job 1 3 job 5 2 job 3 1

25 java.util.PriorityQueue Java provides a PriorityQueue class public class PriorityQueue > { /** Constructs an empty priority queue */ public PriorityQueue () { … } /** Adds an item to the priority queue (ordered based on compareTo) newItem the item to be added true if the items was added successfully */ public boolean add(E newItem) { … } /** Accesses the smallest item from the priority queue (based on compareTo) the smallest item */ public E peek() { … } /** Accesses and removes the smallest item (based on compareTo) the smallest item */ public E remove() { … } public int size() { … } public void clear() { … }... } the underlying data structure is a special kind of binary tree called a heap

26 Heaps a complete tree is a tree in which  all leaves are on the same level or else on 2 adjacent levels  all leaves at the lowest level are as far left as possible a heap is complete binary tree in which  for every node, the value stored is  the values stored in both subtrees (technically, this is a min-heap -- can also define a max-heap where the value is  ) since complete, a heap has minimal height =  log 2 N  +1  can insert in O(height) = O(log N), but searching is O(N)  not good for general storage, but perfect for implementing priority queues can access min value in O(1), remove min value in O(height) = O(log N)

27 Inserting into a heap note: insertion maintains completeness and the heap property  worst case, if add smallest value, will have to swap all the way up to the root  but only nodes on the path are swapped  O(height) = O(log N) swaps to insert into a heap  place new item in next open leaf position  if new value is smaller than parent, then swap nodes  continue up toward the root, swapping with parent, until smaller parent found see add 30

28 Removing from a heap note: removing root maintains completeness and the heap property  worst case, if last value is largest, will have to swap all the way down to leaf  but only nodes on the path are swapped  O(height) = O(log N) swaps to remove the min value (root) of a heap  replace root with last node on bottom level  if new root value is greater than either child, swap with smaller child  continue down toward the leaves, swapping with smaller child, until smallest see

29 Implementing a heap a heap provides for O(1) find min, O(log N) insertion and min removal  also has a simple, List-based implementation  since there are no holes in a heap, can store nodes in an ArrayList, level-by-level  root is at index 0  last leaf is at index size()-1  for a node at index i, children are at 2*i+1 and 2*i+2  to add at next available leaf, simply add at end

30 MinHeap class import java.util.ArrayList; public class MinHeap > { private ArrayList values; public MinHeap() { this.values = new ArrayList (); } public E minValue() { if (this.values.size() == 0) { throw new java.util.NoSuchElementException(); } return this.values.get(0); } public void add(E newValue) { this.values.add(newValue); int pos = this.values.size()-1; while (pos > 0) { if (newValue.compareTo(this.values.get((pos-1)/2)) < 0) { this.values.set(pos, this.values.get((pos-1)/2)); pos = (pos-1)/2; } else { break; } this.values.set(pos, newValue); }... we can define our own simple min-heap implementation minValue returns the value at index 0 add places the new value at the next available leaf (i.e., end of list), then moves upward until in position

31 MinHeap class (cont.)... public void remove() { E newValue = this.values.remove(this.values.size()-1); int pos = 0; if (this.values.size() > 0) { while (2*pos+1 < this.values.size()) { int minChild = 2*pos+1; if (2*pos+2 < this.values.size() && this.values.get(2*pos+2).compareTo(this.values.get(2*pos+1)) < 0) { minChild = 2*pos+2; } if (newValue.compareTo(this.values.get(minChild)) > 0) { this.values.set(pos, this.values.get(minChild)); pos = minChild; } else { break; } this.values.set(pos, newValue); } remove removes the last leaf (i.e., last index), copies its value to the root, and then moves downward until in position

32 Heap sort the priority queue nature of heaps suggests an efficient sorting algorithm  start with the ArrayList to be sorted  construct a heap out of the elements  repeatedly, remove min element and put back into the ArrayList  N items in list, each insertion can require O(log N) swaps to reheapify  construct heap in O(N log N)  N items in heap, each removal can require O(log N) swap to reheapify  copy back in O(N log N) public static > void heapSort(ArrayList items) { MinHeap itemHeap = new MyMinHeap (); for (int i = 0; i < items.size(); i++) { itemHeap.add(items.get(i)); } for (int i = 0; i < items.size(); i++) { items.set(i, itemHeap.minValue()); itemHeap.remove(); } thus, overall efficiency is O(N log N), which is as good as it gets!  can also implement so that the sorting is done in place, requires no extra storage