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.
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 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 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 8 years ago.
Improve this question
If I have an array of unsorted numbers and a number I'm looking for, I believe there's no way of checking if my number is in it except by going through each member and comparing.
Now, in mathematics and various theoretical branches I've been interested in, there's often the pattern that you usually get what you put in. What I mean is, there's usually an explanation for every unexpected result. Take the Monty Hall problem as an example. It seems counter-intuitive until you realize the host adds more information into the situation because he knows what door the car is behind.
Since you're iterating on the array, instead of just getting a yes or no answer, you also get the exact location of the element (if it's there). Wouldn't it then make sense that there's an algorithm that's less complex, but give you ONLY a single bit of information?
Am I completely off base here?
Is there an actual correlation between the amount of information you get and the complexity of an algorithm? What's the theory behind the relationship between the amount of information you get from an algorithm and it's complexity?
Yes, you're completely off base, sorry!
Algorithmic complexity is defined in terms of how many operations it takes to solve the problem of size N. If the array has N elements in it, then there is no way of determining whether the value appears in the array other than checking all N elements. That makes it linear, or O(N).
The fact that you can also determine the location of the value in O(N) (as indeed you can) doesn't mean that you can solve the simpler problem in less time.
When you are searching an array, indexing is the price that you pay for having an array. An ability to access an element by index is inherent in the structure of the array: in other words, once you say "I am going to search an array" (not a collection, but specifically an array) you have already paid for the index. At this point, there is no way to get rid of it, and the O(n) cost associated with searching the array.
However, this is not the only solution: if you agree to drop the ability to index as a requirement, you could build a collection that gives you a yes/no answer much faster. For example, if you use a hash table, your search time becomes O(1). Of course there is no associated index in a hash table: inability to access items in arbitrary order is your payment for an ability to check for presence or absence of items in constant time.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am trying to understand basic chess algorithms. I have not read the literature in depth yet but after some cogitating here is my attempt:
1) Assign weight values to the pieces(i.e. a bishop is more valuable than a pawn)
2) Define heuristic function that attaches a value to a particular move
3) Build minimax tree to store all possible moves. Prune the tree via alpha/beta pruning.
4) Traverse the tree to find the best move for each player
Is this the core "big picture" idea of chess algorithms? Can someone point me to resources that go more in depth regarding chess algorithms?
Following is an overview of chess engine development.
1. Create a board representation.
In an object-oriented language, this will be an object that will represent a chess board in memory. The options at this stage are:
Bitboards
0x88
8x8
Bitboards is the recommended way for many reasons.
2. Create an evaluation function.
This simply takes a board and side-to-evaluate as agruments and returns a score. The method signature will look something like:
int Evaluate(Board boardPosition, int sideToEvaluateFor);
This is where you use the weights assigned to each piece. This is also where you would use any heuristics if you so desire. A simple evaluation function would add weights of sideToEvaluateFor's pieces and subtract weights of the opposite side's pieces. Such an evaluation function is of course too naive for a real chess engine.
3. Create a search function.
This will be, like you said, something on the lines of a MiniMax search with Alpha-Beta pruning. Some of the popular search algorithms are:
NegaMax
NegaScout
MTD(f)
Basic idea is to try all different variations to a certain maximum depth and choose the move recommended by the variation which results in highest score. The score for each variation is the score returned by Evaluation method for the board position at the maximum depth.
For an example of chess engine in C# have a look at https://github.com/bytefire/shutranj which I put together recently. A better open source engine to look at is StockFish (https://github.com/mcostalba/Stockfish) which is written in C++.
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.