Inserting into a 2-3 Tree - data-structures

I'm not sure if I am understanding the insertion process of a 2-3 Tree correctly. Say I have the tree:
and I want to insert the value 95 into it, would this be the proper new tree?

Yes this is correct.
Inserting 95 would place 3 children in the rightmost leaf (not allowed)
40
/ \
20 60, 80
/ \ / | \
10 30 50 70 90,95,100 <- not valid
The 3 nodes in the leaf make 95 move to the parent node, but now there are 3 nodes in the parent:
40
/ \
20 60,80,95 <- not valid
/ \ / | \
10 30 50 70 90,100
Moving 95 up causes the parent node to split:
40
/ \
20 80
/ \ / \
10 30 60 95
/ \ / \
50 70 90 100 valid

this is incorrect. the height of 2-3 tree is consistent hence you will split the parent not the child.

I think you are wrong. You are violating one fact about 2-3 tree: all leaves have same depth.
reference:
http://pages.cs.wisc.edu/~vernon/cs367/notes/10.23TREE.html#operations
Here is trace of insertion I have in mind:
Please let me know if this is incorrect.

Related

Binary Search Tree, How should I rotate this tree to balance

I am trying to rotate a tree to keep it balanced, but I am having hard time with this example. I am not so sure what I am doing wrong here.
I have below tree with L/R height difference 1 at node 30. So, I guess this tree is balanced.
30
/ \
20 60
/ \
10 25
I am adding 22 here and this is what I get after adding 22.
30
/ \
20 60
/ \
10 25
/
22
At node 20, L/R height difference is 1, but at node 30 it is 2. So, I guess it is not balanced any more. I am trying to right rotate the tree to balance it, but I am getting below tree.
20
/ \
10 30
/ \
25 60
/
22
After the rotation, node 20 has L/R height difference of 2 still.
Where am I doing wrong? Can this tree be balanced using rotation?
I can balance the tree using sorted array method like below, but I am really confused about rotation balance in this example.
22 25
/ \ / \
20 30 20 30
/ / \ / \ \
10 25 60 10 22 60
What am I doing wrong here?
Thanks a lot!!
There are basically four type of rotation in the AVL Tree.
Right-left
Left-Right
Left-Left
Right-Right
In your case, Left-Right should be applicable.
Here you need to perform two steps.
1:- Left rotate from 20 node. So your tree should like as below.
30
/ \
25 60
/
20
/ \
10 22
2:- Right rotate from 30 node. So your tree should be like as below.
25
/ \
20 30
/ \ \
10 22 60
You can refer the N website to understand the behavior. Here is one of the best link
This case requires a double rotation: rotate 25 up twice. I'm assuming that you're thinking about AVL trees, but all of the standard balanced binary trees need double rotations in some cases.

AVL Rotation - Which node to rotate

I have read many sources about AVL trees, but did not find anyone addressing this issue: When AVL tree gets unbalanced, which node should be rotated first?
Assuming I have the tree:
10
/ \
5 25
/
20
and I'm trying to add 15, both the root and its child 25 will be unbalanced.
10
/ \
5 25
/
20
/
15
I could do a RR rotation (or single rotation) of 25, resulting in the following tree:
10
/ \
5 20
/\
15 25
or a RL rotation (double rotation) about the root, creating the following tree:
20
/ \
10 25
/ \
5 15
I am confused about which rotation is the most suitable here and in similar cases.
The RR rotation is correct here. The rotation should be done as soon (as low) as the rule is broken. Which is for 25 here.
The higher rotations first don't necessarily break the rule and secondly would become too complex although it doesn't seem so here at the first sight.

AVL specific case balancing

I have been revisiting old basic algorithms for a class I an online course I am about to watch after christmass. It had been a generally light read, until I reached AVLs. Back when I was learning them, I do not remember having any problem, but after a little more than 10 years, I am no longer as good. While I solve most cases easily, I have stuck for more than 5 hrs on this example:
51
/ \
19 55
/ \ \
10 37 61
/ \
28 46
Inserting 40 into the tree, left child of 46, requires a Single Left Rotation to fix the balance...why? Isn't 40 inserted into the left side of the Right child of 19, who becomes unbalanced? Why is it not a double rotation? What do I fail to see?
Here is how it goes:
1. After insertion:
51
/ \
19 55
/ \ \
10 37 61
/ \
28 46
/
40
It is clear that 19 node becomes unbalanced.
2. That's why it is rotated left once. So it becomes:
51
/ \
37 55
/ \ \
19 46 61
/ \ /
10 28 40
Now the tree is balanced again.
And funnily enough, trying to explain my problem revealed what I was doing wrong.
The tree is left heavy and tree's left subtree is NOT right heavy, so it's a single rotation.

