Binary Tree Puzzle [closed] - algorithm

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

Related

Tree data structure [closed]

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.

Is there an specific algorithm or way to solve this special tree problem? [duplicate]

This question already has answers here:
What is a good algorithm for getting the minimum vertex cover of a tree?
(7 answers)
Closed 2 years ago.
For example, I mark the node A in this binary tree. The nodes next to node A will also be marked at the same time. And I want to output the minimum nodes like A so I can mark all the nodes of the binary tree.
Is there any specific algorithm for these kind of question? Or any ideas to solve this question?
The scenario when I mark A:
All nodes will get marked when A&B are marked:
The output of this example should be 2.
If the tree is not too big, the easiest method seems to be to start from the root, and apply it recursively. Instead of thinking of "marking", it might help to think of it more as a "deleting", while keeping count. There are at most three ways to remove the root, so you just try them:
remove the root and its left and right node, calculate the sum of the marks for the remaining trees, call this number a
if present, remove the node left below the root and its neighbors, calculate the sum of the marks for the remaining trees, call this number b
if present, remove the node right below the root and its neighbors, calculate the sum of the marks for the remaining trees, call this number b
the minimal number of marks is minmum(a, b, c) + 1

Maximum number of red nodes in red black tree [closed]

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.

Concern about Depth First Search [closed]

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.

Can we have a red-black tree without any red nodes? [closed]

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.

Resources