Finding specific tree node in TreeView - c#

I'm adding tree nodes to TreeView programmatically based on values from DB.
I need to add child nodes to those nodes before. The only way to locate them is by myReportsNode.ChildNodes[int index], but this information is useless for me, as I need to find them by their value, or some unique id.
FindNode is not a good option, as I don't know exactly where they are, and looking for each node via itarating through the tree is a waste. I though of a dictionary for nodes.
Any ideas?

You need to use a tree traversal. If there is some sort of ordering, you could make sure they are loaded into the tree reflecting that ordering, and you can use a depth-first search and have logarithmic complexity (every bit as efficient as a tree-based Dictionary). A dictionary of nodes would be a waste of memory, since you already have a tree structure you can use--that is, if there is some sort of ordering.
If you would give us some sort of idea of how the data is in the tree, I could help even more.

Use strategy of using three four columns in your database table.
1) nodeId
2) nodeText
3) nodeValue
4) parentId
When a node is clicked, send request via ajax to find if this id has child, if yes bring its childs and append below it. Else no child found, no need to append.

Related

How to insert the new element on XMLNode in C#

I am trying to insert a new element into the XmlNode in C# code,
How to insert the <delimiter>##<delimiter> element in inside the "/TestBooks/template/field" root. (Screenshot1)
enter image description here
Inside the <field> element, i need to insert the <delimiter> element, based on id element <Id>11-09-2020-505</Id>. (Screenshot2)
enter image description here
First of all you really shouldn't use pictures in questions -- we are taking the time to type you an answer you can take the time to use copy and paste and format your questions.
I'd like to answer your questions but I'm concerned about the questions because it implies that you are adding in markers to aid in the parsing of the data. You really shouldn't need to parse XML.
There are many great parsers of XML including one built into C# you should not roll your own.
In the XML standard the order of the children not defined. If you put a child as the "first" child, there is no reason to expect that a parser would list it as first.
(To have order in children you should just add an order attribute)
Because of these reason it should not matter where in the list of children you add the child.
So we can tell you how to add a child but we can't "put it in a specific spot" since children don't have an order.
In summary, it is not possible to do what you ask.

Get hierarchical tree from database with minimum number of queries

Right now I have the following table schema:
Node_Id (INT)| NodeName (nvarchar(40)) | ParentNode(int, FK)
I want to retrieve hierarchical tree from that structure: structure simmilar to this one: {NodeId, NodeName, IEnumerable<Node> Children, bool hasChildren}
I see two solutions for this problem: first is tree traversal. I mean to load root nodes (where parent=null) and then for each node load it's children and recursively do this for these nodes. But each child load results in additional query to DB which is performance hit.
Another option I see would be to load flat strucure from database (the same as table schema) and then from it build hierarchical structure. That is performance hit to application server.
I was wondering if there any other solutions?
If you don't need to show all the tree children at once, immediately, you could load based on a specified parent node and then load on demand as the user expands the tree children. This would result in a lighter load on the application and DB servers.
In addition to the solutions you already mentioned you also have the possibility to use connect by / start with in oracle sql.
See this link for further infos: http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm

How does DataSet.ReadXml(TextReader) handle nested XML elements?

I'm a PHP programmer, and I'm trying to understand some code which I think is ASP.NET. This is also my first foray into XML. I don't have access to a Windows box to test on.
I need to produce XML output that third-party code can use. The third party wants to use our data instead of the data source they are currently using. I don't want to replicate the current XML structure exactly because it doesn't map well to our data.
The structure of the current XML is very flat. There are only a few nested elements and the third party doesn't make use of any of them. The third party does have a sub-contracted programmer, but he is very busy. Also, I want to understand, for myself, how this works.
This is an excerpt from a plugin for a custom CMS:
Dim obj_set As New Data.DataSet()
Using obj_reader As New System.Xml.XmlTextReader("http://www.example.com/xml_output.php")
obj_set.ReadXml(obj_reader)
End Using
Dim obj_view As Data.DataView = obj_set.Tables("profile").DefaultView
obj_view.Sort = "cname"
Dim obj_data As Data.DataTable = obj_view.ToTable()
So from what I have gathered so far, this code
reads the XML file into a DataSet
sorts the profile table by cname
creates a new DataTable from the sorted view
There is other code that stores the new table to, and retrieves it from, cache. Then there is code that loops through the table rows and maps the column names to template variables.
Sample excerpt of current XML structure:
<profiles>
<profile>
<cname>ABC Corporation</cname>
<fname>John</fname>
<lname>Smith</lname>
<sector>Widgets</sector>
<subsectors>
<subsector>Basic Widgets</subsector>
<subsector>Fancy Widgets</subsector>
</subsectors>
</profile>
</profiles>
So what happens to the subsectors data? Does the reader create a separate table for it? If so, how are the tables related?
Our data includes multiple contacts per company. I could just create multiple elements at the top level fname1, fname2, fname3 to keep the flat structure. But I was thinking a nested structure makes sense for this kind of data. The problem is that I don't understand if such a structural change is compatible with the plugin code.
What kinds of changes would need to be made to the plugin code to make use of nested elements?
I was stumped on this myself, and I don't know if you still are, but for reference to others here's what I found.
You are right in assuming that the reader creates a separate table for it. Being that a DataSet can hold multiple tables, each "level" of elements gets its own table. However, any nested elements that have nested elements of their own will get their own table. Essentially, it keeps creating tables until it reaches the bottom of the xml tree. If an element has no children, it gets added as a cell in the data table.
In your case,
dataSet.Tables[0] will hold the top level nodes (all the <.profiles>). But since the nested element <.profile> has elements of its own, Tables[0] will likely only have one row. The next level deeper, dataSet.Tables[1] will hold all <-profile> nodes. Although since <.subsectors> has sub-element <.subsector>, it will not be in Tables[1], but rather in Tables[2] which goes yet level deeper.
I know it has been a while since this was asked, but hopefully this will be helpful.