Else than backtracking, how do I find longest path in a graph?

I have a graph shaped as a triangle.
8
/ \
1 4
/ \ / \
4 2 0
/ \ / \ / \
9 1 9 4
In the above graph the longest path is {8, 4, 2, 9}
My current algorithm calculates the max number of the adjacent nodes and add it to the list, then calculates the sum of that list. This works in the above graph but won't work in situations such as this scenario:
8
/ \
0 1
/ \ / \
4 0 4
/ \ / \ / \
9 99 3 4
My algorithm will mistakenly go through {8,1,4,4} where the correct longest path is {8,0,4,99}
The only solution I can think of is Backtracking. Where I have to go through all the paths and calculate the max path, which will be insanely slow in a huge graph. This about a 100k nodes graph.
My question is can I do better than this?
Start at the top.
For each node, pick the maximum of its parents (the nodes above connected to it) and add its own value.
Then, in the last row, pick the maximum.
This will just give you the value of the longest path, but you could easily get the actual path by simply starting at the value picked at the bottom and moving upwards, always picking the greater parent.
The running time would be linear in the number of nodes.
Example:
Original:
First example: Second example:
8 8
/ \ / \
1 4 0 1
/ \ / \ / \ / \
4 2 0 4 0 4
/ \ / \ / \ / \ / \ / \
9 1 9 4 9 99 3 4
Output:
First example: Second example:
8 8
/ \ / \
9 12 8 9
/ \ / \ / \ / \
13 14 12 12 9 13
/ \ / \ / \ / \ / \ / \
22 15 23 16 21 111 16 17
Then you'd pick 23 for the first and 111 for the second.
To get the path, we'd have 23-14-12-8, which corresponds to 9-2-4-8, for the first, and 111-12-8-8, which corresponds to 99-4-0-8, for the second.
I'm of course assuming we have a tree, as stated. For general graphs, this problem is quite a lot more difficult - NP-hard, to be exact.
You do not need backtracking here - you can use breadth-first search to propagate the max for the path that you have found so far to the corresponding node, level by level.
Start at the root, and set its max to its own value.
Go through nodes level-by-level
For each node check the max stored in its parent. There may be one or two of these parents. Pick the max of two max-es, add the value of the node itself, and store it in the current node
When the path through the graph is complete, the result would look like this:
Max graph:
8
/ \
8 9
/ \ / \
12 9 13
/ \ / \ / \
21 111 16 17
To recover a path, find the max value in the bottom layer. This is the final node of your path. You can reconstruct the path from the max graph and the original by starting at the max (111), subtracting the value (99), looking for the result (111-99=12) in the max graph, and continuing to that node until you reach the top:
111 - 99 = 12 -- Take 99
12 - 4 = 8 -- Take 4
8 - 0 = 8 -- Take 0
8 is the root -- Take 8
This gives you the max path in reverse. Note that this may not be the unique path (think of a graph filled with equal values to see how there may be multiple max paths). In this case, however, any path that you would recover will satisfy the max path requirement.

More than one rotation needed to balance an AVL Tree?

My best guess is that one rotation is always enough to balance an AVL tree when you insert or delete ONE element from an already balanced AVL tree.
Is one rotation always enough?
An example will help where more than one rotations are needed.
PS: I count RL/LR rotations as one rotation only.
For insertion I believe one is enough.
but for deletion:
consider this tree:
50
/ \
25 75
/ \ / \
15 40 60 80
/ / \ \
35 55 65 90
/
62
delete 15 , first the 25's balance factor is destructed, one rotation:
50
/ \
35 75
/ \ / \
25 40 60 80
/ \ \
55 65 90
/
62
but still, we have to check, now the tree's root's balance factor is destructed, have to be rotated again:
60
/ \
50 75
/ \ / \
35 55 65 80
/ \ / \
25 40 62 90
For insert 1 rotation is at most.
For delete the number of rotation is bounded by O(log(n)). Log(n) is the height of the tree.
More explanation on AVL deletion.
When you delete a node from AVL you might cause the tree unbalanced, which you have to trace back to the point where it is unbalanced. If the unbalanced point is the root. You have to rebalance the tree from top to bottom.

Resources