Professional Documents
Culture Documents
Student Name: Uid: /: Write A Program To Implement Insert and Delete Node in A Red Black Tree
Student Name: Uid: /: Write A Program To Implement Insert and Delete Node in A Red Black Tree
WS9
Write a program to implement insert and delete node in a Red-Black tree.
3.code
insertion:-
package com.practice;
Node(int data)
{
super();
this.data = data; // only including data. not key this.left = null; // left subtree
this.right = null; // right subtree
this.colour = 'R'; // colour . either 'R' or 'B' this.parent = null; // required at time of
rechecking.
}
}
// this function performs left rotation Node rotateLeft(Node node)
{
Node x = node.right; Node y = x.left;
if(root!=this.root)
{
}
}
else
{
this.rl = false;
}
else if(this.lr) // for left and then right.
{
root.left = rotateLeft(root.left); root.left.parent = root;
root = rotateRight(root);
root.colour = 'B'; root.right.colour = 'R'; this.lr = false;
}
// when rotation and recolouring is done flags are reset.
// Now lets take care of RED RED conflict if(f)
{
parent
{
if(root.parent.left==null || root.parent.left.colour=='B') // case when
}
}
else
{
if(root.parent.right==null || root.parent.right.colour=='B')
{
if(root.left!=null && root.left.colour=='R') this.rr = true;
else if(root.right!=null && root.right.colour=='R')
}
else
{
}
}
this.lr = true;
root.parent.right.colour = 'B'; root.colour = 'B'; if(root.parent!=this.root)
root.parent.colour = 'R';
f = false;
}
return(root);
}
}
else
this.root = insertHelp(this.root,data);
for deletion:-
package com.company;
enum Color {
Red, Black
}
public TreeNode(int data) { this.left = null; this.right = null; this.parent = null; this.data
= data; this.color = Color.Red;
}
}
public RBTree() {
TreeNode nilNode = new TreeNode(0); nilNode.color = Color.Black; this.NIL =
nilNode;
this.root = this.NIL;
}
y.left = x; x.parent = y;
}
else
temp = temp.right;
z.parent = y;
insertFixup(z);
}
else {
TreeNode w = x.parent.left; if(w.color == Color.Red) {
w.color = Color.Black;
x.parent.color = Color.Red; rightRotate(x.parent);
w = x.parent.left;
}
if(w.right.color == Color.Black && w.left.color == Color.Black) { w.color =
Color.Red;
x = x.parent;
}
else {
if(w.left.color == Color.Black) { w.right.color = Color.Black; w.color = Color.Red;
leftRotate(w);
w = x.parent.left;
}
w.color = x.parent.color; x.parent.color = Color.Black; w.left.color = Color.Black;
rightRotate(x.parent);
x = this.root;
}
}
}
x.color = Color.Black;
}
else {
y = minimum(z.right); yOrignalColor = y.color;
x = y.right; if(y.parent == z) {
x.parent = z;
}
else {
rbTransplant(y, y.right); y.right = z.right; y.right.parent = y;
}
rbTransplant(z, y); y.left = z.left; y.left.parent = y; y.color = z.color;
}
if(yOrignalColor == Color.Black) deleteFixup(x);
}
TreeNode a, b, c, d, e, f, g, h, i, j, k, l, m;
a = new TreeNode(10);
b = new TreeNode(20);
c = new TreeNode(30);
d = new TreeNode(100);
e = new TreeNode(90);
f = new TreeNode(40);
g = new TreeNode(50);
h = new TreeNode(60);
i = new TreeNode(70);
j = new TreeNode(80);
k = new TreeNode(150);
l = new TreeNode(110);
m = new TreeNode(120);
t.insert(a);
t.insert(b);
t.insert(c);
}
7. Result/Output/
For deletion:-