C#非递归先序遍历二叉树实例

 更新时间:2015年07月15日 17:52:07   作者:落英缤纷  
这篇文章主要介绍了C#非递归先序遍历二叉树的实现方法,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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)
     {//移除的是左结点
      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
     {//移除的是右结点
      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#程序设计有所帮助。

相关文章

  • C#程序员应该养成的程序性能优化写法

    C#程序员应该养成的程序性能优化写法

    工作和生活中经常可以看到一些程序猿,写代码的时候只关注代码的逻辑性,而不考虑运行效率,其实这对大多数程序猿来说都是没有问题的,不过作为一只有理想的CodeMonkey,我还是希望给大家分享一些性能优化心得
    2017-08-08
  • C#使用Twain协议开发一个高扫仪对接功能

    C#使用Twain协议开发一个高扫仪对接功能

    这篇文章主要为大家详细介绍了C#如何使用Twain协议开发一个高扫仪对接功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02
  • C#中实现AES算法加密解读

    C#中实现AES算法加密解读

    这篇文章主要介绍了C#中实现AES算法加密实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • 采用C#实现软件自动更新的方法

    采用C#实现软件自动更新的方法

    这篇文章主要介绍了采用C#实现软件自动更新的方法,非常实用的功能,需要的朋友可以参考下
    2014-08-08
  • 支持多类型数据库的c#数据库模型示例

    支持多类型数据库的c#数据库模型示例

    本文为大家提供一个c#数据库访问模型,支持多类型数据库,简单抽取数据库访问函数,大家参考使用吧
    2014-01-01
  • C#简单实现文件上传功能

    C#简单实现文件上传功能

    这篇文章主要介绍了C#简单实现文件上传功能,利用MVC+EF+LigerUI 实现的upload上传功能,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • 关于C#委托三种调用的分享使用

    关于C#委托三种调用的分享使用

    这篇文章主要介绍了关于C#委托三种调用的分享使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • C#中PuppeteerSharp库的应用详解

    C#中PuppeteerSharp库的应用详解

    PuppeteerSharp是一个针对Google Chrome浏览器的高级API库,这篇文章主要为大家详细介绍了PuppeteerSharp库在C#中的具体应用,需要的小伙伴可以了解下
    2024-01-01
  • C# Sqlite数据库的搭建及使用技巧

    C# Sqlite数据库的搭建及使用技巧

    这篇文章主要介绍了C# Sqlite数据库的搭建及使用技巧,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-08-08
  • C#实现的二维数组排序算法示例

    C#实现的二维数组排序算法示例

    这篇文章主要介绍了C#实现的二维数组排序算法,涉及C#针对二维数组的遍历、判断、排序等相关操作技巧,需要的朋友可以参考下
    2017-12-12

最新评论