最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年07月15日 17:52:07   作者:落英繽紛  
這篇文章主要介紹了C#非遞歸先序遍歷二叉樹的實現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了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è)計有所幫助。

相關(guān)文章

  • 深入多線程之:Wait與Pulse的使用詳解

    深入多線程之:Wait與Pulse的使用詳解

    本篇文章是對Wait與Pulse的使用進行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#實現(xiàn)簡易計算器功能(2)(窗體應(yīng)用)

    C#實現(xiàn)簡易計算器功能(2)(窗體應(yīng)用)

    這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)簡易計算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C#委托現(xiàn)實示例分析

    C#委托現(xiàn)實示例分析

    這篇文章主要介紹了C#委托現(xiàn)實,實例分析了C#委托的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • datatable生成excel和excel插入圖片示例詳解

    datatable生成excel和excel插入圖片示例詳解

    excel導(dǎo)出在C#代碼中應(yīng)用己經(jīng)很廣泛了,下面講了datatable生成excel、復(fù)制sheet頁、刪除sheet頁、選中sheet頁、另存excel文件、excel中插入圖片等功能
    2014-01-01
  • C#動態(tài)執(zhí)行批處理命令的方法

    C#動態(tài)執(zhí)行批處理命令的方法

    這篇文章主要介紹了C#動態(tài)執(zhí)行批處理命令的方法,可實現(xiàn)動態(tài)執(zhí)行一系列控制臺命令,并允許實時顯示出來執(zhí)行結(jié)果,需要的朋友可以參考下
    2014-11-11
  • C#實現(xiàn)將javascript文件編譯成dll文件的方法

    C#實現(xiàn)將javascript文件編譯成dll文件的方法

    這篇文章主要介紹了C#實現(xiàn)將javascript文件編譯成dll文件的方法,涉及C#編譯生成dll動態(tài)鏈接庫文件的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • c#操作Redis的5種基本類型匯總

    c#操作Redis的5種基本類型匯總

    這篇文章主要給大家介紹了關(guān)于c#操作Redis的5種基本類型,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • C# 給PPT中的圖表添加趨勢線的方法

    C# 給PPT中的圖表添加趨勢線的方法

    本文內(nèi)容分享通過C#程序代碼給PPT文檔中的圖表添加數(shù)據(jù)趨勢線的方法,需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09
  • c# 委托的常見用法

    c# 委托的常見用法

    這篇文章主要介紹了c# 委托的常見用法,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-08-08
  • Winform中進行MD5加密的實例

    Winform中進行MD5加密的實例

    下面小編就為大家?guī)硪黄猈inform中進行MD5加密的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論

西藏| 高邮市| 莱州市| 宜章县| 衡阳县| 连山| 霍邱县| 安图县| 永济市| 普陀区| 沿河| 察哈| 合江县| 武城县| 宜阳县| 璧山县| 高安市| 寻甸| 兴文县| 郎溪县| 彰化县| 潮州市| 卢氏县| 闸北区| 晴隆县| 乌兰浩特市| 慈溪市| 甘孜县| 辉南县| 中江县| 马公市| 滨海县| 钦州市| 枞阳县| 宜宾市| 宝应县| 苍梧县| 尼玛县| 务川| 泰州市| 漳州市|