Wasn't sure how to word this question. Basically take the following BST:
25
/ \
20 30
/ \ / \
18 23 27 31
/ \ /\
8 19 22 24
If I were to delete the value 25 and rotate the value 20 in its place, does it make more sense to append the 23 subtree to 27, or to append the 30 subtree to 24. And I don't mean specific to this case, but from a broader perspective.
Just to be clear, what is preferable between these two arrangements:
20
/ \
18 23
/ \ / \
8 19 22 24
\
30
/ \
27 31
20
/ \
18 30
/ \ / \
8 19 27 31
/
23
/ \
22 24
rotating 20 would not be the wisest decision. You should either replace it with the maximum value of the left sub-tree or the minimum value of the right sub-tree.
If you do wish to rotate the way you have, there is no difference between the heights of the tree's and both will be same in terms of time complexity.
Related
I would like to know whether I am applying the following insertion and deletion operations correctly on an AVL tree:
62
/ \
44 78
/ \ \
17 50 88
/ \
48 54
insert(42)
insert(90)
delete(62)
insert(92)
delete(50)
For this question, a deletion replaces the deleted item with its successor.
This is how I think the tree should be modified by those operations:
insert(42) and insert(90)
62
/ \
44 78
/ \ \
17 50 88
\ / \ \
42 48 54 90
delete(62)
78
/ \
44 88
/ \ \
17 50 90
\ / \
42 48 54
insert(92)
78
/ \
44 88
/ \ \
17 50 90
\ / \ \
42 48 54 92
delete(50)
78
/ \
44 88
/ \ \
17 54 90
\ / \
42 48 92
There are a two cases where rotations are needed:
___62___
/ \
__44__ 78
/ \ \
17 50 88
/ \
48 54
You had applied insert(42) correctly, but insert(90) creates an unbalanced subtree rooted at 78 (marked with asterisk): its right side has a height of 2, while its left side is empty:
___62___
/ \
__44__ 78*
/ \ \
17 50 88
\ / \ \
42 48 54 90
So, this will not stay like that: a simple left rotation will move 88 up, and 78 down:
___62___
/ \
__44__ 88
/ \ / \
17 50 78 90
\ / \
42 48 54
You had it correct for delete(62): that will swap the root with its successor, which is 78, and then 62 is removed:
___78___
/ \
__44__ 88
/ \ \
17 50 90
\ / \
42 48 54
insert(92) will bring an unbalance at node 88:
___78___
/ \
__44__ 88*
/ \ \
17 50 90
\ / \ \
42 48 54 92
And so a simple left rotation is again applied:
___78___
/ \
__44__ 90
/ \ / \
17 50 88 92
\ / \
42 48 54
delete(50) was correctly executed. Given the above state, we get:
___78___
/ \
__44__ 90
/ \ / \
17 54 88 92
\ /
42 48
I need that the output of the command cal stay highlighted in a variable in order print it into a wallpaper so I can disable the calendar software to save resources of a low spec computer. How can I make the cal output stay highlighted?
Perhaps something like:
cal -h | sed 's/ \('"$(date +%_d)"'\) /{\1}/'
outputs
June 2021
Su Mo Tu We Th Fr Sa
{ 1} 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
How can I delete the node 1 from 2-3-4 Tree if the tree's structure is as below:
4 10
/ | \
2 6,8 12,14
/ \ / | \ / | \
1 3 5 7 9 11 13 15
The way I like to think of it, you only delete leaves or internal nodes that have a single child, and the children of whatever you delete have to stay the same level.
This requires pulling a key down from the level above to hold them, which merges with a sibling.
If the parent only has one key, this will cause a cascaded delete.
Deleting 1 by pulling down 2 causes a cascaded delete:
4 , 10
/ | \
X 6,8 12,14
| / | \ / | \
2,3 5 7 9 11 13 15
The cascaded delete pulls down the 4:
10
/ \
4,6,8 12,14
/ | | \ / | \
2,3 5 7 9 11 13 15
If the sibling is too big to merge, you may have to redistribute from the sibling. This would be required if this was a 2-3 tree, for example:
Redistributing a key from 6,8
6 , 10
/ | \
4 8 12,14
/ \ / \ / | \
2,3 5 7 9 11 13 15
The goal is to remove 22 from the root node and re-balance the tree.
First I remove 22, and replace it by its in-order successor 28.
Secondly I rebalance the resulting tree, by moving the empty node to the left. The resulting tree is below.
Is moving 28 up the right procedure, and did I balance the left side correctly in the end?
22,34
/ | \
16 28 37
/ \ / \ / \
15 21 25 33 35 43
[28],34
/ | \
16 * 37
/ \ / \ / \
15 21 25 33 35 43
34
/ \
16,28 37
/ | \ / \
15 21,25 33 35 43
Thanks!
To delete 22 from
22,34
/ | \
16 28 37
/ \ / \ / \
15 21 25 33 35 43 ,
we replace it by its in-order successor 25, leaving a hole (*).
25,34
/ | \
16 28 37
/ \ / \ / \
15 21 * 33 35 43
We can't fix the hole by borrowing, so we merge its parent into its sibling, moving the hole up.
25,34
/ | \
16 * 37
/ \ | / \
15 21 28,33 35 43
The hole has two siblings now, so we can redistribute one of the parent's keys down.
34
/ \
16,25 37
/ | \ / \
15 21 28,33 35 43
(I'm working from this set of lecture notes. Don't bother memorizing the details here unless it's for an exam. Even then... I really wish data structure courses did not emphasize balanced search trees to the degree that they do.)
I have a binary tree, and pre-order traversal.
Here is my tree.
15
/ \
10 23
/\ /\
5 12 20 30
/ /
11 25
\
27
so result of pre-order : 15 10 5 12 11 23 20 30 25 27. It's OK
Than I delete 5,12 and 23 elements
Should I get this
15
/ \
10 27
\ /\
11 20 30
\
25
Result:15 10 11 27 20 30 25
or this?
15
/ \
10 25
\ /\
11 20 30
/
27
Result: 15 10 11 25 20 30 27
P.S I get 2nd case. If it isn't right, what is wrong with deletion?
UPD: SO the second updated variant is right?
Your 2nd case is almost right. 27 would be a left node of 30. When deleting a top node of a (sub)tree, you can either replace that node with the right-most node of the left branch or the left-most node of the right branch. In this case, you've replaced 30 with the left-most value of the right branch, which is 25. You'd have to perform this recursively as 25 has branches of its own. Once your target node to delete becomes a leaf, delete it.
First step:
15
/ \
10 25
\ /\
11 20 30
/
23
\
27
Second step:
15
/ \
10 25
\ /\
11 20 30
/
27
/
23
Third (deletion):
15
/ \
10 25
\ /\
11 20 30
/
27
If you want pre-order traversal of the remaining elements to be consistent with the pre-order traversal before the deletes, then your tree should look like this:
15
/ \
10 20
\ \
11 30
/
25
\
27
The delete method is:
If there's a left subtree of a deleted node, move the root of the left subtree to the deleted position. Follow the right subtree links in the (formerly) left subtree and attach the right subtree to the first empty right link. If there is no left subtree, move the root of the right subtree to the deleted position.