What data structure or mix of data structures would I use for a concurrent queue that allows for bulk and specific deletes?

Here is my problem, I need a data structure that behaves like a queue, but has some other properties:
I should be able to easily delete items given a tag (every item in this queue has a tag that groups them)
I also need to be able to delete one item given a key (all items added to the collection will have such a unique key). Here, if it simplifies things, I could remove by tag and key if it would make it faster.
This collection is used in a concurrent environment so using as little locking as possible would be awesome
It should have the usual FIFO properties of a queue. I do not need to access items not in head, but i need the deleting behavior above to work.
I'm using C# to build this solution but I'd be much more interested in algorithms and data structures definitions as I hardly believe any of the available collections meet my needs.
Papers, books, blog posts and any other kind of reference to this are really welcome.
Sounds like you can use a concurrent singly-linked list for the queue part. For the delete-by-key part you could keep a concurrent hash-table (striped locks are easy to do) pointing to nodes in the queue.
If that approach fails, look at how database systems do this. They can do all of what you want, concurrently. They maintain a primary copy of the data in a b-tree and maintain secondary b-tree indices. For locking, they use a concurrent hash-table. B-trees have nice concurrency properties because you can easily share-lock the upper parts of them even while you update the leaves under an exclusive lock.
I think you can use a doubly multi-linked lists and two hashtables.
One part of list for the queue
Another part for grouping nodes by tag
One hashtable for accessing a node by key
Another hashtable for accessing nodes by a tag
Examples in Python (I'm sorry...)
Inserting an element:
items_table['item_key'] = new_item
my_queue.tail.next = new_item
new_item.previous = my_queue.tail
my_queue.tail = new_item
new_item.next_by_tag = tags_table['item_tag'] #head of tag's list
tags_table['item_tag'].previous_by_tag = new_item
tags_table['item_tag'] = new_item
Removing a element by key:
item = key_table['node_key']
item.next.previous = item.previous
item.previous.next = item.next
item.next_by_tag.previous_by_tag = item.previous_by_tag
item.previous_by_tag.next_by_tag = item.next_by_tag
del item
Removing a element by tag:
def remove_elements_by_tag(tag_head):
if tag_head == None:
return
else:
remove_elements_by_tag(tag_head.next_by_tag)
tag_head.next.previous = tag_head.previous
tag_head.previous.next = tag_head.next
Something like that. Hope it helps.

Aging Data Structure in C#

I want a data structure that will allow querying how many items in last X minutes. An item may just be a simple identifier or a more complex data structure, preferably the timestamp of the item will be in the item, rather than stored outside (as a hash or similar, wouldn't want to have problems with multiple items having same timestamp).
So far it seems that with LINQ I could easily filter items with timestamp greater than a given time and aggregate a count. Though I'm hesitant to try to work .NET 3.5 specific stuff into my production environment yet. Are there any other suggestions for a similar data structure?
The other part that I'm interested in is aging old data out, If I'm only going to be asking for counts of items less than 6 hours ago I would like anything older than that to be removed from my data structure because this may be a long-running program.
A simple linked list can be used for this.
Basically you add new items to the end, and remove too old items from the start, it is a cheap data structure.
example-code:
list.push_end(new_data)
while list.head.age >= age_limit:
list.pop_head()
If the list will be busy enough to warrant chopping off larger pieces than one at a time, then I agree with dmo, use a tree structure or something similar that allows pruning on a higher level.
I think that an important consideration will be the frequency of querying vs. adding/removing. If you will do frequent querying (especially if you'll have a large collection) a B-tree may be the way to go:
http://en.wikipedia.org/wiki/B-tree
You could have some thread go through and clean up this tree periodically or make it part of the search (again, depending on the usage). Basically, you'll do a tree search to find the spot "x minutes ago", then count the number of children on the nodes with newer times. If you keep the number of children under the nodes up to date, this sum can be done quickly.
a cache with sliding expiration will do the job ....
stuff your items in and the cache handles the aging ....
http://www.sharedcache.com/cms/

Categories