Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
In a red-black tree with n nodes, what is the maximum number of red nodes (assuming the root is black)?
Is it O(n)?
If the tree has n nodes and the root is black, there are n - 1 nodes left and n - 1 = O(n), so you're right.
If you want to count/bound number of red nodes in the tree more accurately, you have to know the topology of that tree.
For instance, if the tree is a complete binary tree, according to the definition of the red-black-tree, it can have no red nodes at all.
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 7 years ago.
Improve this question
If a graph unweighted G have the same number of Nodes and Edges , it is correct to assume that the graph G just contain one cycle ? Can be proved ?
EDIT: And all node are conected
If and only if there are one component in a graph. In other words, if from each node, there is a path to any node in a graph, you can assume that there are exactly one cycle.
You are assuming that all the nodes in the graph are connected. If the nodes in the graph are not connected, then answer to your question is no.
Also graph has to be a simple graph(https://en.wikipedia.org/wiki/Graph_(mathematics)?oldformat=true#Simple_graph)
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 7 years ago.
Improve this question
Recently I came across this interview puzzle.
Can Someone Explain me what this puzzle means..
You are given a binary tree. A light source is placed on the right of the tree. Print the list of all the nodes over which the light is falling directly.
Which Nodes will be shielded ? It's obvious the right nodes will get printed but if the left subtree is greater, then we have to include a couple of extra test cases , right?
Correct me , If I'm wrong!
You are correct. The rightmost subtree should be printed, and possibly some nodes of the tree that in the left subtree. In fact the rightmost node of every level of the tree should be printed. In the following example the nodes marked with an x should be the output
x
/ \
o x
/ \
o x
You could realize this with recursive backtracking, by traversing the tree and always taking the right path and print out the nodes you traversed.
If you reach a node withoud any children you go back to the parent node and continue on its left path until you reach a deeper level than before (here you start printing them out again) or until you reached a node wihtout children.
If you repreat this you will eventually get all the nodes that are touched by the light
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
If my directed graph is represented as its incidence matrix how do I apply topological sort on that graph? I think it can be done by finding null rows and removing them with their corresponding columns but this is not efficient.How can I do this more efficiently?
I assume this is homework. Try the following algorithm:
1) Identify all of the nodes with indegree 0 (no edge points into the node)
2) For each node from step 1, perform a depth-first-search walk starting from the node.
If the graph is a DAG (directed acyclic graph -- no directed cycles like A -> B, B -> C, C -> A), the order in which you see nodes is guaranteed to be a topological ordering.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Prove that if G is an undirected connected graph, then each of its edges is either in the depth-first search tree or is a back edge.
Now, from intuition and in class lectures by Steven Skiena, I know that the above holds true, since it dives all the way down, and then throw a rope back to a previous vertex. I also know that DFS is great in finding cycles.
However, my problem here is that I don't know how to 'prove' that the edge is either a tree edge or a back edge.
Consider the 4 cases possible (theoretically). An edge can be:
A tree edge
A back edge
Both a tree edge and a back edge
Neither a tree edge or a back edge
To prove what is needed, you need to show that cases 3 and 4 cannot happen, i.e. lead to a contradiction.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I wonder whether a red black tree should have at least one red node. Also, given a BST, if we can convert it into an RBT, is there a unique way to turn this tree into a red-black tree?
A quick glance at the properties of a red-black tree shows that there is no requirement for any node to be red. The only way red nodes come about is through property 5:
Every simple path from a given node to any of its descendant leaves contains the same number of black nodes.
This property is also satisfied by any perfect binary tree, so every perfect binary search tree with only black nodes is also a red-black tree. (I'm not sure if the textbook red-black tree algorithms ever produce these, though.)
Also, given a BST, if we can convert it into an RBT, is there a unique way to turn this tree into a red-black tree?
There is no single unique RBT for an arbitrary BST; there are always multiple equivalent RBTs, except for very shallow trees.