Top Down Insertion for Red-Black Trees

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

To get started, import the starter file, RBTree.java, into the trees package you create in a new Java Project. You CANNOT modify the inner Node class that contains information about the nodes of the binary trees. Please do not change any of the method signatures in the RBTree class, but you are free to add additional helper methods.

 

Part 1: Top Down Insertion for Red-Black Trees

public boolean insert(Integer i)

This method should use a top down insertion strategy (see below) to insert a Node with data equal to i into the Red-Black Tree provided that a Node with data equal to i does

not already exist in the Red-Black Tree (i.e., no duplicate data). If the node is successfully inserted, return true, otherwise return false (tried to insert a duplicate element).

 

For the first part of the assignment, you will implement a top-down insertion for a

red-black tree. Basically the idea is to do the recoloring and rotations on the way down instead of after you insert the node. Node that the node to be inserted is always red, you may need to do one final rotation after inserting the node if its parent is red. Note that in this version of the red-black tree, we do not have parent pointers in our Node class. You cannot add a parent pointer to the node class or do additional tree traversals to find parent nodes. You will need to use a Stack to keep track of the ancestors in order to perform the correct rotations.

This is the situation you need to handle on your way down the tree.

Situation: A black node with two red children. Action: - Recolor the node red and the children black. - If the parent is red, perform rotations, otherwise continue down the tree

 

If the color flip produces a red node with a red child, then perform a single or double rotation depending on the following…

  • If the two red nodes are both left children or both right children, perform a single rotation (see example below)
  • Otherwise, perform a double rotation (see example below)
  •  

Part 2: Other methods on Red-Black Trees

public int height()

This method returns the height of the red-black tree. The height is defined as the longest path from the root to a leaf node. Note that the root has a height of 1.

 

public int blackHeight()

This method returns the height of the black height of red-black tree. The height black is defined as the number of black nodes from a given node to a leaf node (the NIL node). Note that we do not count the current node in the black height, but we do count the NIL leaf node (e.g. the black height of the root in the red-black tree below is 2). This method can be helpful when testing your insert method to make sure that you do not violate the fifth property of red black trees.

 

 

Y
X
Otherwise, perform a double rotation (see example below)
Single Rotation on Left Child
P
G
Z
S
U
Y
P
X
Z
G
SU
• The rotation is done on X's grandparent, G.
• The colors of P and G are flipped.
S
S
P
P
Double Rotation on Left Child
G
Y
X
G
U
Y
X
Z
• Again, the rotation is done on X's grandparent, G.
Z
• Recolor X and G
S
U
P
P
X
G
X
Y
Z
U
G
SY Z U
Transcribed Image Text:Y X Otherwise, perform a double rotation (see example below) Single Rotation on Left Child P G Z S U Y P X Z G SU • The rotation is done on X's grandparent, G. • The colors of P and G are flipped. S S P P Double Rotation on Left Child G Y X G U Y X Z • Again, the rotation is done on X's grandparent, G. Z • Recolor X and G S U P P X G X Y Z U G SY Z U
This is the situation you need to handle on your way down the tree
Y
X
Z Y
X
Z
Situation: A black node with two red children.
Action: - Recolor the node red and the
children black.
-If the parent is red, perform
rotations, otherwise continue down the tree
Transcribed Image Text:This is the situation you need to handle on your way down the tree Y X Z Y X Z Situation: A black node with two red children. Action: - Recolor the node red and the children black. -If the parent is red, perform rotations, otherwise continue down the tree
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education