Rotation distance between two binary trees - binary-tree

I have the following binary tree, which I'm trying to convert into the target binary tree (second tree in the post) using minimum number of tree rotations. The theoretical minimum number of rotations for this tree is 5, however, the smallest value I can figure out is 6 rotations, I have copied the rotations as well, what am I missing?
Tree:
1
\
\
3
/ \
/ \
2 5
/ \
/ \
4 7
/ \
/ \
6 11
/ \
/ \
9 12
/ \
8 10
Target Tree:
1
\
\
11
/ \
/ \
9 12
/ \
/ \
3 10
/ \
/ \
2 5
/ \
/ \
4 7
/ \
6 8
The rotations I have tried so far (all of which require 6 rotations):
Order1:
Rotate left with root 7 and pivot 11
Rotate left with root 5 and pivot 11
Rotate left with root 3 and pivot 11
Rotate left with root 7 and pivot 9
Rotate left with root 5 and pivot 9
Rotate left with root 3 and pivot 9
Order2:
Rotate left with root 7 and pivot 11
Rotate left with root 5 and pivot 11
Rotate left with root 7 and pivot 9
Rotate left with root 3 and pivot 11
Rotate left with root 5 and pivot 9
Rotate left with root 3 and pivot 9
Order3:
Rotate left with root 7 and pivot 11
Rotate left with root 5 and pivot 11
Rotate left with root 7 and pivot 9
Rotate left with root 5 and pivot 9
Rotate left with root 3 and pivot 11
Rotate left with root 3 and pivot 9
Order4:
Rotate left with root 7 and pivot 11
Rotate left with root 7 and pivot 9
Rotate left with root 5 and pivot 11
Rotate left with root 3 and pivot 11
Rotate left with root 5 and pivot 9
Rotate left with root 3 and pivot 9
Order5:
Rotate left with root 7 and pivot 11
Rotate left with root 7 and pivot 9
Rotate left with root 5 and pivot 11
Rotate left with root 5 and pivot 9
Rotate left with root 3 and pivot 11
Rotate left with root 3 and pivot 9

Rotate right with root 11 and pivot 9
Rotate left with root 7 and pivot 9
Rotate left with root 5 and pivot 9
Rotate left with root 3 and pivot 9
Rotate left with root 9 and pivot 11

Related

Quicksort on an set of length 2

When quicksorting a dataset the list gets split down and is recursive, in that the solution calls itself on the smaller lists.
I was practising quicksort on an algorithm but a sublist of length 2 is a stone in my shoe, I can't solve it. The original list was:
2 0 1 7 4 3 5 6
Pivot being at 2, left at 0, right at 6, I start. Left moves along to 7, 7>=2. Right moves down to 1, 1<=2. Left and right have crossed. As I understand, now right becomes the split point and two new lists are formed.
2 0 1 7 4 3 5 6
As you can see, the first list, 2 and 0, is 2 items long. So 2 is the pivot, and 0 is both left and right. Left doesn't move along, right moves along to 2, 2<=2. Left and right have crossed so p replaces R and L onwards is a new list. But this leaves 2 and 0 unsorted.
Where am I going wrong?
The problem in your case came from the fact that i don't move pivot in its sorted place. After the partitioning with pivot 2 your array should look like this:
0 1 2 7 4 3 5 6
^
Let's go through partition procedure with the input array 13 19 9 5 12 8 7 4 21 2 6 11. And let's choose 11 as a pivot.
During the procedure, you need to maintain two pointers, one for the element just before the first element bigger than the pivot ^^, and another one for the current you are looking at ||.
The code looks like this:
A is array left..right
pivot = A[right]
i = left - 1 // the one before the first bigger than the pivot
for j = left to right - 1
if A[j] <= pivot
i = i + 1
swap A[i] with A[j]
swap A[i+1] with A[right] // put pivot at its place, i + 1 - is the index to split on
And the example:
13 19 9 5 12 8 7 4 21 2 6 11
13 19 9 5 12 8 7 4 21 2 6 11 13 > 11, skip
^^ ||
13 19 9 5 12 8 7 4 21 2 6 11 19 > 11, skip
^^ ||
9 19 13 5 12 8 7 4 21 2 6 11 9 < 11, swap
^^ ||
9 5 13 19 12 8 7 4 21 2 6 11 5 < 11, swap
^^ ||
9 5 13 19 12 8 7 4 21 2 6 11 12 > 11, skip
^^ ||
9 5 8 19 12 13 7 4 21 2 6 11 8 < 11, swap
^^ ||
9 5 8 7 12 13 19 4 21 2 6 11 7 < 11, swap
^^ ||
9 5 8 7 4 13 19 12 21 2 6 11 4 < 11, swap
^^ ||
9 5 8 7 4 13 19 12 21 2 6 11 21 > 11, skip
^^ ||
can you continue yourself?
The quicksort algorithm only has base case of empty array or array of size 1. In your case of [2 0] , the algorithm chooses 2 as a pivot, partitions [2 0] into empty array and array [0] and merges it with pivot [2], giving sorted array [0 2].

