Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
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.
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and black antennas.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
A balanced life is a prefect life.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
Trees and Red-Black Trees Gordon College Prof. Brinton.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Euler paths and tours.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
Introduction to Algorithms Jiafen Liu Sept
October 16, Algorithms and Data Structures Lecture IX Simonas Šaltenis Aalborg University
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
CSIT 402 Data Structures II
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
2-3 Trees, Trees Red-Black Trees
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
1 More Trees Trees, Red-Black Trees, B Trees.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
CS 307 Fundamentals of Computer ScienceRed Black Trees 1 Topic 19 Red Black Trees "People in every direction No words exchanged No time to exchange And.
Red-Black Trees Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
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,
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
AA Trees.
File Organization and Processing Week 3
Week 7 - Friday CS221.
Red-Black Trees.
Week 11 - Friday CS221.
ITEC 2620M Introduction to Data Structures
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Advanced Associative Structures
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and Black.
Algorithms and Data Structures Lecture VIII
2-3-4 Trees Red-Black Trees
Red-Black Implementation of 2-3 Trees
Presentation transcript:

Week 8 - Wednesday

 What did we talk about last time?  Level order traversal  BST delete  2-3 trees

Balance in all things

 A 2-3 search tree is a data structure that maintains balance  It is actually a ternary tree, not a binary tree  A 2-3 tree is one of the following three things:  An empty tree (null)  A 2-node (like a BST node) with a single key, smaller data on its left and larger values on its right  A 3-node with two keys and three links, all key values smaller than the first key on the left, between the two keys in the middle, and larger than the second key on the right

 The key thing that keeps a 2-3 search tree balanced is that all leaves are on the same level  Only leaves have null links  Thus, the maximum depth is somewhere between the log 3 n (the best case, where all nodes are 3-nodes) and log 2 n (the worst case, where all nodes are 2-nodes)

 We build from the bottom up  Except for an empty tree, we never put a new node in a null link  Instead, you can add a new key to a 2-node, turning it into a 3-node  Adding a new key to a 3-node forces it to break into two 2-nodes

 Starting with an empty tree, we put in a new 2-node: X Add 19 19

 When adding to a 2-node, make it a 3-node, we put in a new 2-node: Add

 When adding to a 3-node, the 3-node breaks into two 2-nodes, bringing the middle value up: Add

 When breaking a 3-node into two parts, you move the middle value up  If the node above is empty, it's a new 2-node  If the node above is a 2-node, it becomes a 3- node  If the node above is another 3-node, it also breaks into 2-nodes, which might cascade up the tree

 Add the following keys to a 2-3 tree:  62  11  32  7  45  24  88  25  28  90

 Because of the guarantees about the depth of the tree, we the following running times for 2-3 search trees  Θ(log n) insert  Θ(log n) delete (messy, but true)  Θ(log n) find (not that different from a BST find)

 How do we implement a 2-3 tree?  Answer: We don't.  It is (of course) possible, but it involves having weird 2-nodes and 3-nodes that inherit from some abstract node class or interface  It's a huge pain  Note that 2-3 trees are essentially a special case of a B-tree, and someone does have to implement those  Instead, we use red-black trees which are structurally the same as 2-3 trees (if you squint)

Student Lecture

 One hundred ants are walking along a meter long stick  Each ant is traveling either to the left or the right with a constant speed of 1 meter per minute  When two ants meet, they bounce off each other and reverse direction  When an ant reaches an end of the stick, it falls off  Will all the ants fall off?  What is the longest amount of time that you would need to wait to guarantee that all ants have fallen off?

 On the previous slide, we can look at the problem "as if" ants were passing through each other with no effect  This idea of looking at a problem as if it is something else can make solving it easier  Coding up a 2-3 tree is annoying (but possible)  By creating a data structure that is (somehow) equivalent, we can get the job done in an easier way  Sometimes the way we implement an algorithm and the way we analyze it are different

 A red-black tree is a form of binary search tree  Each node looks like a regular BST node, with one additional piece of information: color  A node can either be red or black  Null values are considered black  The color allows us to simulate a 2-3 tree  We can think of a red node is actually part of a 3 node with its parent

 A red-black tree is a BST with red and black nodes and the following properties:  Red nodes lean left from their parents  No node has two red children  The tree has perfect black balance ▪ In other words, every path from the root to a null has the same number of black nodes on the way ▪ The length of this path is called the black height of the tree  The book describes the link as having a color (which is probably easier to think about), but the color has to be stored in the node

M EJ ACHL R PSX M E J A CH L R P S X

 We can do an insertion with a red-black tree using a series of rotations and recolors  We do a regular BST insert  Then, we work back up the tree as the recursion unwinds  If the right child is red and the left is black, we rotate the current node left  If the left child is red and the left child of the left child is red, we rotate the current node right  If both children are red, we recolor them black and the current node red  You have to do all these checks, in order!  Multiple rotations can happen  It doesn't make sense to have a red root, so we always color the root black after the insert

We perform a left rotation when the right child is red Y X B A C Current Y X BA C

We perform a right rotation when the left child is red and its left child is red Z Y B A D Current X C Y Y B AD X C

We recolor both children and the current node when both children are red Y X BAD Current Z C Y X BAD Z C

 Add the following keys to a red-black tree:  62  11  32  7  45  24  88  25  28  90

 The height of a red-black tree is no more than 2 log n  Find is Θ(height), so find is Θ(log n)  Since we only have to go down that path and back up to insert, insert is Θ(log n)  Delete in red-black trees is messy, but it is also actually Θ(log n)

 Balancing trees by construction  Introduce hash tables

 Keep working on Project 3  Form teams by Friday!  Finish working on Assignment 4  Due on Friday  Read section 3.4