Build a specific leftist tree? - algorithm

I was practicing leftist trees and saw an example of min height-biased leftist tree on the textbook:
2
/ \
7 50
/ /
11 80
/
13
The question is, can I use only insertions to build this example?
I tried the following insertion sequence:
2 7 11 13 50 80
and it turns out to be this one:
2
/ \
11 7
/ \ /
13 50 80
So how can I achieve this? If it is impossible, why?
Furthermore, can the example tree on the textbook be built when other operations are allowed?

I figured it out! The following sequence is fine:
13 11 7 2 50 80
The idea is that the tree goes unbalanced when the sequence is descending. For example,
4 3 2 1
builds an unbalanced tree
1
/
2
/
3
/
4

Related

Travel randomly through a matrix

I have just finished a project where I implemented the recursive backtracking algorithm for a maze generator. Since then I have been looking for an algorithm that goes through a matrix but passing through each square only once. Just like in the recursive backtracking algorithm but without "going back". Is there any algorithm for this?
This is an example of what I mean with a 5*5 matrix:
5 4 1 20 21
6 3 2 19 22
7 8 17 18 23
10 9 16 15 24
11 12 13 14 25
As you can see you can start from square 1 and end on 25 without repeating any other square.
Thanks for your time.

Implement B-tree insert, delete

I'm learning how to insert/delete in B-tree.
And just find out that whenever I delete an element, then add it again.
The result after adding is always the same of before deleting.
Is that's always true.
Thanks in advance
What is your question?
The exact layout of a B-tree is order dependant, so no, it will not necessarily be the same when your remove and re-add an element.
5
/ \
1 2 3 4 6 7 8
4
/ \
1 2 3 5 6 7 8
are both proper B-trees of order 2 for the same data, produced by different permutations of the insertions of numbers 1-8.

Complexity of creating an adjacency list from a lits of edges?

I'm creating an algorithm that can build an adjacency list from a list of edges.
For example, if the data input was:
1 2
1 8
2 8
3 5
3 1
4 5
4 6
5 2
5 9
6 4
6 8
7 4
7 10
8 4
8 6
9 4
9 5
10 7
10 3
The output would be:
1: 8 4 6
2: 4 6
3: 9 2 8
4: 2 9 8
5: 8 4
6: 5 4
7: 5 6 3
8: 5 6 4
9: 5 6 2
10: 4 5 1
The algorithm is obviously bounded by the number of vertices and edges so originally I was thinking it would be O(v + e). But I could only get the program to work by implementing for loops inside for loops with 2d arrays, which I believe cause complexity of O(N^2).
Can anyone help me better understand?
It depends a fair bit on what sort of data structure you are using to store the map from vertices to lists of adjacent vertices. Iterating through the list of edges is of course going to have time complexity O(e). Any larger time complexity is going come from the time required to find a vertex in the map and the time required to insert a new item into a vertex's list of adjacent vertices. If you were using flat arrays then you could have O(v*e) complexity (for each edge, loop through the vertex list to find the desired vertex), but this could be improved quite a lot by using a hash-table or tree data structure that gave you better lookup performance.

How to iterate through a tree in spiral order?

1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
the output in spiral order should be
1 3 2 4 5 6 7 15 14 13 12 11 10 9 8
Think about how you would iterate through the root node and the first level.
Can you write a function for that behavior, and call it recursively?
This is really a variation of a breadth-first search. A breadth-first search uses a queue to get a list of the nodes at the next level down. A queue is a FIFO (first in first out). If you reverse the order at each level you'll get this effect so you need a LIFO (last in first out) instead, otherwise known as a stack.

directed graph with minimum number of chains

I have a problem but I can't figure out a solution. It goes like this:
I have a directed graph with N nodes and M links and without cycles. I need to find out the minimum numbers of chains so every node belongs to only one chain.
Example:
7 11 7 nodes; 11 links
1 2
1 5
2 3
2 5
2 7
3 4 // link exists between 3 and 4
3 6
4 6
5 4
5 6
7 3
Answer is: 2
An example is
Chain: 2-7-3-6
Chain: 1-5-4
Thanks.
He doesn't need to know if the graph is hamiltonian - knowing that it's a DAG is enough. It's probably a contest or online judge problem? It does look too hard to be homework.
Solution here: http://www2.cs.science.cmu.ac.th/person/rogaway/ps3-solns.pdf
To find the matching efficiently, consider the Hopcroft Karp algorithm: http://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm

Resources