Insertion into a red-black tree, Data Structure & Algorithms

Assignment Help:

The insertion procedure in a red-black tree is similar to a binary search tree i.e., the insertion proceeds in a similar manner but after insertion of nodes x into the tree T, we color it red. In order to guarantee that the red-black properties are preserved, we then fix up the updated tree by changing the color of the nodes and performing rotations. Let us write the pseudo code for insertion.

Given are the two procedures followed for insertion into a Red-Black Tree:

Procedure 1: This is used to insert an element in a given Red-Black Tree. It involves the method of insertion used in binary search tree.

Procedure 2: Whenever any node is inserted in a tree, it is made red, and after insertion, there may be chances of loosing Red-Black Properties in a tree, and, so, some cases are to be considered in order to retain those properties.

During the insertion procedure, the inserted node is always red. After inserting a node, it is essential to notify that which of the red-black properties are violated. Now let us look at the execution of fix up. Let Z be the node that is to be inserted & is colored red. At the starting of each iteration of the loop,

1. Node Z is red

2. If P(Z) is the root, then P(Z) is black

3. Now if any of the properties that mean property 2 is violated if Z is the root and is red OR when property 4 is violated if both Z and P (Z) are red, then we consider 3 cases in the fix up algorithm. Now let us discuss those cases.

Case 1(Z's uncle y is red): This is executed while both parent of Z (P(Z)) and uncle of Z, i.e. y are red in color.  thus, we can maintain one of the property of Red-Black tree by making both P(Z) and y  black and making point of P(Z) to be red, thus maintaining one more property.  Now, this while loop is repeated again till color of y is black.

Case 2 (Z's uncle is black and Z is the right child): thus, make parent of Z to be Z itself and apply left rotation to newly obtained Z.

Case 3 (Z's uncle is black and Z is the left child): This case executes by making parent of Z as black and P(P(Z)) as red and then performing right rotation to it  i.e., to (P(Z)).


Related Discussions:- Insertion into a red-black tree

Order of linear search, a. In worst case the order of linear search is O (n...

a. In worst case the order of linear search is O (n/2) b. Linear search is more competent than Binary search. c. For Binary search, the array must be sorted in ascending orde

Linked lists - implementation, The Linked list is a chain of structures whe...

The Linked list is a chain of structures wherein each structure contains data in addition to pointer, which stores the address (link) of the next logical structure in the list.

Delete a given specific node from a doubly linked list. , D elete a specif...

D elete a specific Node from Double Linked List as follows DELETEDBL(INFO, FORW, BACK, START, AVAIL,LOC) 1. [Delete Node] Set FORW [ BACK [LOC]]:= FORW[LOC]& BACK [FORW[

Simulation of queues, Simulation of queues: Simulation is the process of f...

Simulation of queues: Simulation is the process of forming an abstract model of a real world situation in order to understand the effect of modifications and the effect of introdu

A full binary tree with n leaves, A full binary tree with n leaves have:- ...

A full binary tree with n leaves have:- 2n -1 nodes.

What is an algorithm, What is an algorithm?  What are the characteristics o...

What is an algorithm?  What are the characteristics of a good algorithm? An algorithm is "a step-by-step process for accomplishing some task'' An algorithm can be given in many

Complete trees, This is a k-ary position tree wherein all levels are filled...

This is a k-ary position tree wherein all levels are filled from left to right. There are a number of specialized trees. They are binary trees, AVL-trees, binary search trees, 2

Division-remainder hashing, According to this, key value is divided by any ...

According to this, key value is divided by any fitting number, generally a prime number, and the division of remainder is utilized as the address for the record. The choice of s

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd