Get the next item in the tree

The presence of a tree (logical in the database) with elements in the form

  1. List Item A
  2. List of items B
    1. List item C
      1. List item D
  3. List Element E
  4. List item F
    1. List of G Elements

and so on (the depth of nesting is not limited), I want to get the next node down (or up), starting from an arbitrary node.

Let's say List Item D is given. I want to write a GetNextNode() function that returns List Item E

My idea would be to do some recursions, but maybe there is a smarter way to handle this?

My question is:

How would you solve this?

EDIT 1:

The tree can be obtained using functions such as:

  • GetParentNode()
  • GetChildrenNodes()
  • GetNextSiblingNode()
  • etc.

Thus, it is similar, for example, to e Windows Forms TreeView .

+6
source share
5 answers

I had to do this several times. From memory:

 public Node GetBelowNode() { if (GetChildrenNodes().count > 0) return GetChildrenNodes()[0]; else if (GetNextSiblingNode() != null) return GetNextSiblingNode(); else { Node curr = this; Node parent; while (true) { parent = curr.GetParentNode(); if (parent == null) return null; else { if (parent.GetNextSiblingNode() != null) return parent.GetNextSiblingNode(); else curr = parent; } } } } 
+4
source

You can handle this through recursion or ... worst xD

I think there are only 3 main cases:

 private string getNext(TreeNode node) { if (node.FirstNode != null) { return node.FirstNode.Name; } else { if (node.NextNode != null) { return node.NextNode.Name; } else if (node.Parent.NextNode != null) { return node.Parent.NextNode.Name; } } return ""; } 

This does not work for every scenario. You should also look for the parent next node. Thanks to Vincent Vancalbergh for the comment ;-)

+1
source
 public Party Next { get { if (this.children.Count > 0) return this.children[0]; Party target = this; do { if (target.NextSibling != null) return target.NextSibling; } while ((target = target.Parent) != null); return null; } } public Party Previous { get { if (Parent != null && Parent.children.Count > 0 && this == Parent.children[0]) { return Parent; } Party target = this; do { if (target.PreviousSibling != null) { target = target.PreviousSibling; break; } } while ((target = target.Parent) != null); if (target != null) { while (target.children.Count > 0) { target = target.children[target.children.Count - 1]; } } return target; } } 
+1
source

Try this, maybe:

 TreeNode currentNode = treeView1.SelectedNode; treeView1.selectedNode = currentNode.NextNode; 
0
source

Since I got a great answer for the "lower" part, I will add my "up" part. Maybe this will help you; the upper part is similar to:

  • Get the previous brother.
  • If there is a previous brother, get the deepest child node of this brother or sister.
  • If there is no previous brother, get a direct parent.

To get the deepest brother (2.), I use the following code:

 function getDeepestChild( page ) dim result set result = nothing dim childPages set childPages = page.ChildPages if childPages.Count>0 then dim p set p = childPages(childPages.Count-1) ' recurse. set result = getDeepestChild( p ) else set result = page end if set getDeepestChild = result end function 

(Yes, I know this is VBScript, I really needed it in this language)

0
source

Source: https://habr.com/ru/post/1348058/


All Articles