# C# 实现二叉树各种排序

## 1. 引言

        [DebuggerDisplay("Value={Value}")]
public class Tree
{
public string Value;
public Tree Left;
public Tree Right;
}

public static Tree CreatFakeTree()
{
Tree tree = new Tree() {Value = "A"};
tree.Left = new Tree()
{
Value = "B",
Left = new Tree() {Value = "D", Left = new Tree() {Value = "G"}},
Right = new Tree() {Value = "E", Right = new Tree() {Value = "H"}}
};
tree.Right = new Tree() {Value = "C", Right = new Tree() {Value = "F"}};

return tree;
}

## 2. 先序遍历

        public static void PreOrder(Tree tree)
{
if (tree == null)
return;

System.Console.WriteLine(tree.Value);
PreOrder(tree.Left);
PreOrder(tree.Right);
}

        public static void PreOrderNoRecursion(Tree tree)
{
if(tree == null)
return;

System.Collections.Generic.Stack<Tree> stack = new System.Collections.Generic.Stack<Tree>();
Tree node = tree;

while (node != null || stack.Any())
{
if (node != null)
{
stack.Push(node);
System.Console.WriteLine(node.Value);
node = node.Left;
}
else
{
var item = stack.Pop();
node = item.Right;
}
}
}

## 3. 中序遍历

        public static void InOrder(Tree tree)
{
if(tree == null)
return;

InOrder(tree.Left);
System.Console.WriteLine(tree.Value);
InOrder(tree.Right);
}

        public static void InOrderNoRecursion(Tree tree)
{
if (tree == null)
return;

System.Collections.Generic.Stack<Tree> stack = new System.Collections.Generic.Stack<Tree>();
Tree node = tree;

while (node != null || stack.Any())
{
if (node != null)
{
stack.Push(node);
node = node.Left;
}
else
{
var item = stack.Pop();
System.Console.WriteLine(item.Value);

node = item.Right;
}
}
}

## 4. 后序遍历

        public static void PostOrder(Tree tree)
{
if (tree == null)
return;

PostOrder(tree.Left);
PostOrder(tree.Right);
System.Console.WriteLine(tree.Value);
}

        public static void PostOrderNoRecursion(Tree tree)
{
if (tree == null)
return;

System.Collections.Generic.Stack<Tree> stack = new System.Collections.Generic.Stack<Tree>();
Tree node = tree;
Tree pre = null;
stack.Push(node);

while (stack.Any())
{
node = stack.Peek();
if ((node.Left == null && node.Right == null) ||
(pre != null && (pre == node.Left || pre == node.Right)))
{
System.Console.WriteLine(node.Value);
pre = node;

stack.Pop();
}
else
{
if(node.Right != null)
stack.Push(node.Right);

if(node.Left != null)
stack.Push(node.Left);
}
}
}

public static void PostOrderNoRecursion2(Tree tree)
{
HashSet<Tree> visited = new HashSet<Tree>();
System.Collections.Generic.Stack<Tree> stack = new System.Collections.Generic.Stack<Tree>();
Tree node = tree;

while (node != null || stack.Any())
{
if (node != null)
{
stack.Push(node);
node = node.Left;
}
else
{
var item = stack.Peek();
if (item.Right != null && !visited.Contains(item.Right))
{
node = item.Right;
}
else
{
System.Console.WriteLine(item.Value);
stack.Pop();
}
}
}
}

## 5. 层序遍历

        public static void LevelOrder(Tree tree)
{
if(tree == null)
return;

Queue<Tree> queue = new Queue<Tree>();
queue.Enqueue(tree);

while (queue.Any())
{
var item = queue.Dequeue();
System.Console.Write(item.Value);

if (item.Left != null)
{
queue.Enqueue(item.Left);
}

if (item.Right != null)
{
queue.Enqueue(item.Right);
}
}
}

## 6. Z-型层序遍历

Z-层序遍历就是奇数层按照由左向右输出，偶数层按照由右向左输出，这里定义了几个辅助函数，比如计算节点所在的层次。算法思想是按照层次保存树形节点，应该是有更加优化的算法，希望大家指出。

        public static int GetDepth(Tree tree, Tree node)
{
if (tree == null)
return 0;

if (tree == node)
return 1;

if (tree.Left == node || tree.Right == node)
return 2;

int lDepth = GetDepth(tree.Left, node);
lDepth = lDepth == 0 ? 0 : lDepth + 1;

int rDepth = GetDepth(tree.Right, node);
rDepth = rDepth == 0 ? 0 : rDepth + 1;

return lDepth >= rDepth ? lDepth : rDepth;
}

public static void Z_LevelOrder(Tree tree, Dictionary<int, List<Tree>> dictionary)
{
if (tree == null)
return;

Queue<Tree> queue = new Queue<Tree>();
queue.Enqueue(tree);

while (queue.Any())
{
var item = queue.Dequeue();
var depth = GetDepth(tree, item);

List<Tree> list;
if (!dictionary.TryGetValue(depth, out list))
{
list = new List<Tree>();
}

if (item.Left != null)
{
queue.Enqueue(item.Left);
}

if (item.Right != null)
{
queue.Enqueue(item.Right);
}
}
}

public static void Z_LevelOrder(Tree tree)
{
if (tree == null)
return;

Dictionary<int, List<Tree>> dictionary = new Dictionary<int, List<Tree>>();
Z_LevelOrder(tree, dictionary);

foreach (KeyValuePair<int, List<Tree>> pair in dictionary)
{
if (pair.Key%2 == 0)
{
pair.Value.Reverse();
}

pair.Value.ForEach(t=> { System.Console.Write(t.Value); });
}
}

posted @ 2017-03-16 17:52  178mz  阅读(1338)  评论(0编辑  收藏  举报