using System;
using System.Collections.Generic;
//System.Collections.Generic 命名空间包含定义泛型集合的接口和类,
//用户可以使用泛型集合来创建强类型集合,
//这种集合能提供比非泛型强类型集合更好的类型安全性和性能。
namespace 所以
{
class Tree
{
public class TreeNode
{
public int Data;//用来决定树的形状
public int number;
public TreeNode Left;//字段“成员变量”,一般在类的内部做数据交互使用。
public TreeNode Right;//就是TreeNode.Right的意思
//data left right都是treenode中的私有元素
//下面的东西是一个元素
public TreeNode(int Dta, int numb)//TreeNode.TreeNode(int Dta)是一个TreeNode里的函数
{
this.Data = Dta;//this可用于限定类似名称隐藏的成员
//eg this.name = name;
//将对象作为参数传递给方法,例如:
//CalcTax(this);
this.number = numb;
this.Left = null;//this代表treenode
this.Right = null;
}
public TreeNode AddNode(TreeNode Root, TreeNode NewNode)//引入两个treenode格式变量
//表示TreeNode.AddNode函数
{
if (Root == null)
{
Root = NewNode;
return Root;
}
else if (Root.Data > NewNode.Data)
//上面的用法!引用root这个treenode类中的public int data 元素
{
Root.Left = AddNode(Root.Left, NewNode);//给左节点赋值
}
else
{
Root.Right = AddNode(Root.Right, NewNode);
}
return Root;
}
}
private TreeNode Root = null;
//public npreorder(TreeNode root)
//{
// Stack<TreeNode> bb = new Stack<TreeNode>();
//}
public void Add(int Data, int number)
{
TreeNode newNodeObj = new TreeNode(Data, number);
Root = newNodeObj.AddNode(Root, newNodeObj);
}
//先序列遍
private void Print(TreeNode Root)
{
if (Root == null)
{
return;
}
else
{
// Console.WriteLine(Root.Data);//输出的是字符串,所以用它也可以
Console.WriteLine(Root.number);
Print(Root.Left);//自己写的函数,遍历左子树
Print(Root.Right);
}
}
//中序遍历
public void Print1()
{
InOrder(Root);//在这个print中运行的是带参数的print,细心!!!
}
public void InOrder(TreeNode Root)
{
if (Root == null)
{
return;
;
}
else
{
{
InOrder(Root.Left);
// Console.WriteLine(Root.Data);
Console.WriteLine(Root.number);
InOrder(Root.Right);
}
}
}
public void Print2()
{
PostOrder(Root);//在这个print中运行的是带参数的print,细心!!!
}
//后序遍历
public void PostOrder(TreeNode Root)
{
if (Root == null)
{
return;
}
else
{
PostOrder(Root.Left);
PostOrder(Root.Right);
// Console.WriteLine(Root.Data);
Console.WriteLine(Root.number);
}
}
public void Print()
{
Print(Root);//在这个print中运行的是带参数的print,细心!!!
}
public void PrintLevelOrder()//层序遍历
{
int i;
int j;
int flag;
flag = 0;
i = 0;
j = 0;
List<TreeNode> Data = new List<TreeNode>();//列表形元素
Data.Add(Root);//在其中新增元素
while (Data.Count != 0)
{
TreeNode temp = Data[0];//tmp取代了data[0]
Data.RemoveAt(0);//删除data[0]的意思
Console.WriteLine(temp.number);
if (temp.Left != null)
{
Data.Add(temp.Left);
i = i + 1;
}
j = j + 1;
if (temp.Right != null)
{
Data.Add(temp.Right);
i = i + 1;
}
j = j + 1;
if ((i != j) && ((i == 6)))//上面三层都是满的,在最下层判断
{
flag = 1;
}
}
if (flag == 1)
{
Console.WriteLine("是完全二叉树");
}
else
{
Console.WriteLine("不是完全二叉树");
}
}
public void Search(int FindIt)
{
SearchIt(Root, FindIt);
}
private void SearchIt(TreeNode Root, int FindIt)
{
if (Root == null)
{
Console.WriteLine("Not Found!!");
return;
}
else if (FindIt == Root.Data)
{
Console.WriteLine("Found!!");
return;
}
else if (FindIt > Root.Data)
{
SearchIt(Root.Right, FindIt);
}
else if (FindIt < Root.Data)
{
SearchIt(Root.Left, FindIt);
}
return;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 所以;
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 };
MyArray[] a = CreateTree(a1);
Console.WriteLine("前序递归遍历");
PreOrderTraversal(a);
Console.WriteLine();
Console.WriteLine("前序非递归遍历");
PreOrderNotRecursion(a);
Console.WriteLine();
Console.WriteLine("中序递归遍历");
InOrderTraversal(a);
Console.WriteLine();
Console.WriteLine("中序非递归遍历");
InOrderNotRecursion(a);
Console.WriteLine();
Console.WriteLine("后序递归遍历");
PostOrderTraversal(a);
Console.WriteLine();
Console.WriteLine("后序非递归遍历");
PostOrderNotRecursion(a);
Console.WriteLine(" ");
Tree Data = new Tree();
//添加新元素
Data.Add(50, 1);
Data.Add(35, 2);
Data.Add(58, 3);
Data.Add(30, 4);
// Data.Add(40, 5);
Data.Add(57, 6);
Data.Add(60, 7);
// Data.Add(2);
// Data.Add(1);
Console.WriteLine("层序输出");
Data.PrintLevelOrder();
Console.WriteLine(" ");
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]);
}
}
}
}
}