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.
Related
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 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
A b tree is a generalized binary tree . How ?
A binary tree is a tree in which each node has at most 2 children. A b-tree of order m is a tree in which
Each node has at most m children.
Each internal node (not the root or a leaf) has at least ⌈m/2⌉ children. (⌈x⌉ means the ceiling of x, the least integer not less than x.)
Each non-leaf node (parent and all internal nodes) has at least 2 children.
All leaves appear on the same level.
(B tree nodes also have keys, but this is not directly part of the tree structure and does not concern us in this question.)
So some b-trees are binary trees. Every b-tree of order 2 is a binary tree. Some b-trees of higher order are binary trees if they happen not to have any nodes with more than 2 children.
b-trees of orders 5 and greater could be binary trees only if they are just a parent and two children, which are leaves. If a tree of order 5 or greater had any internal nodes, that node would be required to have at least ⌈5/2⌉ = 3 children, so it could not be a binary tree. b-trees of orders 3 and 4 could have internal nodes and still be binary trees.
The concepts of binary tree and b-tree overlap, but neither is a subset of the other in the sense that all requirements of one would satisfy the other. For the most part in programming, you are not going to mix uses of routines for binary trees and other routines for b-trees based on just how the current tree happens to be filled and arranged; on a particular set of data being managed, you would be working entirely with binary tree routines or entirely with b-tree routines.
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 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.
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 9 years ago.
Improve this question
I was solving my homework for the Data Structures and Algorithms course that I came across this question :
"Given two methods of traversing which can be pre-order and post-order , pre-order and in-order , post-order and in-order , how many binary trees can we extract ?"
Now I know that you certainly cannot find the binary tree from only one traversal order , but which of those two traversals would give you only one binary tree ? and how ? and the ones that do not exemplify one binary tree , how many binary trees do they exemplify and how can we count that number ?
I believe this source is useful. A pre and a post order traversal of a tree is not enough to uniquely reconstruct it without further restrictions. However an algorithm is shown how to reconstruct a tree from its post order and inorder travesal and as the last case is somewhat symmetric I believe this is algorithmic prove that a tree can be reconstructed uniquely from its inorder and any of the other traversals.
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.