160k views
2 votes
a) Insert the following 10 numbers, 18,3,4,2,8,9,6,1,15,17 into an initially empty red-black tree. Intermediate steps are required. (b) Show the result of deleting 15,1,4,9,18 in sequence. Intermediate steps are required.

User FrankyBoy
by
8.3k points

1 Answer

4 votes

Explanation:

a) To insert the numbers 18,3,4,2,8,9,6,1,15,17 into an initially empty red-black tree, we can follow these steps:

1. Insert 18 as the root node of the tree.

- The color of the root node is black.

```

18(B)

/ \

NIL(T) NIL(T)

```

2. Insert 3 as the left child of 18.

- The color of the new node is red.

- Since the parent of 3 is black, the tree remains balanced.

```

18(B)

/ \

3(R) NIL(T)

/ \

NIL(T) NIL(T)

```

3. Insert 4 as the right child of 3.

- The color of the new node is red.

- Since the parent of 4 is red, we need to perform a rotation and recolor the nodes to maintain the red-black tree properties.

```

18(B)

/ \

4(R) 3(R)

/ \

NIL(T) NIL(T)

```

4. Insert 2 as the left child of 3.

- The color of the new node is red.

- Since the parent of 2 is red, we need to perform a rotation and recolor the nodes to maintain the red-black tree properties.

```

18(B)

/ \

4(B) 2(R)

/ \

3(R) NIL(T)

```

5. Insert 8 as the right child of 4.

- The color of the new node is red.

- Since the parent of 8 is black, the tree remains balanced.

```

18(B)

/ \

4(B) 2(R)

/ \

3(R) 8(R)

```

6. Insert 9 as the right child of 8.

- The color of the new node is red.

- Since the parent of 9 is red, we need to perform a rotation and recolor the nodes to maintain the red-black tree properties.

```

18(B)

/ \

4(B) 2(R)

/ \

3(R) 9(R)

\

8(R)

```

7. Insert

User Mwalling
by
7.8k points

No related questions found