Пример #1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 public BsTree()
 {
     _mRoot = null;
     _carUpdate = new Dictionary<int, int>();
     _carIt = new Dictionary<int, int>();
     _roadUpdate = new Dictionary<string, int>();
     _roadIt = new Dictionary<string, int>();
     carSpeed = new Dictionary<int, double>();
 }
Пример #2
0
        public BstNode Right; // 右孩子

        #endregion Fields

        #region Constructors

        public BstNode(int value, int c, long la, int max, int blockcarnum, double blockspeed, int isblocked, BstNode p,
            BstNode l, BstNode r,bool isfull)
        {
            Key = value;
            CarNumbers = c;
            LatestTime = la;
            MaxCarNumbers = max;
            blockCarNumbers = blockcarnum;
            blockSpeed = blockspeed;
            isBlocked = isblocked;
            Parent = p;
            Left = l;
            Right = r;
            isFull = isfull;
        }
Пример #3
0
        private BstNode Successor(BstNode x)
        {
            if (x.Right != null)
                return Minimum(x.Right);
            BstNode y = x.Parent;
            while ((y != null) && (x == y.Right))
            {
                x = y;
                y = y.Parent;
            }

            return y;
        }
Пример #4
0
        /// <summary>
        /// (递归实现)查找"二叉树x"中latestTime为key的节点
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private BstNode SearchTime(BstNode x, long time)
        {
            if (x == null || x.LatestTime == time)
                return SearchTime(x.Left, time);

            if (x.LatestTime == time)
            {
                return x;
            }

            if (!(x == null || x.LatestTime == time))
            {
                return SearchTime(x.Right, time);
            }

            return x;
        }
Пример #5
0
 /// <summary>
 /// (递归实现)查找"二叉树x"中键值为key的节点
 /// </summary>
 /// <param name="x"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 private BstNode Search(ref BstNode x, long key)
 {
     if (x == null || x.Key == key)
         return x;
     if (key < x.Key)
         return Search(ref x.Left, key);
     return Search(ref x.Right, key);
 }
Пример #6
0
        /// <summary>
        /// 删除结点(z),并返回被删除的结点
        /// </summary>
        /// <param name="tree"></param>
        /// <param name="z"></param>
        /// <returns></returns>
        private void Remove(ref BstNode tree, BstNode z)
        {
            BstNode x;
            BstNode y;

            if ((z.Left == null) || (z.Right == null))
                y = z;
            else
                y = Successor(z);

            if (y.Left != null)
                x = y.Left;
            else
                x = y.Right;

            if (x != null)
                x.Parent = y.Parent;

            if (y.Parent == null)
                tree = x;
            else if (y == y.Parent.Left)
                y.Parent.Left = x;
            else
                y.Parent.Right = x;

            if (y != z)
                z.Key = y.Key;
        }
Пример #7
0
 /// <summary>
 /// 查找最小结点:返回tree为根结点的二叉树的最小结点
 /// </summary>
 /// <param name="tree"></param>
 /// <returns></returns>
 private BstNode Minimum(BstNode tree)
 {
     if (tree == null)
         return null;
     while (tree.Left != null)
     {
         tree = tree.Left;
     }
     return tree;
 }
Пример #8
0
 private long Maximum(BstNode tree)
 {
     if (tree == null)
         return 0;
     long oldTime = Math.Min(tree.LatestTime, Math.Max(Maximum(tree.Left), Maximum(tree.Right)));
     return oldTime;
 }
Пример #9
0
 /// <summary>
 /// 将结点(key为节点键值)插入到二叉树中
 /// </summary>
 /// <param name="key"></param>
 private void Insert(int key,double speed)
 {
     long timeNow = ConvertDateTimeInt(DateTime.Now);
     BstNode z = new BstNode(key, 1, timeNow, 0, 0, speed, 0, null, null, null,true);
     Insert(ref _mRoot, z);
     if (!_isFull)
     {
         if (AllLevel++ > MaxRoadNum)
         {
             _isFull = true;
         }
     }
 }
Пример #10
0
        /// <summary>
        /// 将结点插入到二叉树中
        /// </summary>
        /// <param name="tree"></param>
        /// <param name="z"></param>
        private void Insert(ref BstNode tree, BstNode z)
        {
            BstNode x = tree;
            BstNode y = null;

            while (x != null)
            {
                y = x;
                if (z.Key < x.Key)
                    x = x.Left;
                else
                    x = x.Right;

            }
            z.Parent = y;
            if (y == null)
                tree = z;
            else if (z.Key < y.Key)
                y.Left = z;
            else
                y.Right = z;
        }
Пример #11
0
        /// <summary>
        /// 销毁二叉树
        /// </summary>
        /// <param name="tree"></param>
        private void Destroy(BstNode tree)
        {
            if (tree == null)
                return;

            if (tree.Left != null)
                Destroy(tree.Left);
            if (tree.Right != null)
                Destroy(tree.Right);
        }