class node<T> { T data; node<T> Lnode, Rnode, Pnode; 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; } } public node(){} public node(T data) { this.data = data; } static void PreOrder<T>(node<T> rootNode) { Console.WriteLine(rootNode.data); PreOrder<T>(rootNode.LNode); PreOrder<T>(rootNode.RNode); } static node<string> BinTree() { node<string>[] binTree = new node<string>[8]; //创建结点 binTree[0] = new node<string>("A"); binTree[1] = new node<string>("B"); binTree[2] = new node<string>("C"); binTree[3] = new node<string>("D"); binTree[4] = new node<string>("E"); binTree[5] = new node<string>("F"); binTree[6] = new node<string>("G"); binTree[7] = new node<string>("H"); //使用层次遍历二叉树的思想,构造一个已知的二叉树 binTree[0].LNode = binTree[1]; binTree[0].RNode = binTree[2]; binTree[1].RNode = binTree[3]; binTree[2].LNode = binTree[4]; binTree[2].RNode = binTree[5]; binTree[3].LNode = binTree[6]; binTree[3].RNode = binTree[7]; //返回二叉树的根结点 return binTree[0]; } #region 中序遍历二叉树 static void MidOrder<T>(node<T> rootNode) { if (rootNode != null) { MidOrder<T>(rootNode.LNode); Console.WriteLine(rootNode.Data); MidOrder<T>(rootNode.RNode); } } #endregion #region 后序遍历二叉树 static void AfterOrder<T>(node<T> rootNode) { if (rootNode != null) { AfterOrder<T>(rootNode.LNode); AfterOrder<T>(rootNode.RNode); Console.WriteLine(rootNode.Data); } } #endregion #region 层次遍历二叉树 static void LayerOrder<T>(node<T> rootNode) { node<T>[] Nodes = new node<T>[20]; int front = -1; int rear = -1; if (rootNode != null) { rear++; Nodes[rear] = rootNode; } while (front != rear) { front++; rootNode = Nodes[front]; Console.WriteLine(rootNode.Data); if (rootNode.LNode != null) { rear++; Nodes[rear] = rootNode.LNode; } if (rootNode.RNode != null) { rear++; Nodes[rear] = rootNode.RNode; } } } #endregion } 见原文:http://www.cnblogs.com/xuanfeng/archive/2006/09/20/509897.html