国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#非遞歸先序遍歷二叉樹實例

2020-01-24 01:37:20
字體:
供稿:網(wǎng)友

本文實例講述了C#非遞歸先序遍歷二叉樹的方法。分享給大家供大家參考。具體如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication5{ class Program {  static void Main(string[] args)  {   Node treeRoot = CreateTree();   scanTree(treeRoot);  }  private static void scanTree(Node treeRoot)  {   List<Node> list = new List<Node>();   list.Add(treeRoot);   Node point = treeRoot;   Write(treeRoot);   while (true)   {    if (!list.Contains(point))    { //上一輪是移除的操作     if (treeRoot.leftSon == point)     {//移除的是左結(jié)點      if (treeRoot.rightSon != null)      {       treeRoot = treeRoot.rightSon;       list.Add(treeRoot);       Write(treeRoot);       point = treeRoot;       continue;      }      list.Remove(treeRoot);      if (list.Count == 0)      {       break;      }      point = treeRoot;      treeRoot = list[list.Count - 1];     }     else     {//移除的是右結(jié)點      list.Remove(treeRoot);      if (list.Count == 0)      {       break;      }      point = treeRoot;      treeRoot = list[list.Count - 1];     }     continue;    }    if (treeRoot.leftSon != null)    {     treeRoot = treeRoot.leftSon;     Write(treeRoot);     list.Add(treeRoot);     point = treeRoot;     continue;    }    if (treeRoot.rightSon != null)    {     treeRoot = treeRoot.rightSon;     Write(treeRoot);     point = treeRoot;     list.Add(treeRoot);     continue;    }    if (treeRoot.leftSon == null && treeRoot.rightSon == null)    {     list.Remove(treeRoot);     if (list.Count == 0)     {      break;     }     point = treeRoot;     treeRoot = list[list.Count - 1];    }   }  }  public static void Write(Node node)  {   Console.WriteLine(node.Data);  }  private static Node CreateTree()  {   Node a = new Node("A");   a.leftSon = new Node("B");   a.rightSon = new Node("C");   a.leftSon.leftSon = new Node("D");   a.leftSon.rightSon = new Node("E");   a.rightSon.leftSon = new Node("F");   a.rightSon.rightSon = new Node("G");   a.leftSon.leftSon.leftSon = new Node("H");   a.leftSon.leftSon.rightSon = new Node("I");   return a;  } } class Node {  public string Data { get; set; }  public Node leftSon { get; set; }  public Node rightSon { get; set; }  public Node(string data)  {   Data = data;  } }}

希望本文所述對大家的C#程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 封丘县| 个旧市| 华安县| 凤冈县| 合川市| 习水县| 尼玛县| 稷山县| 丽水市| 湘阴县| 北海市| 隆子县| 枝江市| 锡林郭勒盟| 长宁县| 荣昌县| 交城县| 安顺市| 湟源县| 霍山县| 阿克苏市| 容城县| 文昌市| 隆子县| 千阳县| 西平县| 班玛县| 文山县| 建始县| 威远县| 聂拉木县| 长宁县| 潞城市| 英超| 瑞丽市| 永新县| 沅江市| 织金县| 红桥区| 灌南县| 辽源市|