Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees"— Presentation transcript:

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

2 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.

3 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

4 2-3 tree insertion example
More examples of insertion: Add 10, 11, 12, … to the last tree

5 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 h ≤ lg(N+1) -1. Lower bound. Full ternary tree. Two items in each node N <= 2( … + 3h) = 3h So h ≥ log3(N+1) -1

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

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

8 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.

9 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): Draw the heap rom previous slide on the board, insert 8, then 11; removeMax a couple of times. Then show code on next slide

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

11 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]

12 HeapSort Code

13 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])?


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

Similar presentations


Ads by Google