Chapter 14 Advanced Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
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.
Trees Chapter 8.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
A balanced life is a prefect life.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Trees and Red-Black Trees Gordon College Prof. Brinton.
© 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.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
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.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Chapter 12 B Priority Queues. © 2004 Pearson Addison-Wesley. All rights reserved 12 B-2 The ADT Priority Queue: A Variation of the ADT Table The ADT priority.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Priority Queues, Heaps & Leftist Trees
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
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:
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
© 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.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
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.
Symbol Tables and Search Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
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.
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.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Binary Search Trees Implementing Balancing Operations Hash Tables
AA Trees.
File Organization and Processing Week 3
Hashing Exercises.
Monday, April 16, 2018 Announcements… For Today…
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Part-D1 Priority Queues
Ch. 8 Priority Queues And Heaps
2-3-4 Trees Red-Black Trees
Advanced Implementation of Tables
Presentation transcript:

Chapter 14 Advanced Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Overview ● 14.1 – Heaps provide an efficient implementation of a new kind of queue. ● 14.2 – Trees to model a cluster of disjoint sets. ● 14.3 – Digital search trees provide a new way to store sets of Strings. ● 14.4 – Red-black trees, a variation on binary search trees, are guaranteed to remain balanced.

Heaps ● Heap – A binary tree. ● The value at each node is less than or equal to the values at the children of that node. ● The tree is perfect or close to perfect. – The requirement that a heap is “perfect or close to perfect” lets us use a contiguous representation somewhat analogous to the representation of multidimensional arrays from Section 12.3 – We use an ArrayList with the root at position 0, its children in the next two positions, their children in the next four.

A Sample Heap ● Find the relatives – The left child of the node at index i is at index 2i + 1. – The right child is at index 2i + 2 (e.g. nodes 5 & 10) – The parent is at index:

Heaps

Priority Queues ● A heap is a good data structure for implementing a priority queue. – We remove something from a regular queue and get the oldest element, because first-in first-out (FIFO). – A priority queue, we get the smallest element. – The smallest element in a heap: it's always at index 0.

Priority Queues (to add new node 3) ● Add something to a priority queue by tacking it onto the end. ● Filtering the element up toward the root until it is in a valid position. ● Even the worst case, this takes time proportional to the height of the tree. ● Since the trees is perfect or close to perfect, this is in O(log n) time.

Priority Queue - Code for adding an element into a priority queue

Priority Queue

Removing an Element from a Priority Queue ● For example, remove the root node from a priority queue (Fig 14-6) – Remove the current root node. – Move the element in the last position as the root and filtered it down until it is in a legitimate position. ● This takes O(log n) time

Priority Queue - Code for removing an element from a priority queue

Priority Queue

Heapsort (Fig. 14-8) ● A very useful sorting algorithm. ● Call the Heap constructor to use the input elements to create a heap. ● Note, the constructor on lines 1-13 in Fig is an overloaded constructor for the Heap class. The other was in Figure ● Call the heapsort method to remove the root (smallest) element from the heap, one at a time. ● Call FliterDown() to make the heap valid. ● The order to remove elements from the heap is in the ascending order (i.e. the output elements are in ascending order). ● Heapsort algorithm is O(n log n)

Heapsort

Heaps ● Java.util package contains a PriorityQueue class.

Disjoint Set Clusters ● Disjoint sets – Sets are disjoint if they have no elements in common. – Clusters of disjoint sets include the sets of players on different baseball teams, the set of cities in different countries, and the sets of newspapers owned by different media companies. – We want to allow more efficient performance by: ● Determining whether two elements belong to the same set. ● Merge two sets.

Disjoint Set Clusters ● up-tree – Cluster represents nodes in these trees keep track of their parents.

Disjoint Set Clusters ● To determine if two elements are in the same set, they lead to the same root, the elements are in the same tree.

Disjoint Set Clusters ● Represent the up-trees at each position we store the parent of the corresponding element.

Disjoint Set Clusters

● All of the methods take constant time except for findRoot() – Worst case Θ (n)

Disjoint Set Clusters ● We can keep the trees shorter by making the root of the shorter tree a child of the root of the taller tree.

Disjoint Set Clusters ● We need to keep track of the height of each tree. – We need to keep track of heights only for the roots. – Store the height of each root in the array parents. – To avoid confusion between a root with height 3 and a node whose parent is 3, we store the heights as negative numbers.

Disjoint Set Clusters

● A tree might have height 0 – The entry for the root of a tree of height h is -h-1

Disjoint Set Clusters ● Path Compression – A second improvement ● Suppose we determine that the root of the tree containing 4 is 7. ● We can make this operation faster next time by making 7 the parent of 4. ● In fact, we might as well make 7 the parent of every node we visit on the way to the root. ● We can't alter these parents until after we've found the root.

Disjoint Set Clusters

● O(log* n), log* n is the number of times we have to take the logarithm to get down to 1.

