1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton.

Slides:



Advertisements
Similar presentations
CSE Lecture 17 – Balanced trees
Advertisements

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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
CS202 - Fundamental Structures of Computer Science II
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.
Balanced Binary Search Trees
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
A balanced life is a prefect life.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
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.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
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.
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.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
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.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Balanced Search Trees Chapter Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.
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.
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:
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Binary Tree Representation Of Trees Problems with trees. 2- and 3-nodes waste space. Overhead of moving pairs and pointers when changing among.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
CSIT 402 Data Structures II
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
Red-Black Trees Acknowledgment Many thanks to “erm” from Purdue University for this very interesting way of presenting this course material. 1.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
2-3 Trees, Trees Red-Black Trees
Starting at Binary Trees
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 More Trees Trees, Red-Black Trees, B Trees.
Bottom-Up Red-Black Trees Top-down red-black trees require O(log n) rotations per insert/delete. Color flips cheaper than rotations. Priority search trees.
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
CS 367 Introduction to Data Structures Lecture 8.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
AA Trees.
File Organization and Processing Week 3
G64ADS Advanced Data Structures
Red Black Trees
CSCI Trees and Red/Black Trees
Red-Black Trees Motivations
(edited by Nadia Al-Ghreimil)
Advanced Associative Structures
Red-Black Trees.
2-3-4 Trees Red-Black Trees
Red-Black Implementation of 2-3 Trees
Red-black tree properties
Presentation transcript:

Trees and Red-Black Trees Gordon College Prof. Brinton

2 Regular Binary Trees Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40 Insertion sequence: 9, 5, 20, 3, 7, 15, 75, 6, 12, 17, 35, 100, 18, 25, 40

3 Balanced Trees Need a new search-tree structure - a balanced binary search tree 1.Maintains balanced node when adding or removing them Extra time needed at insert and remove 2.Guarantees at worst O(log n) search time We start with trees… - Perfectly balanced - Difficult and inefficient to implement

Trees Definitions 2-node - a data value and pointers to 2 subtrees 3-node - two data values and pointers to 3 subtrees 4-node - three data values and pointers to 4 subtrees A<B A<B<C

Tree trees have search tree properties What is the basic search algorithm?

6 Inserting into a Tree Insert begins with a single node and adds elements until it is full Insert another item 1. split the 4 node using the median value as the parent (promoting the median value to the parent level) 2. Insert the new item (inserted based on the BST rules for insertion) (always insert node in leaf of tree) C

Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 (4,12,25) Proactive top-down approach to splitting a 4-node

Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 (4,12,25)

Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

17 Another example Keys: A S E R C H I N G X What would the tree look like after inserting this set of keys?

18 Another example Keys: A S E R C H I N G X A

19 Another example Keys: A S E R C H I N G X A S

20 Another example Keys: A S E R C H I N G X A E S

21 Another example Keys: A S E R C H I N G X R S E A

22 Another example Keys: A S E R C H I N G X R S E A C

23 Another example Keys: A S E R C H I N G X H R S E A C

24 Another example Keys: A S E R C H I N G X H IS A C E R

25 Another example Keys: A S E R C H I N G X H I NS A C E R

26 Another example Keys: A S E R C H I N G X G HN A C E I R S

27 Another example Keys: A S E R C H I N G X G HN A C S X I E R

Tree Facts With N elements, the max number of nodes visited during the search for an element is int(log 2 n) + 1 Inserting an element into a tree with n elements requires splitting no more than int(log 2 n) nodes (often far fewer) Problem: allocated a large amount of wasted space 3n + 1 unused pointers (n - nodes) 28 wasted pointers

Warmup Exercise Create a tree from the following sequence of numbers:

Warmup Exercise Create a tree from the following sequence of numbers:

31 Red-Black Trees Designed to represent tree without the additional link overhead. Colors are used to represent the 3-node and 4-node. Red-Black trees are simple binary trees with a color indicator that is used to maintain certain properties - these properties keep the tree balanced.

32 Red-Black Nodes 2-nodes simple binary node (black node) 4-nodes center value becomes the parent (black) with outside values becoming the children (red) N H I N I H N

33 Red-Black Nodes 3-nodes A B A B B A or Note: 1.Red-black trees are not unique 2.However, the corresponding tree is unique

34 Red-Black Nodes A B B A NI H N Use color grouping of nodes to indicate the corresponding nodes in the tree

35 Converting a Tree to Red- Black Tree Example Top-down conversion algorithm: (start at the root) 1.Apply red-black tree representation to each node 2.Repeat for next level…

36 Converting a Tree to Red- Black Tree Example

37 Converting a Tree to Red- Black Tree Example H I N I H N

38 Converting a Tree to Red- Black Tree Example

39 Converting a Tree to Red- Black Tree Example

40 Converting a Tree to Red- Black Tree Example How could this be different?

41 Red-Black Tree Properties 1.The Root of a red-black tree is BLACK 2.A RED parent never has a RED child – there are never 2 RED nodes in succession 3.Every path from the root to an empty subtree (NULL pointer) has the same number of BLACK nodes. a BLACK node corresponds to a level change in the tree

42 Inserting Nodes Guidelines 1.Maintain root as BLACK node 2.Enter a new node as a RED node – since each new node enters a 2- node or a 3-node. 3.Whenever it results in two RED nodes in succession – rotate nodes to create a BLACK parent. 4.When scanning down a path to find insertion location – split any 4- node.

43 Inserting Nodes Inserting a 2 3-node 5 12

44 Inserting Nodes Inserting a 2 3-node 4-node

45 Inserting Nodes Inserting a

46 Inserting Nodes Inserting a

47 Inserting Nodes Inserting a Single left rotation

48 Inserting Nodes Inserting a

49 Inserting Nodes Inserting a

50 Inserting Nodes Inserting a right - left rotation

51 Splitting a 4-node 4 possible situations Step1: color flip – parent x becomes RED and its two children become BLACK.

52 Splitting a 4-node Parent: BLACK Color flip is enough.

53 Splitting a 4-node and inserting node 55 Color flip is enough.

54 Splitting a 4-node Parent: RED Splitting a 4-node oriented left-left from node G using a single right rotation

55 Splitting a 4-node Parent: RED Splitting a 4-node oriented left-right from node G after the color flip

56 Splitting a 4-node Parent: RED Splitting a 4-node oriented left-right from node G after the color flip Red-Black Tree Property Violation?

57 Two REDs: rotations X G P A B A P X G B C D C D A G P X B D C Single Left-rotation About X P G X A B D C Single Right-rotation About X

58 Building a RED-BLACK tree 2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7

59 Building a RED-BLACK tree 2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7 Split when Going down? 55 11

60 Building a RED-BLACK tree 2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7 Correct node

61 Building a RED-BLACK tree 2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7

62 Building a RED-BLACK tree 2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7

63 Building a RED-BLACK tree 10, 20, 30, 40, 50, 31, 34, 36 No problem up to this point. What would the tree look like?

64 Deleting RED-BLACK nodes To Delete: same strategy as deleting any node from a binary tree (Chapter 10) Delete node 25 Replace deleted node with value nearest to deleted value: either 15 or 30

65 Deleting RED-BLACK nodes If replace node is RED then level height of tree is unchanged - no other action is needed except making sure color properties are maintained

66 Deleting RED-BLACK nodes If replace node is BLACK then adjustments must be made: a bottom-up algorithm is used to flip and rotate. DELETE: O(log n)