Traversing/comparing tree structures with only parent references - c#

Consider a tree structure with nodes containing references to only their parent. So, the root node's parent will be null. This tree represents a class hierarchy. The goal is to search such a tree for a particular subtree, and if it doesn't exist, add it to the tree. Is there an algorithm that can be used to achieve this?
In this particular case, I have a C# WinForms application which uses this kind of structure. At the moment, an iterative breadth-first traversal is being used to traverse through such a tree and display it as a treeView. How this is being done is as follows: To represent the nodes of the tree itself, there is a class Data_Node. To perform the traversal, the nodes are placed into a queue. The queue contains instances of a data structure, QueueNode:
private class QueueNode
{
public Model.Data_node dataNode; // actual tree node
public QueueNode parent; // reference to the parent
public TreeNode treeNode; // node for treeView in the UI
}
The next step is to implement another such tree (assume it is already, then check whether it is a subtree of the first one, and add it if it is not.
I have researched examples of what I am trying to achieve, but they were only for binary search trees, with nodes having references to the child nodes. In my case, the nodes only have references to the parent node.

Related

Difference between XmlDocumentFragment and XmlNode in .Net

I don't understand the difference between XmlDocumentFragment and XmlNode.
I understand that the first one is a derived type of the second one.
Why is it better to use XmlDocumentFragment to create or edit the nodes of an XmlDocument in the code ?
I found answer here:
DocumentFragment is a "lightweight" or "minimal" Document object. It
is very common to want to be able to extract a portion of a document's
tree or to create a new fragment of a document. Imagine implementing
a user command like cut or rearranging a document by moving fragments
around. It is desirable to have an object which can hold such
fragments and it is quite natural to use a Node for this purpose.
While it is true that a Document object could fulfil this role, a
Document object can potentially be a heavyweight object, depending on
the underlying implementation. What is really needed for this is a
very lightweight object. DocumentFragment is such an object.
Furthermore, various operations -- such as inserting nodes as children
of another Node -- may take DocumentFragment objects as arguments;
this results in all the child nodes of the DocumentFragment being
moved to the child list of this node. The children of a
DocumentFragment node are zero or more nodes representing the tops of
any sub-trees defining the structure of the document. DocumentFragment
nodes do not need to be well-formed XML documents (although they do
need to follow the rules imposed upon well-formed XML parsed
entities, which can have multiple top nodes). For example, a
DocumentFragment might have only one child and that child node could
be a Text node. Such a structure model represents neither an HTML
document nor a well-formed XML document. When a DocumentFragment is
inserted into a Document (or indeed any other Node that may take
children) the children of the DocumentFragment and not the
DocumentFragment itself are inserted into the Node. This makes the
DocumentFragment very useful when the user wishes to create nodes that
are siblings; the DocumentFragment acts as the parent of these nodes
so that the user can use the standard methods from the Node
interface, such as insertBefore() and appendChild().

Syntax rewriting

I need to transform all of the properties of a certain class using Roslyn.
What is the recommended way to apply more than one transformation to a syntax tree without invalidating references into it?
Here is what I've tried and where I'm stuck:
In the first pass a descendant of CSharpSyntaxWalker is visiting all PropertyDeclarationSyntax nodes, and storing them in a list.
In the second pass a CSharpSyntaxRewriter is transforming the nodes while checking that each visited node equals one in the list before transforming it.
The problem with my attempt is: When I transform a property I add new fields to the class which causes the class to mutate. All the references in the list to the other properties become invalid in the new tree for the class.
It seems to be inefficient to revisit the whole class, and either way I cannot detect the property nodes already handled due to the reference difference.
I would not recommend to reference nodes from a SyntaxTree you want to modify. In your case just using a CSharpSyntaxRewriter in a single pass (without keeping references from a pre-processing pass) would be sufficient, because its VisitPropertyDeclaration method will only be called once per property, so there is no need to keep track of the nodes you've already modified.
The CSharpSyntaxRewriter also visits the nodes bottom-up, so the overrides should always be called with a node from the original tree. Most likely you have modified the node through the call to base.VisitPropertyDeclaration() before comparing its reference to the stored one. So you could still keep and compare references if you really wanted to.
public class PropertyRewriter : CSharpSyntaxRewriter
{
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
// 'node' should be still from the original syntax tree here
node = (PropertyDeclarationSyntax)base.VisitPropertyDeclaration(node);
// 'node' might be replaced here
return node;
}
}
Instead of keeping references to the nodes you want to modify, you could annotate them. Annotations to a node will survive modifications of the containing SyntraxTree as long as the node itself isn't replaced. You can add them like this:
node = node.WithAdditionalAnnotations(
new SyntaxAnnotation("propertyToChange", "todo"));
To retrieve the annotation again either use node.GetAnnotations("propertyToChange") or use GetAnnotatedNodesOrTokens("propertyToChange") to retrieve all nodes or tokens with an annotation of the given kind ("propertyToChange").

Recursive tuple

I am trying to create a recursive Tuple in C#. One side is a reference a class, and the other side goes into recursion to a likewise datastructure. The right side can be null, and this is when the recursion ends.
At the moment I am using an alias, but this does not seem to work.
using Relation = System.Tuple<Node, Relation>;
Directly typing this is not possible, as this goes infinite.
System.Tuple<Node, System.Tuple<Node, System.Tuple<Node, ...>>>
1.
Is this possible in C#?
2.
Can I do this with aliases?
Extra info
I am doing this because I need to keep track the parent of each node. A node can have multiple children. I cannot edit the node class.
I only need to go up the hierarchy. i.e. I don't need to know a nodes children. I use this for an algorithm where I calculate alot of nodes, and when I find one, I want to find the path to it from bottom to root.
You can create a Class for this.
class Relation
{
Node Node; // Node is another class
Tuple<Node, Relation> Relation;
}

should a tree have exactly one root node

I want to display a tree structure. Do i really need to give the user/tree a predefined hardcoded root node like "RootUnit" where he can add his children or descendants?
Does this make sense or cause only trouble when adding nodes?
If you have two roots then you have two trees.
A tree should have only one root. But you need not hardcode a root. Just treat the first created tree node as root.
A tree by definition has only one root and every child node has exactly one parent (except the root which has no parent). If these restrictions are not met then your tree is no longer a tree but a graph (oriented or not)
It depends on the context. From a strict mathematical definition, you cannot have multiple root nodes to a tree. However, there some implementations of trees that ignore that and have multiple top level nodes anyway (Such as the TreeView control you tagged this question with). You simply need to ask yourself if your particular program would be better or worse with multiple top level nodes. Given that we know nothing else about your program it's not a decision we can really make for you.
Rather than using the same constructor for every node, supply a default constructor used for the root node and one for everything else. It isn't ugly, and it works.
public Node()
{
// Set properties if you'd like.
// such as having no children yet or whatnot.
}
public Node(Node parent)
{
// Similar to Node()
}
See! Nice and clean.

TreeView, Access DB & Windows Form (C#)

hello friends I am in trouble, i have a school project in which I have to use tree view control to display fullname from DB in the following form.
suppose A is super memebr & under A there are others, and under other there could be other memeber and so on. its like chain system which will show referred member in hierarchical view.
Thank You
I would first create a class to represent a node in the tree: "Node".
That class should have as properties:
1. a List of child nodes
2. a string for full name
Then create instances of the Node class, drawing the data from your database.
Now you will have an in-memory representation of your data.
Now write a recursive function that takes an instance of Node class and returns an instance of TreeNode for use in the TreeView control. The function should recurse through all the child nodes of each Node instance.
Run that function on the root Node and you should have your TreeNode.

Categories

Resources