MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
MA/CSSE 473 Day 25 Transform and Conquer Student questions?
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
MA/CSSE 473 Day 22 Binary Heaps Heapsort Answers to student questions Exam 2 Tuesday, Nov 4 in class.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Chapter 21 Binary Heap.
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees Student questions?
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
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.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
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.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
MA/CSSE 473 Day 23 Review of Binary Heaps and Heapsort
MA/CSSE 473 Day 20 Finish Josephus
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
AVL Trees CSE, POSTECH.
Trees Chapter 15.
COMP 53 – Week Fourteen Trees.
Heaps (8.3) CSE 2011 Winter May 2018.
Heaps, Heap Sort and Priority Queues
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
Data Structures & Algorithms
CS202 - Fundamental Structures of Computer Science II
Chapter 11: Multiway Search Trees
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
October 30th – Priority QUeues
Heapsort.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
Advanced Associative Structures
Priority Queues.
Ch 6: Heapsort Ming-Te Chi
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
CSE 373: Data Structures and Algorithms
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CS202 - Fundamental Structures of Computer Science II
B-Tree Insertions, Intro to Heaps
Priority Queues.
Heaps and the Heapsort Heaps and priority queues
"Teachers open the door, but you must enter by yourself. "
CS Data Structure: Heaps.
Priority Queues (Chapter 6.6):
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Lecture No.20 Data Structures Dr. Sohail Aslam
CS202 - Fundamental Structures of Computer Science II
Topic 5: Heap data structure heap sort Priority queue
More Trees B.Ramamurthy 4/6/2019 BR.
CSE 373: Data Structures and Algorithms
Heapsort Sorting in place
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Binary SearchTrees [CLRS] – Chap 12.
CSE 373 Priority queue implementation; Intro to heaps
2-3 Trees Extended tree. Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal.
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Priority Queues (Chapter 6):
การวิเคราะห์และออกแบบขั้นตอนวิธี
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees Heap Review: intro Student questions?

Review: Representation change: AVL Trees (what you should remember…) Named for authors of original paper, Adelson-Velskii and Landis (1962). An AVL tree is a height-balanced Binary Search Tree. A BST T is height balanced if T is empty, or if | height( TL ) - height( TR ) |  1, and TL and TR are both height-balanced. Show: Maximum height of an AVL tree with N nodes is (log N) How do we maintain balance after insertion? Exercise for later: Given a pointer to the root of an AVL tree with N nodes, find the height of the tree in log N time Details on balance codes and various rotations are in the CSSE 230 slides that are linked from the schedule page. Let's review that together Extra data to make this more efficient. Balance code in each node. This is an essential part of the AVL data structure.

Representation change: 2-3 trees Another approach to balanced trees Keeps all leaves on the same level Some non-leaf nodes have 2 keys and 3 subtrees Others are regular binary nodes. Easy to balance

2-3 tree insertion example More examples of insertion: http://www.cs.ucr.edu/cs14/cs14_06win/slides/2-3_trees_covered.pdf http://slady.net/java/bt/view.php?w=450&h=300 Add 10, 11, 12, … to the last tree

Efficiency of 2-3 tree insertion Upper and lower bounds on height of a tree with n elements? Worst case insertion and lookup times is proportional to the height of the tree. Upper bound: A full binary tree. So N ≥ 2h+1 -1. h ≤ lg(N+1) -1. Lower bound. Full ternary tree. Two items in each node N <= 2(1 + 3 + 32 + … + 3h) = 3h+1 -1. So h ≥ log3(N+1) -1

2-3 Tree insertion practice Insert 84 into this tree and show the resulting tree

2-3 Tree insertion practice Insert 84 into this tree and show the resulting tree

Binary (max) Heap Quick Review Representation change example See also Weiss, Chapter 21 (Weiss does min heaps) An almost-complete Binary Tree All levels, except possibly the last, are full On the last level all nodes are as far left as possible No parent is smaller than either of its children A great way to represent a Priority Queue Representing a binary heap as an array: My guess is that for most of you, you only need a quick refresher on the heap insert and removeMax algorithms, but that a subtle issue relating to heapSort analysis may have gone over your head.

Insertion and RemoveMax Insert at the next position (end of the array) to maintain an almost-complete tree, then "percolate up" within the tree to restore heap property. RemoveMax: Move last element of the heap to replace the root, then "percolate down" to restore heap property. Both operations are Ѳ(log n). Many more details (done for min-heaps): http://www.rose-hulman.edu/class/csse/csse230/201230/Slides/18-Heaps.pdf Draw the heap rom previous slide on the board, insert 8, then 11; removeMax a couple of times. Then show code on next slide

Heap utilitiy functions Code is on-line, linked from the schedule page

HeapSort Arrange array into a heap. (details next slide) for i = n downto 2: a[1]a[i], then "reheapify" a[1]..a[i-1]

HeapSort Code

Recap: HeapSort: Build Initial Heap Two approaches: for i = 2 to n percolateUp(i) for j = n/2 downto 1 percolateDown(j) Which is faster, and why? What does this say about overall big-theta running time for HeapSort? What is the depth of the node in position a[i]? Answer: floor(lg i) In a full tree, what is the relationship between Height(node[i]) and depth(node[i])?