avltree
avltree
25, 12, 1. Then, after the tree is built, I will delete a specified key and show the updated tree.
Insert 15: This is the first key, so it becomes the root of the tree.
Insert 20: 20 is greater than 15 but less than 32, so it goes to the left of 32.
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.
After inserting all keys, the BST will look like this:
15
/ \
9 32
/\ /
3 12 20
/ \
1 25
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.
Insert 15: This is the first key, so it becomes the root of the tree.
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.
After inserting all keys, the BST will look like this:
15
/ \
9 32
/\ /
3 12 20
/ \
1 25
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).
After deleting 15 and replacing it with 20, the new tree looks like this:
markdown
Copy code
20
/ \
9 32
/\
3 12