CS 331 DATA STRUCTURES & ALGORITHMS BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, THE TREE TRAVERSALS, B TREES WEEK - 7

Similar documents
12 July, Red-Black Trees. Red-Black Trees

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

CS350: Data Structures Red-Black Trees

B-Trees. Introduction. Definitions

Trees. (Trees) Data Structures and Programming Spring / 28

Chapter 12 Advanced Data Structures

Algorithms. Deleting from Red-Black Trees B-Trees

Properties of red-black trees

CS 350 : Data Structures B-Trees

Section 4 SOLUTION: AVL Trees & B-Trees

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6

Binary Search Trees. Analysis of Algorithms

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

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

Red-black trees (19.5), B-trees (19.8), trees

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

CS350: Data Structures B-Trees

CS24 Week 8 Lecture 1

13.4 Deletion in red-black trees

Motivation for B-Trees

13.4 Deletion in red-black trees

Search Trees. COMPSCI 355 Fall 2016

TREES. Trees - Introduction

Define the red- black tree properties Describe and implement rotations Implement red- black tree insertion

Trees Slow Insertion in an Ordered Array If you re going to be doing a lot of insertions and deletions, an ordered array is a bad choice.

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

Red-Black trees are usually described as obeying the following rules :

Self-Balancing Search Trees. Chapter 11

Lecture 21: Red-Black Trees

Binary Trees, Binary Search Trees

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

Binary search trees (chapters )

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

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

Binary Trees

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

A set of nodes (or vertices) with a single starting point

Programming II (CS300)

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

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Binary search trees (chapters )

Advanced Algorithms. Class Notes for Thursday, September 18, 2014 Bernard Moret

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

Search Trees - 1 Venkatanatha Sarma Y

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

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

Trees. Eric McCreath

A dictionary interface.

Spring 2018 Mentoring 8: March 14, Binary Trees

CS 171: Introduction to Computer Science II. Binary Search Trees

CS 758/858: Algorithms

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

Data Structures and Algorithms

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

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

Unit III - Tree TREES

Week 3 Web site:

Balanced Search Trees. CS 3110 Fall 2010

Question Bank Subject: Advanced Data Structures Class: SE Computer

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search

Balanced Binary Search Trees. Victor Gao

Balanced search trees

CS350: Data Structures AVL Trees

DDS Dynamic Search Trees

Data Structures and Algorithms

Binary Search Tree (3A) Young Won Lim 6/2/18

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Lecture 13: AVL Trees and Binary Heaps

CS102 Binary Search Trees

CS127: B-Trees. B-Trees

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

Binary heaps (chapters ) Leftist heaps

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

UNIT III BALANCED SEARCH TREES AND INDEXING

Chapter 2: Basic Data Structures

Programming II (CS300)

Data Structures and Algorithms

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Balanced Binary Search Trees

Multi-way Search Trees

Lecture 6: Analysis of Algorithms (CS )

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

Graduate Algorithms CS F-07 Red/Black Trees

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is?

MIDTERM WEEK - 9. Question 1 : Implement a MyQueue class which implements a queue using two stacks.

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Transform & Conquer. Presorting

AVL Trees (10.2) AVL Trees

Heapsort. Why study Heapsort?

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Data structures. Priority queues, binary heaps. Dr. Alex Gerdes DIT961 - VT 2018

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

CMSC 341 Lecture 14: Priority Queues, Heaps

Lecture 6. Binary Search Trees and Red-Black Trees

Transcription:

Ashish Jamuda Week 7 CS 331 DATA STRUCTURES & ALGORITHMS BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, THE TREE TRAVERSALS, B TREES OBJECTIVES: Red Black Trees WEEK - 7 RED BLACK TREES Ordinary binary search trees suffer from a troublesome problem. They work well if the data is inserted into the tree in random order. However, they become much slower if data is inserted in already-sorted order (17, 21, 28, 36,...) or inversely sorted order (36, 28, 21, 17,...). When the values to be inserted are already ordered, a binary tree becomes unbalanced. With an unbalanced tree, the ability to quickly find (or insert or delete) a given element is lost. One way to solve the problem of unbalanced trees is: the red-black tree, which is a binary search tree with some added features. Red-Black Tree Characteristics The nodes are colored. During insertion and deletion, rules are followed that preserve various arrangements of these colors. Red-Black Rules When inserting (or deleting) a new node, certain rules, which we call the red-black rules, must be followed. If they re followed, the tree will be balanced. Let s look briefly at these rules: 1. Every node is either red or black. 2. The root is always black. 3. If a node is red, its children must be black (although the converse isn t necessarily true). 4. Every path from the root to a leaf, or to a null child, must contain the same number of black nodes. CS 331 Week 7 Page 1

The null child referred to in Rule 4 is a place where a child could be attached to a non-leaf node. In other words, it s the potential left child of a node with a right child, or the potential right child of a node with a left child. This will make more sense as we go along. The number of black nodes on a path from root to leaf is called the black height. Another way to state Rule 4 is that the black height must be the same for all paths from the root to a leaf. Fixing Rule Violations There are two, and only two, possible actions you can take: You can change the colors of nodes. You can perform rotations. Experiment 1: Inserting Two Red Nodes Root node has the value 50. Insert a new node with a value smaller than the root, say 25. Adding this node doesn t cause any rule violations. Insert a second node that s larger than the root, say 75. The tree is still red-black correct. It s also balanced; there are the same number of nodes on the right of the only non-leaf node (the root) as there are on its left. Notice that newly inserted nodes are always colored red (except for the root). This is not an accident. Inserting a red node is less likely to violate the red-black rules than inserting a black one. This is because, if the new red node is attached to a black one, no rule is broken. It doesn t create a situation in which there are two red nodes together (Rule 3), and it doesn t change the black height in any of the paths (Rule 4). Of course, if you attach a new red node to a red node, Rule 3 will be violated. However, with any luck this will happen only half the time. Whereas, if it were possible to add a new black node, it would always change the black height for its path, violating Rule 4. Experiment 2: Rotations CS 331 Week 7 Page 2

