CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

Similar documents
Lecture 23: Binary Search Trees

Algorithms. AVL Tree

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Self-Balancing Search Trees. Chapter 11

CS Transform-and-Conquer

CISC 235: Topic 4. Balanced Binary Search Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Balanced Search Trees. CS 3110 Fall 2010

Lecture 6: Analysis of Algorithms (CS )

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

Algorithms. Deleting from Red-Black Trees B-Trees

CHAPTER 10 AVL TREES. 3 8 z 4

CS 380 ALGORITHM DESIGN AND ANALYSIS

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Fundamental Algorithms

Search Trees - 1 Venkatanatha Sarma Y

Search Trees - 2. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore. M.S Ramaiah School of Advanced Studies - Bangalore

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Balanced search trees. DS 2017/2018

Properties of red-black trees

Data Structures in Java

Splay Trees. (Splay Trees) Data Structures and Programming Spring / 27

Analysis of Algorithms

Binary Search Trees. Analysis of Algorithms

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

CSI33 Data Structures

Balanced Binary Search Trees. Victor Gao

Trees. Eric McCreath

AVL Trees. Version of September 6, AVL Trees Version of September 6, / 22

(2,4) Trees. 2/22/2006 (2,4) Trees 1

CS350: Data Structures Red-Black Trees

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Search Trees. COMPSCI 355 Fall 2016

13.4 Deletion in red-black trees

COMP Analysis of Algorithms & Data Structures

Dynamic Access Binary Search Trees

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

Balanced search trees

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

COMP Analysis of Algorithms & Data Structures

Lecture: Analysis of Algorithms (CS )

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Ch04 Balanced Search Trees

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

CS102 Binary Search Trees

Balanced Binary Search Trees

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

CS 206 Introduction to Computer Science II

Part 2: Balanced Trees

COMP171. AVL-Trees (Part 1)

Dynamic Access Binary Search Trees

Advanced Set Representation Methods

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Multiway Search Trees. Multiway-Search Trees (cont d)

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Recall: Properties of B-Trees

CMPS 2200 Fall 2015 Red-black trees Carola Wenk

Section 1: True / False (1 point each, 15 pts total)

CS 3343 Fall 2007 Red-black trees Carola Wenk

Data Structure: Search Trees 2. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Data Structures and Algorithms CMPSC 465

Red-Black Trees. 2/24/2006 Red-Black Trees 1

Algorithms, Spring 2014, CSE, OSU Lecture 5: Red-black trees

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

Search Trees. Undirected graph Directed graph Tree Binary search tree

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

AVL Trees (10.2) AVL Trees

Lesson 21: AVL Trees. Rotation

13.4 Deletion in red-black trees

Binary Heaps in Dynamic Arrays

Inf 2B: AVL Trees. Lecture 5 of ADS thread. Kyriakos Kalorkoti. School of Informatics University of Edinburgh

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

CS 310: Tree Rotations and AVL Trees

CIS265/ Trees Red-Black Trees. Some of the following material is from:

COMP Analysis of Algorithms & Data Structures

Transform & Conquer. Presorting

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

Binary search trees (chapters )

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

3137 Data Structures and Algorithms in C++

CS 261 Data Structures. AVL Trees

CS350: Data Structures AVL Trees

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

Binary search trees (chapters )

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

Search Trees (Ch. 9) > = Binary Search Trees 1

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Data Structures and Algorithms

More BSTs & AVL Trees bstdelete

AVL Trees Heaps And Complexity

DATA STRUCTURES AND ALGORITHMS

Multi-Way Search Trees

CSC148 Week 7. Larry Zhang

AVL Trees. See Section 19.4of the text, p

Algorithms for Memory Hierarchies Lecture 2

Algorithms and Data Structures

Transcription:

CSCI 136 Data Structures & Advanced Programming Lecture 25 Fall 2018 Instructor: B 2

Last Time Binary search trees (Ch 14) The locate method Further Implementation 2

Today s Outline Binary search trees (Ch 14) Implementation wrap-up Tree balancing to maintain small height AVL Trees Partial taxonomy of balanced tree species Red-Black Trees Splay Trees 3

Add: Repeated Nodes 4

Add Duplicate to Predecessor If insertlocation has a left child then Find insertlocation s predecessor Add repeated node as right child of predecessor Predecessor will be in insertlocation s left sub-tree Do you believe me? 5

Corrected Version: add(e value) BinaryTree<E> newnode = new BinaryTree<E>(value,EMPTY,EMPTY); if (root.isempty()) root = newnode; else { } BinaryTree<E> insertlocation = locate(root,value); E nodevalue = insertlocation.value(); if (ordering.compare(nodevalue,value) < 0) insertlocation.setright(newnode); else count++; if (insertlocation.left().isempty()) else insertlocation.setleft(newnode); // if value is in tree, we insert just before predecessor(insertlocation).setright(newnode); 6

How to Find Predecessor 7

