Data structures and algorithms (C # implementation) series-demonstration (II)
Heavenkiller (original)
Public static void ShowGeneralTree_travel ()
{
IEnumerator tmpIEnum;
Tree. TraversalType travelType = 0;
// --------------------- Prompt ----------------------------
Console. WriteLine ("please choose a No. of a item you want to travel :");
Console. WriteLine ("1. BreadthFirst ----- span traversal ");
Console. WriteLine ("2. PreDepthFirst ----- forward traversal ");
Console. WriteLine ("3. InDepthFirst ---- middle-order traversal ");
Console. WriteLine ("4. PostDepthFirst ---- post-order traversal ");
Switch (Console. ReadLine ())
{
Case "1": // Show Stack
TravelType = Tree. TraversalType. Breadth;
Console. WriteLine ("span traversal ");
Break;
Case "2": // SortedList
TravelType = Tree. TraversalType. PreDepth;
Console. WriteLine ("Forward traversal ");
Break;
Case "3 ":
TravelType = Tree. TraversalType. InDepth;
Console. WriteLine ("sequential traversal ");
Break;
Case "4 ":
TravelType = Tree. TraversalType. PostDepth;
Console. WriteLine ("sequential traversal ");
Break;
Default:
Break;
}
// Construct a generalized tree generaltree
GeneralTree A = new GeneralTree ("");
GeneralTree B = new GeneralTree ("B ");
GeneralTree C = new GeneralTree ("C ");
GeneralTree D = new GeneralTree ("D ");
GeneralTree E = new GeneralTree ("E ");
GeneralTree F = new GeneralTree ("F ");
A. AttackSubtree (B );
A. AttackSubtree (C );
B. AttackSubtree (D );
B. AttackSubtree (E );
A. AttackSubtree (F );
// Show the operation
Console. WriteLine ("A. AttackSubtree (B )");
Console. WriteLine ("A. AttackSubtree (C )");
Console. WriteLine ("B. AttackSubtree (D )");
Console. WriteLine ("B. AttackSubtree (E )");
Console. WriteLine ("A. AttackSubtree (F )");
//--------------------------------------------------------
A. SetTraversalType (travelType); // you can specify the traversal type.
TmpIEnum = A. GetEnumerator ();
// Console. WriteLine ("begin to depthfist travel :");
While (tmpIEnum. MoveNext ())
{
Console. WriteLine (t