1 Public classTree<t>whereT:icomparable<t>2 {3 /// <summary>4 ///Definition Tree5 /// </summary>6 PrivateT data;7 PrivateTree<t>Left ;8 PrivateTree<t>Right ;9 Ten /// <summary> One ///constructor Function A /// </summary> - /// <param name= "NodeValue" >two fork root node</param> - PublicTree (T nodevalue) the { - This. data =NodeValue; - This. left =NULL; - This. right =NULL; + } - + /// <summary> A ///Data Node Properties at /// </summary> - PublicT Nodedata - { - Get{return This. Data;} - Set{ This. data =value;} - } in - PublicTree<t>Lefttree to { + Get{return This. Left;} - Set{ This. left =value;} the } * $ PublicTree<t>RighttreePanax Notoginseng { - Get{return This. Right;} the Set{ This. right =value;} + } A the /// <summary> + ///The insertion node is less than the left side of the node, and is larger than the right side of the node - /// </summary> $ /// <param name= "NewItem" ></param> $ Public voidInsert (T newitem) - { -T Currentnodevalue = This. Nodedata; the if(Currentnodevalue.compareto (NewItem) >0) - {Wuyi if( This. Lefttree = =NULL) the { - This. Lefttree =NewTree<t>(newitem); Wu } - Else About { $ This. Lefttree.insert (newitem); - } - } - Else A { + if( This. Righttree = =NULL) the { - This. Righttree =NewTree<t>(newitem); $ } the Else the { the This. Righttree.insert (newitem); the } - } in } the the /// <summary> About ///first-order traversal around the root the /// </summary> the /// <param name= "root" ></param> the Public voidPreordertree (tree<t>root) + { - if(Root! =NULL) the {Bayi Console.Write (Root. Nodedata); the Preordertree (root.lefttree); the Preordertree (root.righttree); - } - } the the /// <summary> the ///Middle Sequence traversal Zogen right the /// </summary> - /// <param name= "root" ></param> the Public voidInordertree (tree<t>root) the { the if(Root! =NULL)94 { the Inordertree (root.lefttree); the Console.Write (Root. Nodedata); the Inordertree (root.righttree);98 } About } - 101 /// <summary>102 ///Post-sequential traversal of left and right roots103 /// </summary>104 /// <param name= "root" ></param> the Public voidPostordertree (tree<t>root)106 {107 if(Root! =NULL)108 {109 Postordertree (root.lefttree); the Postordertree (root.righttree);111 Console.Write (Root. Nodedata); the }113 } the the /// <summary> the ///Step -by-step: from the root node, access a node and then place the root node of the left and right subtree into the linked list, then delete the node117 ///iterate to the list without data118 /// </summary>119 Public voidWideordertree () - {121List<tree<t>> nodeList =NewList<tree<t>>();122Nodelist.add ( This);123tree<t> temp =NULL;124 while(Nodelist.count >0) the {126Console.Write (nodelist[0]. Nodedata);127temp = nodelist[0]; -Nodelist.remove (nodelist[0]);129 if(Temp.lefttree! =NULL) the {131 Nodelist.add (temp.lefttree); the }133 if(Temp.righttree! =NULL)134 {135 Nodelist.add (temp.righttree);136 }137 }138 Console.WriteLine ();139 } $ 141}View Code
This is the second time let people say two fork tree, must not be forgotten!
Re-Learn algorithm (1)--traverse binary tree