Predecessor protected BinaryTree<E> predecessor(binarytree<e> root) { Assert.pre(!root.isEmpty(), Root has predecessor"); Assert.pre(!root.left().isEmpty(),"Root has left child."); BinaryTree<E> result = root.left(); while (!result.right().isempty()) result = result.right(); } return result; 8

Removal Removing the root is a (not so) special case Let s figure that out first If we can remove the root, we can remove any element in a BST in the same way Do you believe me? We need to implement: public E remove(e item) protected BT removetop(bt top) 9

Case 1: No left binary tree x x.right x.right 10

Case 2: No right binary tree x x.left x.left 11

Case 3: Left has no right subtree x.left x x.right x.left a.root A B a.root A x.right B 12

Case 4: General Case (HARD!) Consider BST requirements: Left subtree must be <= root Right subtree must be > root Strategy: replace the root with the largest value that is less than or equal to it predecessor(root) : rightmost left descendant This may require reattaching the predecessor s left subtree! 13

Case 4: General Case (HARD!) x 2 1 4 1 4 2 B B A A Replace root with predecessor(root), then patch up the remaining tree 14

Case 4: General Case (HARD!) x 3 1 2 4 1 2 4 3 D D A B A B C C Replace root with predecessor(root), then patch up the remaining tree 15

RemoveTop(topNode) Detach left and right sub-trees from root (i.e. topnode) If either left or right is empty, return the other one If left has no right child make right the right child of left then return left Otherwise find largest node C in left // C is the right child of its own parent P // C is the predecessor of right (ignoring topnode) Detach C from P; make C s left child the right child of P Make C new root with left and right as its sub-trees 16

But What About Height? Can we design a binary search tree that is always shallow? Yes! In many ways. Here s one AVL trees Named after its two inventors, G.M. Adelson- Velsky and E.M. Landis, who published a paper about AVL trees in 1962 called "An algorithm for the organization of information" 17

AVL Trees 18

AVL Trees Balance Factor of a binary tree node: height of right subtree minus height of left subtree. A node with balance factor 1, 0, or -1 is considered balanced. A node with any other balance factor is considered unbalanced and requires rebalancing the tree. Definition: An AVL Tree is a binary tree in which every node is balanced. 19

AVL Trees have O(log n) Height Theorem: An AVL tree on n nodes has height O(log n) Proof idea Show that an AVL tree of height h has at least fib(h) nodes (easy induction proof---try it!) Recall (HW):!"# h ( ( )) + if h 10 So, ( ( )) + and thus log 0 1, h Recall that for any 2, # > 0, log 6, = 89: ; < 89: ; 6 So log 6, and log =, are Big-O of one another So h is O(log n) 20

Single Rotation A +2 B +1 C 0 A 0 B 0 C 0 Unbalanced trees can be rotated to achieve balance. 21

Single Right Rotation 22

Double Rotation E -2 E -2 D 0 B 1 F 0 D -2 F 0 B 0 E +1 0 A D -1 C 0 B 0 A 0 C 0 A 0 C 0 F 0 23

AVL Tree Facts A tree that is AVL except at root, where root balance factor equals 2 can be rebalanced with at most 2 rotations add(v) requires at most O(log n) balance factor changes and one (single or double) rotation to restore AVL structure remove(v) requires at most O(log n) balance factor changes and (single or double) rotations to restore AVL structure An AVL tree on n nodes has height O(log n) 24

AVL Trees: One of Many There are many strategies for tree balancing to preserve O(log n) height, including AVL Trees: guaranteed O(log n) height Red-black trees: guaranteed O(log n) height B-trees (not binary): guaranteed O(log n) height 2-3 trees, 2-3-4 trees, red-black 2-3-4 trees,... Splay trees: Amortized O(log n) time operations Randomized trees: O(log n) expected height 25

A Red-Black Tree (from Wikipedia.org) 26

Red-Black Trees Red-Black trees, like AVL, guarantee shallowness Each node is colored red or black Coloring satisfies these rules All empty trees are black We consider them to be the leaves of the tree Children of red nodes are black All paths from a given node to it s descendent leaves have the same number of black nodes This is called the black height of the node 27

A Red-Black Tree (from Wikipedia.org) 28

Red-Black Trees The coloring rules lead to the following result Proposition: No leaf has depth more than twice that of any other leaf. This in turn can be used to show Theorem: A Red-Black tree with n internal nodes has height satisfying h 2 log(/ + 1) Note: The tree will have exactly n+1 (empty) leaves since each internal node has two children 29

Red-Black Trees Theorem: A Red-Black tree with n internal nodes has height satisfying h 2 log(/ + 1) Proof sketch: Note: we count empty tree nodes! If root is red, recolor it black. Now merge red children into (black) parents Now n n nodes and height h h/2 New tree has all children with degree 2, 3, or 4 All leaves have depth exactly h and there are n+1 leaves So / + 1 2 45, so log 6 / + 1 h 7 4 6 Thus 2 log 6 / + 1 h Corollary: R-B trees with n nodes have height O(log n) 30

Red-Black Tree Insertion 31

Red-Black Tree Insertion 32

Red-Black Tree Insertion 33

Red-Black Tree Insertion 34

Red-Black Tree Insertion 35