前序,中序,后序遍历
Posted zjone391
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了前序,中序,后序遍历相关的知识,希望对你有一定的参考价值。
public enum BinaryTreeTraversal
{
PreOrder,
InOrder,
PostOrder
}
public class BianaryTreeNode
{
public BianaryTreeNode Left
{
get;
set;
}
public BianaryTreeNode Right
{
get;
set;
}
public int Data
{
get;
set;
}
public BianaryTreeNode(int data)
{
this.Data = data;
}
}
public void PreOrderTraversal(Action<int> action, BianaryTreeNode root)
{
if (root == null)
{
return;
}
action(root.Data);
PreOrderTraversal(action, root.Left);
PreOrderTraversal(action, root.Right);
}
public void InOrderTraversal(Action<int> action, BianaryTreeNode root)
{
if (root == null)
{
return;
}
InOrderTraversal(action, root.Left);
action(root.Data);
InOrderTraversal(action, root.Right);
}
public void PostOrderTraversal(Action<int> action, BianaryTreeNode root)
{
if (root == null)
{
return;
}
PostOrderTraversal(action, root.Left);
PostOrderTraversal(action, root.Right);
action(root.Data);
}
public void PrintTree(BianaryTreeNode root, BinaryTreeTraversal traverse)
{
Action<int> PrintValue = delegate(int v)
{
Console.WriteLine(v);
};
switch (traverse)
{
case BinaryTreeTraversal.PreOrder:
PreOrderTraversal(PrintValue, root);
break;
case BinaryTreeTraversal.InOrder:
InOrderTraversal(PrintValue, root);
break;
case BinaryTreeTraversal.PostOrder:
PostOrderTraversal(PrintValue, root);
break;
default:
break;
}
}
static void Main(string[] args)
{
}
以上是关于前序,中序,后序遍历的主要内容,如果未能解决你的问题,请参考以下文章