Image Data Structure [closed] - algorithm

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
An image(square image) can be stored as a tree: A node is white if the image is white, is black if the image is black, and is mixed if it contains both. White and black nodes are leaves, whereas a mixed node will have exactly 4 children, representing the 4 quadrants in the image. Given 2 images (trees), find the image representing their intersection. (Intersection: B^B -> B, B^W -> W, W^W->W)
This is a Google Interview Question

Here's a simple way to do it: Traverse both trees at the same time, using the same ordering. Build up an output tree while you do that. Then:
If you see a mixed node in both trees, output a mixed node
If you see a mixed node in one tree, but a white node in the other, output a white node (and ignore the mixed node in the traversal)
If you see a mixed node in one tree, but a black node in the other, copy the mixed node and it's children to the output tree
If you see two white nodes, output a white node
If you see two black nodes, output a black node
This has the possibility to create a mixed node that's actually only got white children, so you probably want a compression step where you traverse the tree collapsing mixed nodes that only have white children.
Edit: I think you could avoid the compression step by letting your recursion know whether output black nodes were found below (and putting in a white leaf if the answer was no).

Since both are represented as a binary tree you have to traverse a tree structure starting from root and check whats the color of nodes in both trees at any time and store the result in another tree. If either are black or white you stop traversing further. Else if anyone of them is mixed traverse further in that node until you find both of them of single color.

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.

Converting 2-3-4 to Red-Black Tree

I am familiar with converting individual 2-node, 3-node, and 4-nodes straight to Red-Black trees. And this Stackoverflow link is a good explanation 2-3-4 to Red-Black. However, I have a question about the example given in that link.
This is how the Stackoverflow question 2-3-4 to red-black was illustrated 2-3-4 to Red-Black
I highlighted the part that I am questioning. Why is it on this guide 4-node connected to 2-node I found and others on the internet, they say when encountering a 4 node connected to a 2 or 3 node, you need to switch the colors around. But in the StackOverflow example that I highlighted red, they didn't. Thanks
The following image does not imply that the colors of the red-back tree need to be swapped:
It merely describes the process of splitting a B-tree node, which leads to an alternative B-tree for the same data.
Then it shows how that different shape of the B-tree leads to a different coloring in the corresponding red-black tree, and how that new coloring is also a valid alternative.
But the translation from B-tree to red-black tree follows the rules you referred to:
If we look at the left side of the image, we see at the bottom layer of the B-tree a 4-node. According to the rules, this translates to a black node (c) with two red children (b and d). The 2-node at the root translates to a black node (a).
If we look at the right side of the image, we see two 2-nodes at the bottom layer of the B-tree. These translate each to a back node (b and d). The root 3-node is translated to a back node (a) with a red node (c) as child.
This is exactly what is depicted at the bottom of the image. The point is that these two variants are valid red-black trees for the same data, but derived from different shapes of B-trees.
Such a transition from the left to the right version might be needed when inserting a node. For instance, if an "e" would be added, then it cannot be added as a child of the "d" node in the red-black tree without recoloring. By switching to the right-side version of the red-black tree, the node can be added as (red) child of node "d".

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.

Most efficient data structure for Prim-Jarnik's algorithm [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
What is the most efficient data structure among these there:
Edge list
Adjacency list
Adjacency matrix
for executing Prim-Jarnik's algorithm and why?
By edge list I suppose you mean the list of all edges in a graph G ? Then that is the slowest of the three since you would need to traverse the entire list each time you are at a vertice u just to know which (u, v) pairs are in G. Adjacency matrix is somewhat faster than that, but still slow since you will need to traverse an entire row of the matrix to find the adjacent vertices and the respective edge weights. But if you have a dense graph, the adjacency matrix is just like an adjacency list. Adjacency list is the faster one supposing a not so dense graph such that traversing the list isn't more costly than directly accessing each column in the matrix row.
Said that, the key issue in Prim's algorithm is not actually this. To achieve its described computational complexity, you need to use a priority queue (and this is the part you should be concerned).

How to tell whether a red-black tree can have X black nodes and Y red nodes or not

I have an exam next week in algorithms, and was given questions to prepare for it. One of these questions has me stumped though.
"Can we draw a red-black tree with 7 black nodes and 10 red nodes? why?"
It sounds like it could be answered quickly, but I can't get my mind around it.
The CRLS gives us the maximum height of a RB tree with n internal nodes: 2*lg(n+1).
I think the problem could be solved using this lemma alone, but am not sure.
Any tips?
Since this is exam preparation, I don't want to give you a direct answer, but I think what you need to consider is the properties that govern how you build a Red-Black Tree:
A node is either red or black.
The root is black. (This rule is sometimes omitted from other definitions. Since the root can always be changed from red to black but not necessarily vice-versa this rule has little effect on analysis.)
All leaves are black.
Both children of every red node are black.
Every simple path from a given node to any of its descendant leaves contains the same number of black nodes.
(Stole these from the wikipedia page: http://en.wikipedia.org/wiki/Red-black_tree)
Given the count of nodes you listed, can you meet all of these properties?
the answer is simple.
As we know that a red node can have only black parent.The max no. of nodes will be when each black node's both children are red and, hence, every black node has red parent. So, for 'n' black nodes '2n' red node are possible.
Think it this way:
put the first node(which is root) & make it black
make both its children red
make left and right children of both these red nodes black and for all these black nodes,
follow the same procedure as followed with root until black node count reaches the given value (in this case 7)
hope this helped you visualize the solution.
The answer critically depends on whether your RB tree uses black dummy nodes at the leaves, and if so, they are included in the count of seven black nodes. If not, consider a complete tree of seven black nodes
*
/ \
* *
/\ /\
* * * *
You won't have much trouble adding ten red nodes.

Resources