数据结构-二叉树

 

二叉树的遍历方式:

 

 

顺序存储的二叉树实现:

代码实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _013_二叉树
{
    //如果一个结点时空的话,那么这个结点所在的数组位置,位置为-1
    class BiTree<T>
    {
        private T[] data;
        private int count = 0;
        public BiTree(int capacity) //当前二叉树的容量
        {
            data = new T[capacity];
        }
        public bool Add(T item)
        {
            if (count >= data.Length)
                return false;

            data[count++] = item;
            return true;
        }

        public void FirstTraversal()
        {
            FirstTraversal(0);
        }
        public void MiddleTraversal()
        {
            MiddleTraversal(0);
        }
        public void LastTraversal()
        {
            LastTraversal(0);
        }

        /// <summary>
        /// 前序遍历
        /// </summary>
        private void FirstTraversal(int index)
        {
            if (index >= count)
                return;

            //得到要遍历的这个结点的编号
            int number = index + 1;
            if (data[index].Equals( -1)) return;
            //得到左结点编号
            int leftNumber = number * 2;
            int rightNumber = leftNumber + 1;

            Console.Write(data[index] + " ");
            FirstTraversal(leftNumber-1);
            FirstTraversal(rightNumber-1);
        }


        /// <summary>
        /// 中序遍历
        /// </summary>
        /// <param name="index"></param>
        private void MiddleTraversal(int index)
        {
            if (index >= count) return;

            //得到要遍历的这个结点的编号
            int number = index + 1;
            if (data[index].Equals(-1)) return;
            //得到左结点编号
            int leftNumber = number * 2;
            int rightNumber = leftNumber + 1;

            MiddleTraversal(leftNumber - 1);
            Console.Write(data[index] + " ");
            MiddleTraversal(rightNumber - 1);
        }

        /// <summary>
        /// 后序遍历
        /// </summary>
        /// <param name="index"></param>
        private void LastTraversal(int index)
        {
            if (index >= count) return;

            //得到要遍历的这个结点的编号
            int number = index + 1;
            if (data[index].Equals(-1)) return;
            //得到左结点编号
            int leftNumber = number * 2;
            int rightNumber = leftNumber + 1;

            LastTraversal(leftNumber - 1);
            LastTraversal(rightNumber - 1);
            Console.Write(data[index] + " ");
        }

        /// <summary>
        /// 层序遍历
        /// </summary>
        public void LayerTraversal()
        {
            for (int i = 0; i < count; i++)
            {
                if (data[i].Equals(-1)) continue;
                Console.Write(data[i] + " ");
            }
        }
    }
}

测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _013_二叉树
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] data = { 'A', 'B', 'C', 'D', 'E', 'F', 'G','H','I','J' };
            BiTree<char> tree = new BiTree<char>(data.Length);
            for (int i = 0; i < data.Length; i++)
            {
                tree.Add(data[i]);
            }
            tree.FirstTraversal();
            Console.WriteLine();
            tree.MiddleTraversal();
            Console.WriteLine();
            tree.LastTraversal();
            Console.WriteLine();
            tree.LayerTraversal();
        }
    }
}

顺序存储一般只用于完全二叉树,不然会造成很多空间浪费。

 

posted @ 2018-01-06 15:57  RONGWEIJUN  阅读(300)  评论(0编辑  收藏  举报