0% found this document useful (0 votes)
3 views

BST_(1)[1]

The document outlines the process of constructing binary search trees (BST) using specified numbers and demonstrates the deletion of certain keys while maintaining the tree structure. It provides step-by-step instructions for creating the BSTs, deleting nodes, and showing the resulting trees at each stage. The document covers multiple examples to illustrate the concepts clearly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

BST_(1)[1]

The document outlines the process of constructing binary search trees (BST) using specified numbers and demonstrates the deletion of certain keys while maintaining the tree structure. It provides step-by-step instructions for creating the BSTs, deleting nodes, and showing the resulting trees at each stage. The document covers multiple examples to illustrate the concepts clearly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Create a binary search tree (BST) from the numbers 10, 11, 8, 6, 9, 12, 14, 18, 36, and

16, and then


delete the keys 6, 14, and 12 one after the other while showing the trees at each stage, we will
follow these steps:

Step 1: Constructing the Binary Search Tree

1. Insert 10: This becomes the root of the tree.


2. Insert 11: Since 11 > 10, it goes to the right of 10.
3. Insert 8: Since 8 < 10, it goes to the left of 10.
4. Insert 6: Since 6 < 10 and < 8, it goes to the left of 8.
5. Insert 9: Since 9 < 10 but > 8, it goes to the right of 8.
6. Insert 12: Since 12 > 10 and > 11, it goes to the right of 11.
7. Insert 14: Since 14 > 10 and >11 but >12, it goes to the right of 12.
8. Insert 18: Since 18 >10 and >11 but >12 and >14, it goes to the right of 14.
9. Insert 36: Since 36 >10 and >11 but >12 and >14 but >18, it goes to the right of 18.
10. Insert 16: Since 16 is greater than 14 but less than 18, it goes to the left of 18.

The BST looks like this:

10
/ \
8 11
/\ \
6 9 12
\
14
\
18
/\
16 36

Step 2: Deleting Key 6

Node 6 is a leaf node (no children), so we can simply remove it.

The tree after deleting 6:

10
/ \
8 11
\ \
9 12
\
14
\
18
/\
16 36
Step 3: Deleting Key 14

Node 14 has one child (18). We replace 14 with its child.

The tree after deleting 14:

10
/ \
8 11
\ \
9 12
\
18
/\
16 36

Step 4: Deleting Key 12

Node 12 is a node with one child (18). We can simply remove it by linking its parent directly to its
child.

The final tree after deleting 12:

10
/ \
8 11
\ \
9 18
/\
16 36

Create a binary search tree (BST) from the numbers 10, 13, 16, 8, 5, 9, 12, 14, and 6, and then delete
the keys 10, 6, and 143 one after the other while show the trees at each stage.

Step 1: Constructing the Binary Search Tree

1. Insert 10: This becomes the root of the tree.


2. Insert 13: Since 13 > 10, it goes to the right of 10.
3. Insert 16: Since 16 > 10 and > 13, it goes to the right of 13.
4. Insert 8: Since 8 < 10, it goes to the left of 10.
5. Insert 5: Since 5 < 10 and < 8, it goes to the left of 8.
6. Insert 9: Since 9 < 10 but > 8, it goes to the right of 8.
7. Insert 12: Since 12 > 10 but < 13, it goes to the left of 13.
8. Insert 14: Since 14 > 10 and > 13 but <16, it goes to the right of 13.
9. Insert 6: Since 6 < 10 but >5 and <8, it goes to the right of 5.
The BST looks like this:

10
/ \
8 13
/\ /\
5 9 12 16
\
6

Step 2: Deleting Key 10

Node 10 has two children (8 and 13). We find its inorder successor (the smallest node in its right
subtree), which is 12. We replace 10 with 12 and then delete 12.

The tree after deleting 10:

12
/ \
8 13
/\ \
5 9 16
\
6

Step 3: Deleting Key 6

Node 6 is a leaf node (no children), so we can simply remove it.

The tree after deleting 6:

12
/ \
8 13
/\ \
5 9 16
Step 4: Deleting Key 143
Key 143 does not exist in the tree. Therefore, no changes are made.

The final tree remains:

12
/ \
8 13
/\ \
5 9 16

Create a binary search tree (BST) from the numbers 5, 3, 7, 2, 4 and then delete the keys 3 and 4
Step 1: Constructing the BST

1. Insert 5: This becomes the root.


2. Insert 3: Goes to the left of 5.
3. Insert 7: Goes to the right of 5.
4. Insert 2: Goes to the left of 3.
5. Insert 4: Goes to the right of 3.

The BST looks like this:

5
/\
3 7
/\
2 4

Step 2: Deleting Key 3

Node 3 has two children (2 and 4). We replace it with its inorder successor (4).

The tree after deleting 3:

5
/\
4 7
/
2
Step 3: Deleting Key 5

Node 5 has one child (4). We replace it with its child.

The final tree after deleting 5:

4
/
2
\

Create a binary search tree (BST) from the numbers 8, 6, 10, 5, 7 and then delete the keys 6 and 8

Step 1: Constructing the BST

1. Insert 8: This becomes the root.


2. Insert 6: Goes to the left of 8.
3. Insert 10: Goes to the right of 8.
4. Insert 5: Goes to the left of 6.
5. Insert 7: Goes to the right of 6.

The BST looks like this:

8
/\
6 10
/\
5 7

Step 2: Deleting Key 6

Node 6 has two children (5 and 7). We replace it with its inorder successor (7).

The tree after deleting 6:

8
/\
7 10
/
5

Step 3: Deleting Key 8

Node 8 has one child (7). We replace it with its child.


The final tree after deleting 8:

7
/
5

Create a binary search tree (BST) from the numbers 15, 10, 20, 8, 12 and then delete the keys 10
and 15

Step 1 Constructing the BST

1. Insert 15 as root.
2. Insert 10 as left child of root.
3. Insert 20 as right child of root.
4. Insert 8 as left child of node with key 10.
5. Insert 12 as right child of node with key 10.

The BST looks like this:

15
/\
10 20
/\
8 12

Step 2 Deleting Key 10

Node 10 has two children (8 and 12). We replace it with its inorder successor (12).

The tree after deleting 10:

15
/\
12 20
/
8

Step 3 Deleting Key 15

Node 15 has one child (12). We replace it with its child.

The final tree after deleting 15:


12
/
8

---

These questions provide a simpler understanding of constructing a binary search tree and
performing deletion operations while maintaining its properties!

You might also like