代码改变世界

数据结构与算法回顾之二叉树

2010-12-12 19:16  yearN  阅读(1166)  评论(0编辑  收藏  举报

二叉树具有如下的性质:

  1. 在二叉树中,第i层的结点总数不超过2^(i-1);
  2. 深度为h的二叉树最多有2^(h)-1个结点(h>=1),最少有h个结点;
  3. 对于任意一棵二叉树,如果其叶结点数为N0,而度数为2的结点总数为N2, 则N0=N2+1;
  4. 具有n个结点的完全二叉树的深度为int(log2n)+1 ;
  5. 有N个结点的完全二叉树各结点如果用顺序方式存储,则结点之间有如下关系:
  • 若I为结点编号则 如果I<>1,则其父结点的编号为I/2;
  • 如果2*I<=N,则其左儿子(即左子树的根结点)的编号为2*I;若2*I>N,则无左儿子;
  • 如果2*I+1<=N,则其右儿子的结点编号为2*I+1;若2*I+1>N,则无右儿子。

简单二叉树实现代码:

namespace Alg
{
    /// <summary>
    /// 树节点类
    /// </summary>
    public class BSTNode
    {
        public IComparable El
        {
            get;
            set;
        }

        public BSTNode Left
        {
            get;
            set;
        }

        public BSTNode Right
        {
            get;
            set;
        }

        public BSTNode()
        {
            Left = Right = null;
        }

        public BSTNode(IComparable el):this(el,null,null)
        {

        }

        public BSTNode(IComparable el, BSTNode left, BSTNode right)
        {
            this.El = el;
            this.Left = left;
            this.Right = right;
        }
    }

    /// <summary>
    /// 二叉搜索树类
    /// </summary>
    public class BST
    {
        /// <summary>
        /// 根节点
        /// </summary>
        public BSTNode Root
        {
            get;
            set;
        }

        public IComparable Search(IComparable el)
        {
            return Search(Root, el);
        }

        /// <summary>
        /// 查找节点
        /// </summary>
        /// <param name="p">要查找的节点</param>
        /// <param name="el">节点位置条件</param>
        /// <returns>查找结果</returns>
        public IComparable Search(BSTNode p, IComparable el)
        {
            while (p != null)
            {
                if (el.Equals(p.El))
                {
                    return p.El;
                }
                else if (el.CompareTo(p.El) < 0)
                {
                    p = p.Left;
                }
                else
                {
                    p = p.Right;
                }
            }
            return null;
        }

        /// <summary>
        /// 插入新节点
        /// </summary>
        /// <param name="el">节点位置条件</param>
        public void Insert(IComparable el)
        {
            BSTNode p = Root;
            BSTNode prev = null;
            while (p != null)
            {
                prev = p;
                if (p.El.CompareTo(el) < 0)
                {
                    p = p.Right;
                }
                else
                {
                    p = p.Left;
                }
            }

            if (Root == null)
            {
                Root = new BSTNode(el);
            }
            else if (prev.El.CompareTo(el) < 0)
            {
                prev.Right = new BSTNode(el);
            }
            else
            {
                prev.Left = new BSTNode(el);
            }
        }

        /// <summary>
        /// 访问一个节点
        /// </summary>
        /// <param name="p">要访问的节点</param>
        public static void Visit(BSTNode p)
        {
            Console.WriteLine("el:{0},left:{1},right:{2}", p.El, p.Left, p.Right);
        }
    }
}