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#程序设计有所帮助。

相关文章

  • 深入多线程之:Wait与Pulse的使用详解

    深入多线程之:Wait与Pulse的使用详解

    本篇文章是对Wait与Pulse的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C#实现简易计算器功能(2)(窗体应用)

    C#实现简易计算器功能(2)(窗体应用)

    这篇文章主要为大家详细介绍了C#实现简易计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • C#委托现实示例分析

    C#委托现实示例分析

    这篇文章主要介绍了C#委托现实,实例分析了C#委托的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • datatable生成excel和excel插入图片示例详解

    datatable生成excel和excel插入图片示例详解

    excel导出在C#代码中应用己经很广泛了,下面讲了datatable生成excel、复制sheet页、删除sheet页、选中sheet页、另存excel文件、excel中插入图片等功能
    2014-01-01
  • C#动态执行批处理命令的方法

    C#动态执行批处理命令的方法

    这篇文章主要介绍了C#动态执行批处理命令的方法,可实现动态执行一系列控制台命令,并允许实时显示出来执行结果,需要的朋友可以参考下
    2014-11-11
  • C#实现将javascript文件编译成dll文件的方法

    C#实现将javascript文件编译成dll文件的方法

    这篇文章主要介绍了C#实现将javascript文件编译成dll文件的方法,涉及C#编译生成dll动态链接库文件的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • c#操作Redis的5种基本类型汇总

    c#操作Redis的5种基本类型汇总

    这篇文章主要给大家介绍了关于c#操作Redis的5种基本类型,文中通过示例代码介绍的非常详细,对大家的学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-07-07
  • C# 给PPT中的图表添加趋势线的方法

    C# 给PPT中的图表添加趋势线的方法

    本文内容分享通过C#程序代码给PPT文档中的图表添加数据趋势线的方法,需要的朋友可以参考下面文章的具体内容
    2021-09-09
  • c# 委托的常见用法

    c# 委托的常见用法

    这篇文章主要介绍了c# 委托的常见用法,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
    2020-08-08
  • Winform中进行MD5加密的实例

    Winform中进行MD5加密的实例

    下面小编就为大家带来一篇Winform中进行MD5加密的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01

最新评论