C# 二叉树遍历
C#完成的二叉树递归和非递归的遍历。BTreeNode是节点类,Visited是ENUM类型的表示当前节点是否被访问以及被访问的是左孩子还是右孩子(非递归后序遍历用到)
BTree是二叉树类,preVisit,InVisit,BackVisit分别是递归的前中后序遍历,preVisit1,InVisit1,BackVisit1分别是 非递归的前中后序遍历
public enum Visited
{
none,
left,
right
}
public class BTreeNode
{
public BTreeNode left;
public BTreeNode right;
public int data;
public Visited visited;
}
public class BTree
{
public BTreeNode btreenode;
public Stack<BTreeNode> NodeStack=new Stack<BTreeNode>();
public BTree(BTreeNode node)
{
this.btreenode = node;
}
public void preVisit(BTreeNode node)
{
visit(node);
if (node != null)
{
preVisit(node.left);
preVisit(node.right);
}
}
public void InVisit(BTreeNode node)
{
if (node != null)
InVisit(node.left);
visit(node);
if (node != null)
InVisit(node.right);
}
public void BackVisit(BTreeNode node)
{
if (node != null)
{
BackVisit(node.left);
BackVisit(node.right);
}
visit(node);
}
public void preVisit1(BTreeNode node)
{
while (node != null || NodeStack.Count > 0)
{
if (node == null)
{
node = NodeStack.Pop();
if (node.right != null)
{
visit(node.right);
NodeStack.Push(node.right);
}
node = null;
}
else
{
visit(node);
NodeStack.Push(node);
node = node.left;
}
}
}
public void InVisit1(BTreeNode node)
{
while (node != null || NodeStack.Count > 0)
{
if (node == null)
{
node = NodeStack.Pop();
visit(node);
if (node.right != null)
{
NodeStack.Push(node.right);
}
node = null;
}
else
{
NodeStack.Push(node);
node = node.left;
}
}
}
public void BackVisit1(BTreeNode node)
{
while (node != null || NodeStack.Count>0)
{
if (node == null)
node = NodeStack.Peek();
if (node.visited == Visited.none)
{
node.visited = Visited.left;
NodeStack.Push(node);
node = node.left;
}
else if (node.visited == Visited.left)
{
node.visited = Visited.right;
NodeStack.Pop();
NodeStack.Push(node);
node = node.right;
}
else if (node.visited == Visited.right)
{
visit(node);
NodeStack.Pop();
node = null;
}
}
}
private void visit(BTreeNode node)
{
if(node!=null)
Console.Out.Write(" "+node.data);
}
}
下面是构造树以及测试的方法:
BTreeNode node1 = new BTreeNode();
node1.data = 3;
node1.visited = Visited.none;
BTreeNode node2 = new BTreeNode();
node2.data = 4;
node2.visited = Visited.none;
BTreeNode node3 = new BTreeNode();
node3.data = 7;
node3.visited = Visited.none;
BTreeNode node4 = new BTreeNode();
node4.data = 5;
node4.visited = Visited.none;
BTreeNode node5 = new BTreeNode();
node5.data = 2;
node5.visited = Visited.none;
BTreeNode node6 = new BTreeNode();
node6.data = 9;
node6.visited = Visited.none;
node1.left = node2;
node1.right = node3;
node2.left = node4;
node2.right = node5;
node3.right = node6;
BTree btree = new BTree(node1);
Console.Write(" 前序递归遍历:");
btree.preVisit(btree.btreenode);
Console.WriteLine("");
Console.Write("前序非递归遍历:");
btree.preVisit1(btree.btreenode);
Console.WriteLine("");
Console.Write(" 中序递归遍历:");
btree.InVisit(btree.btreenode);
Console.WriteLine("");
Console.Write("中序非递归遍历:");
btree.InVisit1(btree.btreenode);
Console.WriteLine("");
Console.Write(" 后序递归遍历:");
btree.BackVisit(btree.btreenode);
Console.WriteLine("");
Console.Write("后序非递归遍历:");
btree.BackVisit1(btree.btreenode);
Console.WriteLine("");
Console.Read();