Algorithm note (with root tree traversal)

Source: Internet
Author: User
Public class treenode {Public String key {Get; set;} public object data {Get; set;} public treenode parent; public list <treenode> children {Get; set ;} public treenode (string key, object data) {children = new list <treenode> (); this. key = key; this. data = data;} public void addchild (treenode node) {This. children. add (node); node. parent = This;} public void delchild (treenode node) {This. chil Invalid. remove (node) ;}} public class trees {public treenode root {Get; Set ;}/// <summary> /// non-recursive tree traversal method, here we use the sequential traversal of the parent and child. if you want to have a child and a father, you need to use two stacks and two loops. /// </Summary> /// <Param name = "listcurrnode"> </param> Public void listchildren (Action <treenode> listcurrnode) {If (root = NULL) {return;} // the first parent and then child stack <treenode> _ nodesstack1 = new stack <treenode> (); While (_ nodesstack1.count> 0) {treenode node = _ Nodesstack1.pop (); listcurrnode (node); foreach (VAR item in node. children) {_ nodesstack1.push (item) ;}// child and parent. in fact, the left-and right-Order Access to Binary Trees also requires two stacks. _ nodesstack1.clear (); _ nodesstack1.push (Root); stack <treenode> _ nodesstack2 = new stack <treenode> (); While (_ nodesstack1.count> 0) {treenode node = _ nodesstack1.pop (); _ nodesstack2.push (node); foreach (VAR item in node. children) {_ nodesstack1.push (item );}} While (_ nodesstack2.count> 0) {treenode node = _ nodesstack1.pop (); listcurrnode (node) ;}/// <summary> // non-recursive middle order, linear time, constant extra auxiliary space, cannot modify the tree (not temporary), can traverse any Cross Tree. for k-tree, the comparison time for each return is <= K, // because each node needs to be returned at most once, the return is compared to <= KN, the tree traverses its own O (n ). traversal or linear time. /// In fact, if you can modify the tree, such as setting the access flag, you can directly return it and continue to access 1st unaccessed subnodes. /// The following algorithm uses this idea. Instead of directly setting the flag, it uses the array pointer feature. /// The expression of the right left sub-brother can also be implemented by any decision tree, but the return Traversal method is different (simpler ). /// if K child pointer variables are used to represent child nodes, it is also applicable, but you only need to default an access logic order. /// note: the access sequence of the first parent and the second child is used here. If you need to have the first child and then the second child, you can also add a child access to complete the return flag, modify the Code a little. /// </Summary> /// <Param name = "listcurrnode"> traverse visitors </param> Public void listall1 (Action <treenode> listcurrnode) {int thequitfrom =-1; //-1 indicates that the child is returned from the parent node, and 0-n indicates that the child is returned from the K. treenode thecurrnode = root; while (thecurrnode! = NULL) {treenode thetmp = NULL; If (thequitfrom <0) // just came from the parent node (thequitfrom =-1) {listcurrnode (thecurrnode); // when it came from the parent node, access 1st children (index 0 ). if (thecurrnode. children. count> 0) {thetmp = thecurrnode. children [0];} else {thetmp = NULL;} else // returned from the child node traversal, go to the next child node. {If (thequitfrom + 1 <thecurrnode. children. count) {thetmp = thecurrnode. children [thequitfrom + 1]; thequitfrom =-1;} else // the access to the child node is complete. If thetmp is left blank, the backend policy is continued. {thetmp = NULL; // if you want to subscribe to the parent, you need to mark it here} If (thetmp = NULL) // If thetmp is null, it indicates that the traversal fails. You need to go back to the sibling node {// if the current node is root, it indicates that the request has been returned from the root node, and the entire tree has been traversed, // It is also guaranteed that the getchildrenindex will not return-1. if (thecurrnode = root) break; thequitfrom = getchildrenindex (thecurrnode. Parent, thecurrnode); thecurrnode = thecurrnode. parent;} else // if it is not empty, access the thetmp node as the current node. {// In this case, thequitfrom must be-1. because it is initially-1, this value is changed only when it is returned. // if there is a returned brother that can be accessed, thequitfrom will be set to-1. If not, you can only continue. if you can continue the access, thequitfrom must be-1. thecurrnode = thetmp ;}}/// <summary> /// obtain the location of the child node in the parent node. /// </Summary> /// <Param name = "parent"> </param> /// <Param name = "child"> </param> /// <returns> </returns> private int getchildrenindex (treenode parent, treenode child) {for (INT I = 0; I <parent. children. count; I ++) {If (parent. children [I] = Child) {return I ;}// actually, putting it back here-1 cannot be executed. return-1 ;}}

This article references the csdn user's article: http://blog.csdn.net/acrazer/article/details/4454508

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.