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

avltree

The document outlines the process of constructing a Binary Search Tree (BST) using the keys 15, 32, 20, 9, 3, 25, 12, and 1, detailing the insertion steps and the resulting tree structure. It then describes the deletion of the key 15, explaining how to replace it with its in-order successor, which is 20. The final structure of the BST after the deletion is also presented.

Uploaded by

dgpguru
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)
31 views

avltree

The document outlines the process of constructing a Binary Search Tree (BST) using the keys 15, 32, 20, 9, 3, 25, 12, and 1, detailing the insertion steps and the resulting tree structure. It then describes the deletion of the key 15, explaining how to replace it with its in-order successor, which is 20. The final structure of the BST after the deletion is also presented.

Uploaded by

dgpguru
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/ 3

the process of constructing a Binary Search Tree (BST) using the given keys: 15, 32, 20, 9, 3,

25, 12, 1. Then, after the tree is built, I will delete a specified key and show the updated tree.

Step 1: Construct the BST

The keys will be inserted into the tree one by one.

Insert 15: This is the first key, so it becomes the root of the tree.

Insert 32: 32 is greater than 15, so it goes to the right of 15.

Insert 20: 20 is greater than 15 but less than 32, so it goes to the left of 32.

Insert 9: 9 is less than 15, so it goes to the left of 15.

Insert 3: 3 is less than 15 and also less than 9, so it goes to the left of 9.

Insert 25: 25 is greater than 15, less than 32, and greater than 20, so it goes to the right of 20.

Insert 12: 12 is less than 15, greater than 9, so it goes to the right of 9.

Insert 1: 1 is less than 15, less than 9, and less than 3, so it goes to the left of 3.

Step 2: BST Structure

After inserting all keys, the BST will look like this:

15

/ \

9 32

/\ /

3 12 20

/ \

1 25

Step 3: Deleting a Key

delete a key from the tree. I'll choose to delete 20 (which is a node with two children). When
go through the process of constructing a Binary Search Tree (BST) using the given keys: 15,
32, 20, 9, 3, 25, 12, 1. Then, after building the tree, we will delete the key 15 and update the
tree accordingly.

Step 1: Construct the BST

Insert 15: This is the first key, so it becomes the root of the tree.

Insert 32: 32 is greater than 15, so it goes to the right of 15.

Insert 20: 20 is greater than 15 but less than 32, so it goes to the left of 32.
Insert 9: 9 is less than 15, so it goes to the left of 15.

Insert 3: 3 is less than 15 and also less than 9, so it goes to the left of 9.

Insert 25: 25 is greater than 15, less than 32, and greater than 20, so it goes to the right of 20.

Insert 12: 12 is less than 15, greater than 9, so it goes to the right of 9.

Insert 1: 1 is less than 15, less than 9, and less than 3, so it goes to the left of 3.

Step 2: BST Structure

After inserting all keys, the BST will look like this:

15

/ \

9 32

/\ /

3 12 20

/ \

1 25

Step 3: Deleting the Key 15

Now, let's delete the key 15 from the tree. The key 15 has two children (9 and 32). When
deleting a node with two children, we replace it with its in-order successor (the smallest node
in its right subtree). In this case, the in-order successor of 15 is 20 (the smallest node in the
right subtree of 15).

Step 4: New Tree after Deleting 15

After deleting 15 and replacing it with 20, the new tree looks like this:

markdown

Copy code

20

/ \

9 32

/\
3 12

This is the final tree after the deletion of 15.

You might also like