二叉树遍历,很是陈词滥调了。一般分三种遍历方式(先序遍历、中序遍历、后序遍历)。只是以前自己一直真正的去写过。最近上班没有什么事,就随便写写,填塞一下自己上班空闲时空虚的心情。
一个二叉树节点所持有的信息有哪些?我的这个二叉树,每个节点有四个信息,分别是:父节点信息、左子节点信息、右子节点信息以及节点的内容。
详细的不描述了,让代码来说话吧:
代码
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp_BinaryTree
{
public class Node < T >
{
// 节点数据
private T _Data;
// 左子节点
private Node < T > _LNode;
// 右子节点
private Node < T > _RNode;
// 父节点
private Node < T > _PNode;
public Node()
{
}
public Node(T data)
{
_Data = data;
}
public T Data
{
get { return _Data; }
set { _Data = value; }
}
public Node < T > LNode
{
get { return _LNode; }
set { _LNode = value; }
}
public Node < T > RNode
{
get { return _RNode; }
set { _RNode = value; }
}
public Node < T > PNode
{
get { return _PNode; }
set { _PNode = value; }
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp_BinaryTree
{
public class Node < T >
{
// 节点数据
private T _Data;
// 左子节点
private Node < T > _LNode;
// 右子节点
private Node < T > _RNode;
// 父节点
private Node < T > _PNode;
public Node()
{
}
public Node(T data)
{
_Data = data;
}
public T Data
{
get { return _Data; }
set { _Data = value; }
}
public Node < T > LNode
{
get { return _LNode; }
set { _LNode = value; }
}
public Node < T > RNode
{
get { return _RNode; }
set { _RNode = value; }
}
public Node < T > PNode
{
get { return _PNode; }
set { _PNode = value; }
}
}
}
那么,我要遍历的二叉树对象又是谁呢?如下图所示,这是我即将要遍历的二叉树。
看到这个图形,大家以可以知道,分三种方式遍历这棵二叉树的结果:
先序遍历:A B D G H C E F
中序遍历:B G D H A E C F
后序遍历:G H D B E F C A
以上这个结果是我们分析所得,那么我们用程序写出来又是什么样子的呢,下面就来实践一下吧!
代码
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp_BinaryTree
{
class Program
{
static void Main( string [] args)
{
// 先初始化二叉树(要遍历它,当然要先对它进行初始化了~~)
// 先初始化A/B/C/D/E/F/G/H这些节点
Node < string > nodeA = new Node < string > ( " A " );
Node < string > nodeB = new Node < string > ( " B " );
Node < string > nodeC = new Node < string > ( " C " );
Node < string > nodeD = new Node < string > ( " D " );
Node < string > nodeE = new Node < string > ( " E " );
Node < string > nodeF = new Node < string > ( " F " );
Node < string > nodeG = new Node < string > ( " G " );
Node < string > nodeH = new Node < string > ( " H " );
// 再将这些节点建立关系,让其形成一棵名符其实的二叉树
SetNodeInfo < string > ( ref nodeG, null , null , nodeD);
SetNodeInfo < string > ( ref nodeH, null , null , nodeD);
SetNodeInfo < string > ( ref nodeD, nodeG, nodeH, nodeB);
SetNodeInfo < string > ( ref nodeB, null , nodeD, nodeA);
SetNodeInfo < string > ( ref nodeE, null , null , nodeC);
SetNodeInfo < string > ( ref nodeF, null , null , nodeC);
SetNodeInfo < string > ( ref nodeC, nodeE, nodeF, nodeA);
SetNodeInfo < string > ( ref nodeA, nodeB, nodeC, null );
while ( true )
{
Console.WriteLine();
Console.WriteLine( " 1、先序遍历 " );
Console.WriteLine( " 2、中序遍历 " );
Console.WriteLine( " 3、后序遍历 " );
Console.Write( " input num: " );
string num = Console.ReadLine();
switch (num)
{
case " 1 " : RootFirst < string > (nodeA); break ;
case " 2 " : RootSecond < string > (nodeA); break ;
case " 3 " : RootLast < string > (nodeA); break ;
default : break ;
}
}
}
// 设置节点信息
protected static void SetNodeInfo < T > ( ref Node < T > node, Node < T > l, Node < T > r, Node < T > p)
{
node.LNode = l;
node.RNode = r;
node.PNode = p;
}
// 先序遍历
protected static void RootFirst < T > (Node < T > root)
{
if (root != null )
{
Console.Write(root.Data + " " );
RootFirst < T > (root.LNode);
RootFirst < T > (root.RNode);
}
}
// 中序遍历
protected static void RootSecond < T > (Node < T > root)
{
if (root != null )
{
RootSecond < T > (root.LNode);
Console.Write(root.Data + " " );
RootSecond < T > (root.RNode);
}
}
// 后序遍历
protected static void RootLast < T > (Node < T > root)
{
if (root != null )
{
RootLast < T > (root.LNode);
RootLast < T > (root.RNode);
Console.Write(root.Data + " " );
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp_BinaryTree
{
class Program
{
static void Main( string [] args)
{
// 先初始化二叉树(要遍历它,当然要先对它进行初始化了~~)
// 先初始化A/B/C/D/E/F/G/H这些节点
Node < string > nodeA = new Node < string > ( " A " );
Node < string > nodeB = new Node < string > ( " B " );
Node < string > nodeC = new Node < string > ( " C " );
Node < string > nodeD = new Node < string > ( " D " );
Node < string > nodeE = new Node < string > ( " E " );
Node < string > nodeF = new Node < string > ( " F " );
Node < string > nodeG = new Node < string > ( " G " );
Node < string > nodeH = new Node < string > ( " H " );
// 再将这些节点建立关系,让其形成一棵名符其实的二叉树
SetNodeInfo < string > ( ref nodeG, null , null , nodeD);
SetNodeInfo < string > ( ref nodeH, null , null , nodeD);
SetNodeInfo < string > ( ref nodeD, nodeG, nodeH, nodeB);
SetNodeInfo < string > ( ref nodeB, null , nodeD, nodeA);
SetNodeInfo < string > ( ref nodeE, null , null , nodeC);
SetNodeInfo < string > ( ref nodeF, null , null , nodeC);
SetNodeInfo < string > ( ref nodeC, nodeE, nodeF, nodeA);
SetNodeInfo < string > ( ref nodeA, nodeB, nodeC, null );
while ( true )
{
Console.WriteLine();
Console.WriteLine( " 1、先序遍历 " );
Console.WriteLine( " 2、中序遍历 " );
Console.WriteLine( " 3、后序遍历 " );
Console.Write( " input num: " );
string num = Console.ReadLine();
switch (num)
{
case " 1 " : RootFirst < string > (nodeA); break ;
case " 2 " : RootSecond < string > (nodeA); break ;
case " 3 " : RootLast < string > (nodeA); break ;
default : break ;
}
}
}
// 设置节点信息
protected static void SetNodeInfo < T > ( ref Node < T > node, Node < T > l, Node < T > r, Node < T > p)
{
node.LNode = l;
node.RNode = r;
node.PNode = p;
}
// 先序遍历
protected static void RootFirst < T > (Node < T > root)
{
if (root != null )
{
Console.Write(root.Data + " " );
RootFirst < T > (root.LNode);
RootFirst < T > (root.RNode);
}
}
// 中序遍历
protected static void RootSecond < T > (Node < T > root)
{
if (root != null )
{
RootSecond < T > (root.LNode);
Console.Write(root.Data + " " );
RootSecond < T > (root.RNode);
}
}
// 后序遍历
protected static void RootLast < T > (Node < T > root)
{
if (root != null )
{
RootLast < T > (root.LNode);
RootLast < T > (root.RNode);
Console.Write(root.Data + " " );
}
}
}
}
到此为止,代码部分己经全部写完了,运行看一下结果吧!