首页 > 代码库 > C# 二叉树遍历
C# 二叉树遍历
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;
}
{
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);
}
}
{
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 = http://www.mamicode.com/3;
node1.visited = Visited.none;
BTreeNode node2 = new BTreeNode();
node2.data = http://www.mamicode.com/4;
node2.visited = Visited.none;
BTreeNode node3 = new BTreeNode();
node3.data = http://www.mamicode.com/7;
node3.visited = Visited.none;
BTreeNode node4 = new BTreeNode();
node4.data = http://www.mamicode.com/5;
node4.visited = Visited.none;
BTreeNode node5 = new BTreeNode();
node5.data = http://www.mamicode.com/2;
node5.visited = Visited.none;
BTreeNode node6 = new BTreeNode();
node6.data = http://www.mamicode.com/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();
node1.data = http://www.mamicode.com/3;
node1.visited = Visited.none;
BTreeNode node2 = new BTreeNode();
node2.data = http://www.mamicode.com/4;
node2.visited = Visited.none;
BTreeNode node3 = new BTreeNode();
node3.data = http://www.mamicode.com/7;
node3.visited = Visited.none;
BTreeNode node4 = new BTreeNode();
node4.data = http://www.mamicode.com/5;
node4.visited = Visited.none;
BTreeNode node5 = new BTreeNode();
node5.data = http://www.mamicode.com/2;
node5.visited = Visited.none;
BTreeNode node6 = new BTreeNode();
node6.data = http://www.mamicode.com/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();
C# 二叉树遍历
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。