Write out pre-order, in-order, and post-order traversals given a tree:

Okay, so given this tree I need to write out the pre-order, in-order, and post-order traversals for it.
9
/ \
5 12
/ \ / \
2 7 11 15
/ / / \ \
3 6 10 13 16
\
17
This is what I've come up with, my teacher didn't do a great job of going over this so I'm not sure if I'm anywhere near correct.
pre-order: 9 5 2 3 7 6 12 11 10 13 15 16 17
in-order: 3 2 5 7 6 9 12 11 10 13 15 16 17
post-order: 3 2 6 7 5 10 11 17 16 15 13 12 9
Any help would be greatly appreciated
Pre-order: Do a depth-first traveral and write out the node when you encounter it the first time. So this is correct (9 5 2 3 7 6 12 11 10 13 15 16 17).
Post-order: Do a depth-first traversal and write out the node after you have processed all its children. So the correct sequence would be (3 2 6 7 5 10 13 11 17 16 15 12 9).
In-order: Do a depth-first traversal and write out the left subtree first, then the node itself and afterwards the right subtree. So the correct sequence would be (3 2 5 6 7 9 10 11 13 12 15 16 17). Here it makes a difference whether a single child is the left or right, for the other methods it doesn't matter.

X-Y heuristic function for solving N-puzzle

Can somebody please explain this heuristic function, for example for the following arrangement of 4x4 puzzle, whats the X-Y heuristic cost?
1 2 3 4
5 6 7 8
9 10 11 12
0 13 14 15
(0 indicates blank space)
As from here and here the X-Y heuristic is computed by the sum of the minimum number of column-adjacent blank swaps to get all tiles in their destination column and the minimum number of row adjacent blank swaps to get all tiles in their destination row.
So in this situation:
1 2 3 4
5 6 7 8
9 10 11 12
0 13 14 15
the only misplaced tiles are 13 , 14 and 15, assuming the goal state is
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
So in this case the we have to compute at first the number of column swaps the blank has to do to get all the tiles in the correct position. This is equivalent to 3, since the blank has to move three times to the the right column to be in the right position (and to have all the tiles in the right position)
Then we have to compute the number of row swaps the blank has to do. This is 0 thanks to the fact that all the tiles are already on the correct row.
Finally h(n) = 3 + 0 = 3 .

Binary search tree -- ordering

If we have V values for a search tree where the values are V= {1,2,3,4,5,6,7} inserted from right to left
And we are to order it to get the largest and shortest height possible -- how would we do it? Would it require the best and worst (lg2 (n+1)) case??
And would the orderings be unique?
Thanks -- I kinda understand but am not sure on what steps i should take.
The largest height is easy; put them in order:
1
\
2
\
...
With the smallest height, sort them, take the middle as the root, and put the two sides one either branch. Rinse and repeat.
3
/ \
2 5
/ / \
1 4 6
\
7
So... n for the first one, and log_2(n) for the second (rounded up).
The tallest such trees are created by inserting the values from a sorted sequence
1 2 3 4 5 6 7
or
7 6 5 4 3 2 1
The shortest tree is made by ordering the values via a recursive algorithm that finds the median then processes the left and right subtrees recursively:
4 2 1 3 6 5 7
This produces a tree of logarithmic height:
4
/ \
2 6
/ \ / \
1 3 5 7
Here the median is 4, so that goes first.
4
Now you have a partition for the left (1, 2, 3) and right (5, 6, 7). To order the left, start with its median, 2. Now you have 1 and 3 for its subtrees. These are 1 element sets so that's your base case.
4 2 1 3
Now process your right subtree (5, 6, 7), starting with 6.
4 2 1 3 6 5 7

Inserting elements into Binary Min Heaps

If I am inserting items: 10,12,14,1,6 into a binary min heap one item after another how would the results look like, my problem is with the following
when i start i have:
10
then
10
/
12
then
10
/ \
12 14
then
1
/ \
10 14
/
12
but this is not right, so what is the right way to do that?
Note: this is a homework question, i am trying to understand the concept, if you don't feel comfortable solving the question (it is not the full question anyway) please provide an example with similar issue.
You have to add the new element as a child (or leaf exactly) to the heap (not as root), that means you put it in the first "right" free spot (or in your heap-array, just at the end).
Then you have to re-establish the heap conditions, this is called "heapify". This happens in two phases:
Repeatedly exchange the new element (general: the element that violates the heap conditions) with its parent node as long as it is smaller than the parent and it is not the root.
Repeatedly exchange the new element with the child with the smallest value, until the new element becomes a leave or both child nodes are bigger than the element itself.
That means
10
/ \
12 14
+ 1 leads to
10
/ \
12 14
/
1
And that violates the heap conditions, so you have to heapify
10
/ \
1 14
/
12
But this is still not right, so you have to heapify again
1
/ \
10 14
/
12
And there you are... everything OK now :-)
step1: 10
step2: 10
/
12
step3: 10
/ \
12 14
step4: 1
/ \
10 12
/
14
step5: 1
/ \
6 10
/ \
12 14

Resources