复习了下遍历二叉树,以前一直想写下递归非递归方式,老是给忘了,现在写了,以后就不用写了,忘了还可以看看。。。
下面代码是用数组模拟的一棵树做测试用例,测试还不是很全面。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sort
{
class Program
{
public struct MyArray
{
public int value;
public int Lchild;
public int Rchild;
}
static void Main(string[] args)
{
int[] a1 = { 1,2,3,4,5,6,7,8,9};
MyArray [] a=CreateTree(a1);
PreOrderTraversal(a);
Console.WriteLine();
PreOrderNotRecursion(a);
Console.WriteLine();
InOrderTraversal(a);
Console.WriteLine();
InOrderNotRecursion(a);
Console.WriteLine();
PostOrderTraversal(a);
Console.WriteLine();
PostOrderNotRecursion(a);
Console.WriteLine();
PostOrderNotRecursion(a);
Console.Read();
}
public static MyArray[] CreateTree(int[] a1)
{
MyArray[] a = new MyArray[9];
for (int i = 0; i < a1.Length; i++)
{
a[i].value = a1[i];
if (2 * i + 1 <= a1.Length - 1)
a[i].Lchild = 2 * i + 1;
if (2 * i + 2 <= a1.Length - 1)
a[i].Rchild = 2 * i + 2;
}
return a;
}
//先序递归遍历
public static void PreOrderTraversal(MyArray [] a,int i=0)
{
Console.Write(a[i].value);
if (a[i].Lchild != 0)
{
PreOrderTraversal(a, a[i].Lchild);
}
if (a[i].Rchild != 0)
{
PreOrderTraversal(a, a[i].Rchild);
}
}
//先序非递归遍历
public static void PreOrderNotRecursion(MyArray[] a)
{
if (a == null || a.Length == 0)
return;
Stack<MyArray> S = new Stack<MyArray>();
S.Push(a[0]);
MyArray temp;
while (S.Count != 0)
{
temp = S.First();
Console.Write(temp.value);
while (temp.Lchild != 0)
{
temp = a[temp.Lchild];
Console.Write(temp.value);
S.Push(temp);
}
while (S.Count != 0 && temp.Rchild == 0)
{
temp = S.Pop();
}
if (S.Count == 0&&temp.Rchild == 0)
return;
temp = a[temp.Rchild];
S.Push(temp);
}
}
//中序递归遍历
public static void InOrderTraversal(MyArray[] a, int i = 0)
{
if (a[i].Lchild != 0)
{
InOrderTraversal(a, a[i].Lchild);
}
Console.Write(a[i].value);
if (a[i].Rchild != 0)
{
InOrderTraversal(a, a[i].Rchild);
}
}
//中序非递归遍历
public static void InOrderNotRecursion(MyArray[] a)
{
if (a == null || a.Length == 0)
{
return;
}
Stack<MyArray> S = new Stack<MyArray>();
S.Push(a[0]);
MyArray temp;
while (S.Count != 0)
{
temp = S.First();
while (temp.Lchild != 0)
{
temp = a[temp.Lchild];
S.Push(temp);
}
Console.Write(temp.value );
S.Pop();
while (temp.Rchild == 0&&S.Count !=0)
{
temp = S.Pop();
Console.Write(temp.value);
}
if (S.Count == 0 && temp.Rchild == 0)
return;
temp = a[temp.Rchild];
S.Push(temp);
}
}
//后序递归遍历
public static void PostOrderTraversal(MyArray[] a, int i = 0)
{
if (a[i].Lchild != 0)
{
PostOrderTraversal(a, a[i].Lchild);
}
if (a[i].Rchild != 0)
{
PostOrderTraversal(a, a[i].Rchild);
}
Console.Write(a[i].value);
}
//后序非递归遍历
public static void PostOrderNotRecursion(MyArray[] a)
{
if (a == null || a.Length == 0)
{
return;
}
Stack<MyArray> S = new Stack<MyArray>();
S.Push(a[0]);
MyArray temp;
while (S.Count != 0)
{
temp = S.First();
while (temp.Lchild != 0)
{
temp=a[temp.Lchild ];
S.Push(temp);
}
while (S.Count !=0)
{
temp = S.First();
if (temp.Rchild == 0)
{
S.Pop();
Console.Write(temp.value);
}
else
{
break;
}
}
if (S.Count != 0)
{
int rchild = temp.Rchild;
temp.Lchild = temp.Rchild = 0;
S.Pop();
S.Push(temp);
S.Push(a[rchild ]);
}
}
}
}
}