Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given a Binary Tree, write a function to check whether the given Binary Tree is Complete Binary Tree or not.
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. source: wikipedia
My approach is do BFS using queue and count the no of nodes. Run a
loop till the queue is not null but break once you find one of the
below condition holds good:
left node is not present for a node
left node is present but right node is not present.
Now we can compare the count that we get from the above approach and
the original count of the nodes in the tree. If both equal then
complete binary tree else not.
Please tell me whether the approach is correct or not. Thanks.
This question is same as that of this. But i wan to verify my method here.
Edit:
The algorithm is verified by #Boris Strandjev below. I felt this is the easiest algorithm to implement out of some algorithms available in net. Sincere apologize if you do not agree with my assertion.
Your algorithm should solve the problem.
What you are doing with the BFS is entirely equivalent to drawing the tree and then tracing the nodes with your finger top-down and left-right. The first time you can not continue you stop tracing with your finger. If you have not counted all the nodes then the structure is not as expected obviously.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 months ago.
Improve this question
I was given an algorithm as below and my aim is to prove its incorrectness. The thing is that it woud be too obvious to use just counter-example, that is why I was looking for a more formal approach. I have thought the proof by induction, but in the past I had use it only to prove the correctness of an algorithm and I can't really figure out the opposite way.
GoodSort(A, left, right)
{
if (A[left] > A[right])
swap(A[left], A[right]);
if (left+1 >= right)
return;
pivot = floor((right-left+1)/3);
GoodSort(A, left, right-pivot);
GoodSort(A, left+pivot, right);
}
For any purpose but a class assignment where it's forbidden, a proof by counterexample, e.g. [3,2,4,0,1,4], would be ideal. As some commenters said, clarity and simplicity is desirable.
Assuming this is a class assignment and you need to categorize the set of inputs (or a set of inputs) where this will fail that's broader than a single counterexample, take some minimal input that fails, and analyze why it fails, then generalize that.
An algorithm can either be correct or incorrect, if you are having a hard time finding ways to prove its incorrectness you can rather try to prove its correctness. If you reach a nonsense you can then conclude that the algorithm is incorrect. This method is called Reductio ad Absurdum
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I am a little stuck on a problem in which we are supposed to fill in the values after running two iterations of the Bellman-Ford algorithm on a basic directed graph.
I believe I understand the first iteration and I understand the concept of "relaxing edge weights" as shorter paths are found. However, I don't see how the second iteration, in this particular problem, would yield any shorter paths than the ones located in the first iteration.
For example, I know that visiting node 'C' via the path of starting at node A, then going to node 'B' then going to node 'C' would have a total "cost" of 6+8 = 14. However, because the traversal order of this graph is: AB, AC, BC, BD, etc., the cost of reaching node C via node B (14) would never be saved because a shorter path to C directly from A would have already been found (yielding a cost of 7) I don't see how any additional iterations would give a shorter path length from A to C for example which seems to be the significance of the subsequent iterations.
upon closer inspection it appears that the data is indeed correct. It's just a poorly formatted question in the sense that the second iteration does not, in this particular case, yield any further "relaxation" of edges and is therefore misleading to those expecting to see a difference over the second iteration.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm not sure what kind of "list", could be considered as "linear list".
For example, if the concept "linear" means we have one and only one rule to say what is the "next" element: then "Circular list", should also be "linear list"?
If yes, then "general lists", although they could have high dimensional structure, but as long as we give the rule of how to find "next" element, could it be considered as "linear list"?
Circular lists are linear data structures. However, it is not sufficient to give a rule for finding next element: in order for the structure to be linear, a single element must not be the next element to more than one element.
For example, the structure below is not linear:
Although each node has at most one successor, node "C" is a successor to two other nodes - "B" and "F". The structure is, therefore, cannot be considered linear.
A list of linear data structures can be found here.
I'm not sure what context you are coming from with this question, but my understanding of "circular list" in general computer data structure terminology is a list where the last element points back to the first element, so that the list could be traversed infinitely. This has usefulness in certain applications.
Yes you are right that , linearly linked means that you have a specific method for reaching to a unique next node
Because of a little difference in implementation of circular lists ,that is,
None of the pointers points to a NULL and becoz of it's infinite nature
there tends to be a confusion ...
but
Circular Linked List is generally called as a linear Linked List only
Note that
a tree is called a non linear datatype because one node's next could be more than one node so not a unique next node hence ** tree is example of non linear**
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Currently reviewing how to construct a BST, and it seems like there are two "common" ways of constructing it. One way, like this example, simply puts everything inside a Node class and does all the operation within such Node class. Another way is to break it down to both Node and BST class, and construct the tree from there.
I can see the appeal for both, but what is the standard way of constructing s BST? or is it really just more of a personal preference?
There's no good reason to have a separate BST class. It doesn't have any properties that a Node doesn't also have. Any Node is also a BST.
Alternatively there is no good reason to have a Node class, just a BST class.
Whatever you call it, there's only one of it.
A binary tree (a binary search tree is a special case of a binary tree that satisfies a property known as binary search tree property at every node) is a recursive data structure. A binary tree is either
nothing (represented as null), or
a node with three (optionally four) things (a key, a left child and a right child).
The optional fourth part of a node can be a parent reference, which itself is a tree. This reference is useful in several useful tree operations like node deletion (and even traversal).
So, a binary tree can be sufficiently represented by a node and vice-versa and a separate BST or BinaryTree representation (for instance, as a 'class') is not needed since a binary tree can be seen as a reference to its "root" node.
Since balancing of a BST is critical for its performance, it's important to note that in practice (e.g. library code), red-black trees usually replace binary (search) trees.
With respect to this representation, a binary tree is no different from an n-ary tree.
There is no need to define a 'subtree' while defining a binary tree. A separate BST class is not needed because a Node is enough to represent an entire tree.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a list whose members are sets of 5 numbers chosen from the
integers 1 to 600 [or 0 to 599 for storage purposes].
I need to choose a sublist of this list such that among the sets in this
sublist, each integer in the 1 to 600 range appears exactly once, so a
sublist of 120 elements. My list has either 4200 or 840 elements in
it--I'll find out by running whether the bigger number is necessary.
I need any one such sublist.
This sounds like a standard problem to me, but I have no idea how to
search. Can someone help with providing an algorithm, please?
From Set Cover Problem
The greedy algorithm for set covering chooses sets according to one rule: at each stage, choose the set that contains the largest number of uncovered elements
Wikipedia seems to say that this algorithm works the best under plausible complexity assumptions.
I would boil it down to these steps:
Pick an element from the list (the first one, probably)
Pick the next element you come across where all 5 numbers are not yet represented in the sub-list
If you reach the end, go back to the beginning of the list and lower the criteria of step #2 to 4 numbers
Repeat steps 2 & 3 until you have covered all integers
Depending on the programming language you're using, there are ways of making this pretty quick.
Edit: the poster has explained that each integer must be used exactly once
So, what you really need to do is just continue adding elements until the element contains an integer that is already present in your subset. The "exactly" criterion takes precedent over the "not yet in the subset" criterion. You'll break out of the loop when you hit 120 subsets.
You may also want to keep track of the order in which you add elements to your subset, and when you hit a dead end (e.g., each of the elements remaining in the superset contains an integer that is already present in your subset) you backtrack one element and continue.
In order to backtrack and remember what combinations do not work, you will need to keep a list of "banned collections", and each time you decide whether to add a new element you should first make sure it's not in this list of banned collections. The best way (that I've found) to do this in Ruby is to store the Hash of the collection rather than the collection itself. This provides an inexpensive way to evaluate whether the prospective collection has already been tried and has led to a dead-end.
Good luck!