using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace imikity4_二叉树_
{
//二叉树的结构
public class Tree
{
public string value;
public Tree Left;
public Tree Right;
}
class Program
{
//二叉树的建树过程 :
// A
// / \
// B C
// / \ /
// D E F
// / /
// G H
public static Tree CreateTree()
{
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", Left = new Tree() { value = "H" } },
};
tree.Right = new Tree() { value = "C", Left = new Tree() { value = "F" } };
return tree;
}
//先序遍历的递归实现
//ABDGEHCF
public static void PreOrder(Tree tree)
{
if (tree ==null)
{
return;
}
System.Console.Write(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>();
//记录当前节点
while (tree!=null || stack.Count !=0)
{
while (tree!= null)
{
Console.Write(tree.value);
stack.Push(tree);
tree = tree.Left;
}
if (stack.Count!=0)
{
tree = stack.Pop();
tree = tree.Right;
}
}
}
//中序遍历的递归实现
//GDBHEAFC
public static void InOrder(Tree tree)
{
if (tree ==null)
{
return;
}
InOrder(tree.Left);
Console.Write(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>();
while (tree!=null || stack.Count!=0)
{
while (tree!=null)
{
stack.Push(tree);
tree = tree.Left;
}
if (stack.Count != 0)
{
tree = stack.Pop();
Console.Write(tree.value);
tree = tree.Right;
}
}
}
//后序遍历的递归
//GDHEBFCA
public static void lastOrder(Tree tree)
{
if (tree ==null)
{
return;
}
lastOrder(tree.Left);
lastOrder(tree.Right);
Console.Write(tree.value);
}
//此处为比较复杂的非递归后序遍历
public static void lastOrderNoRecursion(Tree tree)
{
if (tree==null)
{
return;
}
System.Collections.Generic.Stack<Tree> s1 = new System.Collections.Generic.Stack<Tree>();
//声明一个栈用来标记该节点是否被访问过(0:表示未被访问 1:表示已被访问)
System.Collections.Generic.Stack<int> s2 = new System.Collections.Generic.Stack<int>();
int index = 1;
while (tree!=null || s1.Count!=0)
{
while (tree!=null)
{
s1.Push(tree);
s2.Push(0);
tree = tree.Left;
}
while (s1.Count!=0 && s2.Peek()==index)
{
s2.Pop();
Console.Write(s1.Pop().value);
}
if (s1.Count!=0)
{
s2.Pop();
//已经被访问过
s2.Push(1);
tree = s1.Peek();
tree = tree.Right;
}
}
}
//函数的入口
static void Main(string[] args)
{
Tree tree = CreateTree();
PreOrder(tree);
Console.WriteLine("\n");
PreOrderNoRecursion(tree);
Console.WriteLine("\n");
InOrder(tree);
Console.WriteLine("\n");
InOrderNoRecursion(tree);
Console.WriteLine("\n");
lastOrder(tree);
Console.WriteLine("\n");
lastOrderNoRecursion(tree);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace imikity4_二叉树_
{
//二叉树的结构
public class Tree
{
public string value;
public Tree Left;
public Tree Right;
}
class Program
{
//二叉树的建树过程 :
// A
// / \
// B C
// / \ /
// D E F
// / /
// G H
public static Tree CreateTree()
{
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", Left = new Tree() { value = "H" } },
};
tree.Right = new Tree() { value = "C", Left = new Tree() { value = "F" } };
return tree;
}
//先序遍历的递归实现
//ABDGEHCF
public static void PreOrder(Tree tree)
{
if (tree ==null)
{
return;
}
System.Console.Write(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>();
//记录当前节点
while (tree!=null || stack.Count !=0)
{
while (tree!= null)
{
Console.Write(tree.value);
stack.Push(tree);
tree = tree.Left;
}
if (stack.Count!=0)
{
tree = stack.Pop();
tree = tree.Right;
}
}
}
//中序遍历的递归实现
//GDBHEAFC
public static void InOrder(Tree tree)
{
if (tree ==null)
{
return;
}
InOrder(tree.Left);
Console.Write(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>();
while (tree!=null || stack.Count!=0)
{
while (tree!=null)
{
stack.Push(tree);
tree = tree.Left;
}
if (stack.Count != 0)
{
tree = stack.Pop();
Console.Write(tree.value);
tree = tree.Right;
}
}
}
//后序遍历的递归
//GDHEBFCA
public static void lastOrder(Tree tree)
{
if (tree ==null)
{
return;
}
lastOrder(tree.Left);
lastOrder(tree.Right);
Console.Write(tree.value);
}
//此处为比较复杂的非递归后序遍历
public static void lastOrderNoRecursion(Tree tree)
{
if (tree==null)
{
return;
}
System.Collections.Generic.Stack<Tree> s1 = new System.Collections.Generic.Stack<Tree>();
//声明一个栈用来标记该节点是否被访问过(0:表示未被访问 1:表示已被访问)
System.Collections.Generic.Stack<int> s2 = new System.Collections.Generic.Stack<int>();
int index = 1;
while (tree!=null || s1.Count!=0)
{
while (tree!=null)
{
s1.Push(tree);
s2.Push(0);
tree = tree.Left;
}
while (s1.Count!=0 && s2.Peek()==index)
{
s2.Pop();
Console.Write(s1.Pop().value);
}
if (s1.Count!=0)
{
s2.Pop();
//已经被访问过
s2.Push(1);
tree = s1.Peek();
tree = tree.Right;
}
}
}
//函数的入口
static void Main(string[] args)
{
Tree tree = CreateTree();
PreOrder(tree);
Console.WriteLine("\n");
PreOrderNoRecursion(tree);
Console.WriteLine("\n");
InOrder(tree);
Console.WriteLine("\n");
InOrderNoRecursion(tree);
Console.WriteLine("\n");
lastOrder(tree);
Console.WriteLine("\n");
lastOrderNoRecursion(tree);
Console.ReadLine();
}
}
}
本文详细介绍了二叉树的基本概念及其先序、中序、后序遍历的方法,包括递归与非递归实现方式,并提供了一个具体的二叉树构建实例。
3万+

被折叠的 条评论
为什么被折叠?



