Balanced binary search trees

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
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.
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.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
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:
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
Red-Black Trees an 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.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Binary Search Trees Chapter 7 Objectives
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 23 Red Black Tree Chapter 10 of textbook
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Balancing Binary Search Trees
Red Black Trees
CSIT 402 Data Structures II
Binary Search Tree (BST)
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
FLIPPED CLASSROOM ACTIVITY CONSTRUCTOR – USING EXISTING CONTENT
CO4301 – Advanced Games Development Week 10 Red-Black Trees continued
Balanced Trees (AVL and RedBlack)
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Binary Search Tree Chapter 10.
Summary of General Binary search tree
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees Motivations
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
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.
AVL Trees: AVL Trees: Balanced binary search tree
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Red-Black Trees.
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.
A Robust Data Structure
Lecture 12 CS203 1.
Algorithms and Data Structures Lecture VIII
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Binary Trees, Binary Search Trees
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
AVL-Trees (Part 1).
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
CSE 373 Data Structures Lecture 8
1 Lecture 13 CS2013.
Chapter 12&13: Binary Search Trees (BSTs)
Binary Trees, Binary Search Trees
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Presentation transcript:

Balanced binary search trees Red Black Trees Balanced binary search trees

Binary Trees Tree may be empty Each node may have 0, 1, or two children The time it takes to search the tree is related to the number of nodes {O(n) worst case} How can that be???? We can search an ordered array in O(lg n), but a binary tree has no order.

Binary Search Trees Same rules as a binary tree …PLUS for any given node: The key values are comparable All nodes in the left child tree have values less than the node All nodes in the right child tree have values greater than the node Duplicates Not allowed (all duplicate key values are ignored) May be in the left subtree (all left node key values are strictly not greater than a node’s key value) May be in the right subtree (all left node key values are strictly less than a node’s key value) Handled by increasing the count of duplicates for a given node. This is not really useful without additional programming

BST of Pool Balls

How do we delete 16? 11? 4?

Insertion of Data In-order is Problematic When the data is presented in order we get a right-leaning tree of depth n When the data is presented in reverse order we get a left-leaning tree of depth n Both have a search time on the order of O(n) which is not close to O(log n) How can we balance the BST? When we have no control over the data?

Search is Dependent Upon Tree Depth It takes O(depth) to search for a value (worst case) It should be possible to achieve a depth of log n This is a problem if the input data is presented in some order In order creates a strongly right-leaning tree with a depth of n Reverse order creates a strongly left-leaning tree with a depth of n Randomizing the input should give better results, but may not You cannot randomize the input if it is received one value at a time…oops.

Red Black Trees Adjustments to BST Every node is either red or black The root node is black (some implementations allow the root to be red) Every leaf node (nil) is black If a node is red, its children are black For each node, all simple paths from the node to descendant leaves contain the same number of black nodes

Effects of These Rules These simple rules have important side effects: Adding a node may require recoloring all ancestors up to, and including, the root Adding a node may require restructuring the tree Add a node and then do an addition fixup routine Deleting a node may require recoloring all ancestors Deleting a node may require restructuring the tree Delete a node and then do a deletion fixup routine All of these can be done in O(log n) time After all the recoloring and restructuring, the red-black properties are preserved A red black tree has a depth of no more than 2 (log n+1) no matter how the key values are input

A Graphical Demo of a Red Black Tree Watch for: Does the order the key values are added change the resulting tree? Is there a fix up phase after each add? Does the order the key values are deleted change the resulting tree? Is there a fix up phase after each delete If a key value is deleted and then immediately re-added, is the same tree re-constructed? After each add or delete Is the tree correctly rebalanced (red-black properties preserved)? Is the tree perfectly rebalanced?

DEMO!!! Things to explore: Does the input order matter? What happens if I delete a node? Do I get the same tree if I delete a node and then insert it back? https://www.cs.usfca.edu/~galles/visualization/Algorithms.html

What Good Are Red Black Trees? Provides a balanced search tree in reasonable time Searchable in at worst O( 2 lg n+1) Can add and delete keys (nodes) in a reasonable time A reasonable method to sort data that is presented one value at a time Sorts in close to O(n log n) Beats Insertion Sort if data is in reverse order Can insure a tree that can be searched close to the optimal time Can be used to build a priority tree by always deleting the lowest (highest) key value and allowing the tree to rebalance Might be useful as a data structure any time a search or sort is needed.

In Closing…. Binary Search Trees can greatly speed searching only if the BSTs are balanced Balanced BST can be searched in O(tree-depth) BST have an optimal depth of lg n+1, which requires a specific input order Red-Black Trees use color and simple rules to achieve a relatively balanced BST regardless of the input order The final Red-Black Tree depends heavily upon the input order Updates to a Red-Black Tree (including recoloring and restructuring) are relatively fast On the order of O(lg n) Usually no worse than O(depth) if done from the leaves to the root

Questions?