Let s try some rotations. Start with the three nodes as shown in Figure. Position the red arrow on the root (50). This node will be the top node in the rotation. Now perform a right rotation. The nodes all shift to new positions. In this right rotation, the parent or top node moves into the place of its right child, the left child moves up and takes the place of the parent, and the right child moves down to become the grandchild of the new top node. Experiment 3: Color Flips As we mentioned, a color flip is necessary whenever, during the insertion process, a black node with two red children is encountered. The red arrow should already be positioned on the black parent (the root node). The root s two children change from red to black. Ordinarily, the parent would change from black to red, but this is a special case because it s the root: It remains black to avoid violating Rule 2. Now all three nodes are black. The tree is still red-black correct. The tree is still red-black correct. The root is black, there s no situation in which a parent and child are both red, and all the paths have the same number of black nodes (two). Adding the new red node didn t change the red-black correctness. CS 331 Week 7 Page 3

Rotations To balance a tree, you need to physically rearrange the nodes. If all the nodes are on the left of the root, for example, you need to move some of them over to the right side. This is done using rotations. In this section we ll learn what rotations are and how to execute them. Rotations must do two things at once: Raise some nodes and lower others to help balance the tree. Ensure that the characteristics of a binary search tree are not violated. Simple Rotations In Experiment 2 we tried rotations to the left and right. Those rotations were easy to visualize because they involved only three nodes. Let s clarify some aspects of this process. What s Rotating? The term rotation can be a little misleading. The nodes themselves aren t rotated; it s the relationship between them that changes. One node is chosen as the top of the rotation. If we re doing a right rotation, this top node will move down and to the right, into the position of its right child. Its left child will move up to take its place. Remember that the top node isn t the center of the rotation. If we talk about a car tire, the top node doesn t correspond to the axle or the hubcap; it s more like the topmost part of the tire tread. The rotation we described in Experiment 2 was performed with the root as the top node, but of course any node can be the top node in a rotation, provided it has the appropriate child. Mind the Children You must be sure that, if you re doing a right rotation, the top node has a left child. Otherwise, there s nothing to rotate into the top spot. Similarly, if you re doing a left rotation, the top node must have a right child. The Efficiency of Red-Black Trees Like ordinary binary search trees, a red-black tree allows for searching, insertion, and deletion in O(log2N) time. Search times should be almost the same in the red-black tree as in the ordinary tree because the red-black characteristics of the tree aren t used during searches. The only penalty is that the storage required for each node is increased slightly to accommodate the red-black color (a boolean variable). CS 331 Week 7 Page 9

Ashish Jamuda Week 8 CS 331 DATA STRUCTURES & ALGORITHMS BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B- TREES OBJECTIVES: B-TREES WEEK 8 B-TREES: A B-Tree is a height balanced search tree. A B-Tree of order m satisfies the following properties: The root node has at least 2 children (if it is not empty or is not a leaf node) All nodes other than root have at least ceil(m/2) children (i.e. links) The following is an example of a B-tree of order 5 (A 4-5 B-Tree). A 4-5 B-Tree means 4 key values and 5 links/children. To be precise, 1. Other that the root node, all internal nodes have at least, ceil(5 / 2) = ceil(2.5) = 3 children. Maximum is 5. 2. At least ceil(5/2)-1 = ceil(2.5)-1 = 3-1 = 2 keys. Maximum is 4. 3. In practice B-trees usually have orders a lot bigger than 5.B-tree of order 5: (Creation & Addition) CS 331 Week 8 Page 1

CNGAHEKQMFWLTZDPRXYS CS 331 Week 8 Page 2

Deletion: Delete H: Since H is in a leaf and the leaf has more than the minimum number of keys, this is easy. We move the K over where the H had been and the L over where the K had been. (Next T) CS 331 Week 8 Page 3

Delete T : Since T is not in a leaf, we find its successor (the next item in ascending order), i.e. W, and move W up to replace the T. That way, what we really have to do is to delete W from the leaf, which we already know how to do, since this leaf has extra keys. In ALL cases we reduce deletion to a deletion in a leaf, by using this method. (Next R) Delete R: Although R is in a leaf, this leaf does not have an extra key; the deletion results in a node with only one key, which is not acceptable for a B-tree of order 5. If the sibling node to the immediate left or right has an extra key, we can then borrow a key from the parent and move a key up from this sibling. In our specific case, the sibling to the right has an extra key. So, the successor W of S (the last key in the node where the deletion occurred), is moved down from the parent, and the X is moved up. (S and W are inserted in their proper place.) (Next E) CS 331 Week 8 Page 4

Delete E: This one causes lots of problems. Although E is in a leaf, the leaf has no extra keys, nor do the siblings to the immediate right or left. In such a case the leaf has to be combined with one of these two siblings. This includes moving down the parent's key that was between those of these two leaves. In our example, let's combine the leaf containing F with the leaf containing A C. We also move down the D. Delete E: The parent node now contains only one key, G. This is not acceptable. If this problem node had a sibling to its immediate left or right that had a spare key, then we would again "borrow" a key. Since we have no way to borrow a key from a sibling, we must again combine with the sibling, and move down the M from the parent. In this case, the tree shrinks in height by one. CS 331 Week 8 Page 9