First of all I know this question has been asked multiple times so my apologies but I got lost when I thought of creating a BST for the word 'BERNER' with duplicates allowed .
So I know this might look like this, I'm half way done with it but I got confused when I think about placing N and R
B
/ \
E R
/
E
Also I have some other questions
In the above BST the first 'E' letter initially gets placed on the right side of the root node (B) due to E>B, but when we move on to 'R' it becomes the right node and 'E' becomes left node so I was thinking isn't that a violation of the BST rule since E > B, and it should also be a right node of 'B' ? Is this some kind of rule which I'm missing.
Creating 'BERNER' without Duplicates
Any help/Suggestion will be appreciated. Please don't be tough on me. I'm a newbie to BST Concepts.
Binary Tree for BERNER (PLEASE PAY ATTENTION that There must be no duplicate nodes in BST), with consideration that there is no different to insert nodes to left or right when its value is equal to its parent.
Related
I'm trying to predict the amount of parents in a Binary tree given that it all you know is the amount of leaves, and its a balanced binary tree.
Currently, my code runs like this:
int width = exits;
int amountOfParents = 0;
do
{
width -= 2;
AmountOfParents++;
} while (width > 0);
The basic premise of the code is that it will take all the child, and find the number of parents for them. Do this iteratively until you reach the root. However, the problem comes in when the height of the tree is uneven.
This solution gives correct number of parents up till 5. When it hits 6, the binaray tree creates another parent node, so there should be 4, but it gives 3. I know why it gives 3, but I don't know how to fix it.
Edit: I just had another idea. What if I find the closest perfect square number perfectly balanced tree, and than individually find the unaccounted? Trying now.
The formula is log2(exits) * 2 + 1
C#: Math.Ceiling(Math.Log(x) / Math.Log(2)) * 2 + 1;
But it has to be perfectly balanced indeed
So since I'm doing the inverse of square numbers, your idea of the square numbers could work.
I tried it with several different methods, but this seems to be the best one.
You take the all the child and group them into pairs. Put the pairs into a list and do the same thing again. If there is an odd number of pairs, just put him into the list. He will get handled in later iterations due to the nature of it.
Please excuse my small knowledge of graph theory vocabulary.
I can only describe the problem with common english words. Maybe someone can point me into the right direction and/or terms to look up.
The problem came up as part of the implementation of a visual programming language. Where a vertex is a function/method and the edges transport data between the functions. Now there is the following problem:
It could be allowed to connect the output of vertex A with the type of Collection< TItem > to the input of vertex B with type TItem. And then the output of vertex B with type TItem to the input vertex C with type Collection< TItem >. This would tell the compiler that it has to wrap a foreach function around vertex B to apply the function of B to each item in the collection from A and output the new items as collection to the input of C. So the edge from A to B is a many to one connection and from B to C is one to many.
Now the actual problem is, what kind of algorithm would find a (directed) subgraph that is surrounded/isolated by one to many connections? so that the compiler would wrap a foreach function around this particular subgraph? I've tried to visualize the problem in this picture:
Notice that there could be multiple such subgraphs in your graph.
To find each one you visit all the nodes in the graph and count the parents/children to determine whether it is a member of the desired set, then separate out all the marked nodes into their respective subgraphs or cliques. General procedures for working with cliques can be found on Wikipedia: The Clique Problem.
I would suggest the following algorithm:
Step 1 Walk through all the nodes. If you find a blue node, do a depth-first search in the directed graph to find out the set of white nodes reachable from it. Don't cross blue nodes while doing the DFS. Along with the set of nodes, store the starting blue node and the outgoing blue nodes that you discovered during the DFS.
You end up with multiple sets of white nodes, along with information about the incoming and outgoing blue nodes:
(bear with me, my mouse drawing skills are really bad)
Step 2 As you can see, you might have overlaps. There are two possibilities to resolve this:
Merge overlapping sets by using a disjoint-set data structure afterwards. This results in a O(n² + m) worst case runtime.
Avoid creating the overlaps in the first place by modifying the standard DFS algorithm. It should detect when you reach a node that you have already seen in one of the previously explored sets. It should then not explore the subgraph further, but record that the currently explored set and the overlapping one are to be merged later. Afterwards you can find the connected components in the merging graph. This will give you a
O(n + m) runtime, which is a lot better.
You end up with a collection of disjoint sets of white nodes together with respective incoming and outgoing blue nodes:
Given an input like
a { b c d { e f } g }
I want to parse it one token at at time (letter or brace). When I hit the first closing brace } I need to know how many elements there were since the last opening brace (e and f = 2). And then when I hit the one after that, I need 4 (b,c,d,g).
Grabbing the tokens 1 by 1 is easy, but... I don't know how to count them. I was thinking about Stack<int> but I can't modify the top element to increment it?
Rather than trying to modify the top element, why not keep that one just in an int variable.
When you see an opening brace, push your "count so far" onto the stack, and set the count to 0.
When you see a letter, increment your "count so far"
When you see a closing brace, do whatever you need to with the count, and pop the stack to get the new "count so far" value
EDIT: If you wanted to keep all the state in the stack itself, you can always think of the top element as a variable, which is changed by performing pop-increment-push. At that point, the operations are:
Opening brace: push 0
Letter: pop-increment-push
CLosing brace: pop, use value however you want to before it vanishes forever
This is likely to be very slightly less efficient, but I think it's actually more elegant.
I know there are quite some questions out there on generating combinations of elements, but I think this one has a certain twist to be worth a new question:
For a pet proejct of mine I've to pre-compute a lot of state to improve the runtime behavior of the application later. One of the steps I struggle with is this:
Given N tuples of two integers (lets call them points from here on, although they aren't in my use case. They roughly are X/Y related, though) I need to compute all valid combinations for a given rule.
The rule might be something like
"Every point included excludes every other point with the same X coordinate"
"Every point included excludes every other point with an odd X coordinate"
I hope and expect that this fact leads to an improvement in the selection process, but my math skills are just being resurrected as I type and I'm unable to come up with an elegant algorithm.
The set of points (N) starts small, but outgrows 64 soon (for the "use long as bitmask" solutions)
I'm doing this in C#, but solutions in any language should be fine if it explains the underlying idea
Thanks.
Update in response to Vlad's answer:
Maybe my idea to generalize the question was a bad one. My rules above were invented on the fly and just placeholders. One realistic rule would look like this:
"Every point included excludes every other point in the triagle above the chosen point"
By that rule and by choosing (2,1) I'd exclude
(2,2) - directly above
(1,3) (2,3) (3,3) - next line
and so on
So the rules are fixed, not general. They are unfortunately more complex than the X/Y samples I initially gave.
How about "the x coordinate of every point included is the exact sum of some subset of the y coordinates of the other included points". If you can come up with a fast algorithm for that simply-stated constraint problem then you will become very famous indeed.
My point being that the problem as stated is so vague as to admit NP-complete or NP-hard problems. Constraint optimization problems are incredibly hard; if you cannot put extremely tight bounds on the problem then it very rapidly becomes not analyzable by machines in polynomial time.
For some special rule types your task seems to be simple. For example, for your example rule #1 you need to choose a subset of all possible values of X, and than for each value from the subset assign an arbitrary Y.
For generic rules I doubt that it's possible to build an efficient algorithm without any AI.
My understanding of the problem is: Given a method bool property( Point x ) const, find all points the set for which property() is true. Is that reasonable?
The brute-force approach is to run all the points through property(), and store the ones which return true. The time complexity of this would be O( N ) where (a) N is the total number of points, and (b) the property() method is O( 1 ). I guess you are looking for improvements from O( N ). Is that right?
For certain kind of properties, it is possible to improve from O( N ) provided suitable data structure is used to store the points and suitable pre-computation (e.g. sorting) is done. However, this may not be true for any arbitrary property.
I found an interesting pairs matching game at http://xepthu.uhm.vn. The rule is simple, you have to find and connect two identical pokemon but the path between them is not blocked and the direction can't not be changed 3 times. Let's see an example:
I've think alot about the algorithm to check if the path between any 2 selected pokemon is valid but because I'm a newbie so I can't find any solution. Can you suggest me one in C#?
This is basically a path finding problem from graph theory. The fields in the grid are the nodes, and all adjacent fields are connected by an edge.
Path finding is a well-known problem, and there are many algorithms that solve this. Since your graph is quite small, the best solution here is probably just a brute force algorithm. A popular path finding algorithm is Dijkstra's algorithm.
Brute force: Start at some pokemon and explore all possible ways to see if one leads to an identical pokemon. You can stop exploring a way if the way is blocked or has more than 2 turns.
You'll need some "pointer" pointing to a field in the grid. The pointer can move left, right, up or down, provided that the field in that direction is not blocked. Move the pointer to an adjacent field. Remember where you came from and how many turns you made. Repeat this until you've reached your destination. Backtrack if the number of turns reaches 3. Make sure you don't run in circles.
Take a look at planar graphs. You will have to introduce a second condition: no more than four nodes may be traversed (start node - first direction change - second direction change - end node).