Disjoint Set Clusters

● The entire word set can be represented as a digital search tree. – Words are represented as paths through the tree. – Each child of node is associated with a letter.

Disjoint Set Clusters

● Whenever the user enters a letter, we descend to the appropriate child. – If there is no such child, the user loses. – When we need to pick a letter, we randomly choose a child and the corresponding letter.

Disjoint Set Clusters

● getChild() is nothing more than a has table lookup. – Makes a digital search tree an excellent choice when searching for Strings containing a prefix that grows one character at a time.

Red-Black Trees ● A plain binary search tree performs poorly if the tree is not balanced. – Worst case occurs if the elements are inserted in order, the tree is linear, so search, insertion, and deletion take linear time. – Red-black tree ● Ensures that the tree cannot become significantly imbalanced. – In the Java collections framework, the classes TreeSet and TreeMap use red-black trees.

Red-Black Trees ● Red-black tree is a binary search tree and the node has a color, either red or black. – The root is black. – No red node has a red child. – All paths must contain the same number of black nodes.

Red-Black Trees ● These properties ensure that the tree cannot be significatnly imbalanced. – The shortest path to a null child contains d nodes. – The longest path can contain at most 2d nodes. – Height of a red-black tree containing n nodes is in O(log n). – Search in red-black trees is identical to search in binary search trees.

Red-Black Trees ● Start at the root and descend until we either find the target or try to descend from a leaf. – In the latter case, the target is not present in the tree, so we attach a new, red leaf. – The new node may be a child of another red node. – We fix this by working our way back up the tree, changing colors and rearranging nodes. – Repair operation, complicated, time for insertion is still in O(log n).

Red-Black Trees ● A key step in tree repair is rotation. ● When we rotate, we replace a node with one of its children.

Red-Black Trees ● Work back up the tree in several steps, performing color changes and rotations. – Each step either fixes the tree or moves the problem closer to the root. – If we get the root and still have a red node, we can simply color it black. – How do we fix this? ● Three cases to consider, depending on the color of the node's parent's sibling and on whether node is a left or right child.

Red-Black Trees ● Node has a red aunt – Since the great-grandparent may also be red, we may have to do some repair there, too, but we're getting closer to the root.

Red-Black Trees ● Node has a black aunt. – Outer-child. ● No further work is necessary at this point.

Red-Black Trees ● Node has a black aunt and is an inner child – New outer child is red and has a red parent, so we can repair it as before.

Red-Black Trees ● Splicing out a red node can never cause any problems. – Let node be the child of the node that was spliced out. – If node is red, we can simply color it black to cancel out the problem. – If node is black, the subtree rooted at node's parent is short a black node.

Red-Black Trees ● Node's sibling is black and has two black children ● Repair closer to the root.

Red-Black Trees ● Node's sibling is black and the outer child is red – No further work is necessary at this point.

Red-Black Trees ● Node's sibling is black, has a black outer child and has a red inner child. – Leads to the previous case.

Red-Black Trees ● Fourth case node's sibling is red. – Lead to one of the other 3 cases.

Red-Black Trees ● We use references to a special black node called a sentinel. – Sentinel indicates that we can't go any farther. – We use a single sentinel instance to represent all nonexistent children. – We keep track of the parent of each node. – The root of a parent is the sentinel.

Red-Black Trees

Summary ● A heap is a binary tree data structure used either to represent a priority queue or in the heapsort algorithm. – A heap is a perfect binary tree or close to it. – The value at each node is less than or equal to the values at the node's children. – When changes are made to a heap, it is repaired by filtering the offending element up or down until it is in the right place.

Summary ● A cluster of disjoint sets may be represented as a forest of up-trees. – Two elements in the same set are in the same tree, which can be detected by following parents up to the root. – This data structure supports efficient algorithm for determining compression, these operations take amortized time in O(log*n) ● A set of strings may be represented as a digital search tree. – Each string corresponds to a path through the tree.

Summary ● Java's TreeSet and TreeMap classes use red- black trees, which are similar to binary search trees. – The colors in the tree must have certain properties, which guarantee that the tree cannot be badly out of balance. – Worst-case running time in O(log n) for search, insertion, and deletion. ● A special sentinel node is used in place of absent parents and children, where there would normally be null references.

Summary ● Search in red-black tree works just as it does in a binary search tree. – After the basic insertion and deletion operations, it may be necessary to repair the tree to satisfy the properties.

Chapter 14 Self-Study Homework ● Pages: 377 ● A. Do Exercise 14.1 ● B. Use heapsort to sort the following integer set {5, 2, 1, 4, 9, 8, 10, 7, 6, 3} in ascending order. 1. Use the input data to create a heap 2. Then output the root node one by one to sort the data set in ascending order. 3. Show the heap in step 1 (the heap with 10 integers). 4. Show the heap after output each integer (9 heaps totally)