In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to realize the depth-first traversal of the tree in c #, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
Depth first traversal of the tree
First, we define the tree node: public class Node {public Node (long value, bool visited) {Value = value; Visited = visited;} public long Value {get; set;} / / store the value of the node public bool Visited {get; set;}}
Then, we can happily write the DFS stack.
Public static long Fblc (int n) {Stack s = new Stack (); s.Push (new Node (n, false)); long sum = 0; long [] childrenResultMemo = new long [nyst1]; childrenResultMemo [0] = 1; childrenResultMemo [1] = 1; / / long children = 0 While (s.Any ()) {var cur = s.Pop (); if (cur.Visited = = false) {if (childrenResultMemo [cur.Value] = = 0) {cur.Visited = true If (childrenResultMemo [cur.Value-1]! = 0 & & childrenResultMemo [cur.Value-2]! = 0) {var result = childrenResultMemo [cur.Value-1] + childrenResultMemo [cur.Value-2]; childrenResultMemo [cur.Value] = result Sum + = result; s.Push (cur);} else {s.Push (cur) S.Push (new Node (cur.Value-1, false)); s.Push (new Node (cur.Value-2, false)) }} else {sum + = childrenResultMemo [cur.Value] / / to save the optimization of subtree results, there will be a special case to deal with} return sum;}
The core idea of the above algorithm is to traverse the stack and pop out of the top element of the stack, and skip it if you have already visited (visited is true). (the above code will have a special case to deal with due to the optimization of saving the results of the subtree, which will be described in detail below); otherwise, mark the visited of the current parent node as true, representing that it has been visited, and push to the stack; then push all its child nodes to the stack.
If you follow this line of thinking, the code will not be the same as above, the amount of code will be much less and much simpler, but the algorithm complexity will be about the same as recursive writing, because there are double calculations.
So what to do, there is only one solution, space for time, save the results of the subtree, if the corresponding subtree has been calculated to have the results, no longer traverse the depth of the next layer, directly use the results. We saved the results of the subtree in an array:
Long [] childrenResultMemo = new long [nasty 1]
Usually if the subtree already has a result, it should logically have been accessed. However, there are special cases, which are subtree 0 and subtree 1 at the beginning:
ChildrenResultMemo [0] = 1 childrenResultMemo [1] = 1
Just add up the results in the branch of this particular case:
Sum + = childrenResultMemo [cur.Value]; Thank you for reading this article carefully. I hope the article "how to realize the depth-first traversal of trees" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.