/****************************************************文件:BiTree.cs作者:黄山学院--Li Qingshan邮箱: 2781838262@qq.com日期:2020/05/19 12:24 功能:
*****************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 二叉树_顺序存储
{public class BiTree{public T[] data;private int nextIndex = 0;public BiTree(int length){data = new T[length];}public bool Add(T item){if (nextIndex >= data.Length){return false;}else{data[nextIndex] = item;nextIndex++;return true;}}//前序遍历public void PreorderTraversal(int number){//当下标越界或者当前元素为'^'时此次递归分支终止if (number > data.Length || data[number - 1].Equals('^')){return;}Console.Write(data[number - 1] + " ");int leftChildNum = number * 2;PreorderTraversal(leftChildNum);int rightChildNum = number * 2 + 1;PreorderTraversal(rightChildNum);}//中序遍历public void MiddleTraversal(int number){//当下标越界或者当前元素为'^'时此次递归分支终止if (number > data.Length || data[number - 1].Equals('^')){return;}int leftChildNum = number * 2;MiddleTraversal(leftChildNum);Console.Write(data[number - 1] + " ");int rightChildNum = number * 2 + 1;MiddleTraversal(rightChildNum);}//后序遍历public void PostorderTraversal(int number){//当下标越界或者当前元素为'^'时此次递归分支终止if (number > data.Length || data[number - 1].Equals('^')){return;}int leftChildNum = number * 2;PostorderTraversal(leftChildNum);int rightChildNum = number * 2 + 1;PostorderTraversal(rightChildNum);Console.Write(data[number - 1] + " ");}//层序遍历public void LayerTraversal(){for (int i = 0; i }
/****************************************************文件:Program.cs作者:黄山学院--Li Qingshan邮箱: 2781838262@qq.com日期:2020/05/19 15:49 功能:
*****************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 二叉树_顺序存储
{class Program{static void Main(string[] args){//完全二叉树//char[] CompleteBinaryTree = new[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };//满二叉树//char[] FullBinaryTree = new[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G'};//普通二叉树char[] NormalBinaryTree = new[] { 'A', 'B', 'C', 'D', '^', 'F', 'G', '^', 'H', '^', '^', 'I' };BiTree biTree = new BiTree(NormalBinaryTree.Length);for (int i = 0; i }