二叉树非递归遍历算法
namespace Tree

{
/**//// <summary>
/// 前、中序遍历所使用的堆栈
/// </summary>
public class TreeStack : Stack<TreeNode>
{
}

/**//// <summary>
/// 后序遍历所使用的结构体
/// </summary>
public struct TagNode
{
private TreeNode _node;
private bool _tag;
public TagNode(TreeNode node)
{
this._node = node;
this._tag = false;
}
public TreeNode Node
{
get
{
return this._node;
}
}
public bool Tag
{
get
{
return this._tag;
}
set
{
this._tag = value;
}
}
}

/**//// <summary>
/// 后序遍历所使用的堆栈
/// </summary>
public class TagNodeStack : Stack<TagNode>
{
}

/**//// <summary>
/// 二叉树结点类
/// </summary>
public class TreeNode
{
private TreeNode _parent;
private TreeNode _lChild = null;
private TreeNode _rChild = null;
private String _content;
public TreeNode(TreeNode parent, bool isLChild)
{
this._parent = parent;
if (isLChild)
{
this._parent._lChild = this;
}
else
{
this._parent._rChild = this;
}
}
public TreeNode()
{
_parent = null;
}
public TreeNode Parent
{
get
{
return this._parent;
}
}
public TreeNode LChild
{
get
{
return this._lChild;
}
}
public TreeNode RChild
{
get
{
return this._rChild;
}
}
public String Content
{
get
{
return this._content;
}
set
{
this._content = value;
}
}
}
}遍历算法如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace Tree

{
public delegate void Visitor(TreeNode node);
public class TreeVisitor
{
private Visitor visit = null;
public TreeVisitor(Visitor v)
{
this.visit = v;
}

/**//// <summary>
/// 中序遍历
/// </summary>
/// <param name="root"></param>
public void BT_InOrderNoRec(TreeNode root)
{
TreeStack nodes = new TreeStack();
while (root != null || nodes.Count != 0)
{
if (root != null)
{
nodes.Push(root);
root = root.LChild;
}
else
{
root = nodes.Pop();
this.visit(root);
root = root.RChild;
}
}
}

/**//// <summary>
/// 前序遍历
/// </summary>
/// <param name="root"></param>
public void BT_PreOrderNoRec(TreeNode root)
{
TreeStack nodes = new TreeStack();
while (root != null || nodes.Count != 0)
{
if (root != null)
{
this.visit(root);
nodes.Push(root);
root = root.LChild;
}
else
{
root = nodes.Pop();
root = root.RChild;
}
}
}

/**//// <summary>
/// 后序遍历
/// </summary>
/// <param name="root"></param>
public void BT_BckOrderNoRec(TreeNode root)
{
TagNodeStack nodes = new TagNodeStack();
while (root != null || nodes.Count != 0)
{
while (root != null)
{
TagNode node = new TagNode(root);
nodes.Push(node);
root = root.LChild;
}
while (nodes.Count != 0 && nodes.Peek().Tag)
{
this.visit(nodes.Pop().Node);
}
if (nodes.Count > 0)
{
TagNode temp = nodes.Pop();
temp.Tag = true;
nodes.Push(temp);
root = temp.Node.RChild;
}
}
}
}
}
class TreeNodeFactory
{
public static TreeNode CreateRoot(string content)
{
TreeNode root = new TreeNode();
root.Content = content;
return root;
}
public static TreeNode CreateLeftChild(TreeNode parent, string content)
{
TreeNode lChild = new TreeNode(parent);
lChild.Content = content;
parent.LChild = lChild;
return lChild;
}
public static TreeNode CreateRightChild(TreeNode parent, string content)
{
TreeNode rChild = new TreeNode(parent);
rChild.Content = content;
parent.RChild = rChild;
return rChild;
}
}
class Program
{
static void Main(string[] args)
{
TreeNode root = TreeNodeFactory.CreateRoot("A");
TreeNode lchild = TreeNodeFactory.CreateLeftChild(root, "B");
TreeNode rchild = TreeNodeFactory.CreateRightChild(root, "C");
TreeNodeFactory.CreateLeftChild(lchild, "D");
TreeNodeFactory.CreateRightChild(lchild, "E");
TreeVisitor myVisitor = new TreeVisitor(new Program().VisitNode);
myVisitor.BT_InOrderNoRec(root);
Console.WriteLine();
myVisitor.BT_PreOrderNoRec(root);
Console.WriteLine();
myVisitor.BT_BckOrderNoRec(root);
Console.Read();
}
private void VisitNode(TreeNode node)
{
Console.Write("\t"+node.Content);
}
}