Esempio n. 1
0
        public void TravIn_I2(BinNode <T> x, Action <BinNode <T> > action)
        {
            Stack <BinNode <T> > s = StackVectorImpl <BinNode <T> > .StackFactory();

            while (true)
            {
                if (x != null)
                {
                    s.Push(x);
                    x = x.LChild;
                }
                else
                {
                    if (s.Size != 0)
                    {
                        x = s.Pop();
                        action(x);
                        x = x.RChild;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Esempio n. 2
0
 protected BinNode <T> Connect34(BinNode <T> a, BinNode <T> b, BinNode <T> c,
                                 BinNode <T> t0, BinNode <T> t1, BinNode <T> t2, BinNode <T> t3)
 {
     a.LChild = t0;
     if (t0 != null)
     {
         t0.Parent = a;
     }
     a.RChild = t1;
     if (t1 != null)
     {
         t1.Parent = a;
     }
     UpdateHeight(a);
     c.LChild = t2;
     if (t2 != null)
     {
         t2.Parent = c;
     }
     c.RChild = t3;
     if (t3 != null)
     {
         t3.Parent = c;
     }
     UpdateHeight(c);
     b.LChild = a;
     a.Parent = b;
     b.RChild = c;
     c.Parent = b;
     UpdateHeight(b);
     return(b);
 }
Esempio n. 3
0
        /// <summary>
        /// 交换数据,用在删除操作
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        protected void Swap(BinNode <T> a, BinNode <T> b)
        {
            T temp = a.Data;

            a.Data = b.Data;
            b.Data = temp;
        }
Esempio n. 4
0
        public void TravIn_I3(BinNode <T> x, Action <BinNode <T> > aciton)
        {
            bool backTrack = false;

            while (true)
            {
                if (!backTrack && x.HasRChild)
                {
                    x = x.LChild;
                }
                else
                {
                    if (x != null)
                    {
                        aciton(x);
                        if (x.HasRChild)
                        {
                            x = x.RChild;
                        }
                        else
                        {
                            if ((x = x.Succ()) != null)
                            {
                                break;
                            }
                            backTrack = true;
                        }
                    }
                }
            }
        }
Esempio n. 5
0
 private static void GoAlongLeftBranch(BinNode <T> x, Stack <BinNode <T> > s)
 {
     while (x != null)
     {
         s.Push(x);
         x = x.LChild;
     }
 }
Esempio n. 6
0
 /// <summary>
 /// 更新当前节点和上面给结点所有的高度
 /// </summary>
 /// <param name="x"></param>
 protected void UpdateHeightAbove(BinNode <T> x)
 {
     while (x != null)
     {
         UpdateHeight(x);
         x = x.Parent;
     }
 }
Esempio n. 7
0
        /// <summary>
        /// 作为某个结点的左孩子结点插入
        /// </summary>
        /// <param name="x">要插入的父节点</param>
        /// <param name="e">结点的值</param>
        /// <returns>插入的结点的值</returns>
        public BinNode <T> InsertAsLc(BinNode <T> x, T e)
        {
            Size++;
            var leftChild = x.InsertAsLc(e);

            UpdateHeightAbove(x);
            return(leftChild);
        }
Esempio n. 8
0
 /// <summary>
 /// 递归调用求取Size
 /// </summary>
 /// <param name="x"></param>
 /// <returns></returns>
 private static int Size(BinNode <T> x)
 {
     if (x == null)
     {
         return(0);
     }
     return(1 + Size(x.LChild) + Size(x.RChild));
 }
Esempio n. 9
0
        /// <summary>
        /// 将结点X从树中摘除
        /// </summary>
        /// <param name="x">结点X</param>
        /// <returns>摘除结点为根节点的子树的结点数目</returns>
        public int Remove(BinNode <T> x)
        {
            ClearFromParent(x);
            UpdateHeight(x.Parent);
            int n = RemoveAt(x);

            Size -= n;
            return(n);
        }
Esempio n. 10
0
 /// <summary>
 /// 查找
 /// <remarks>
 /// 更新Hot结点
 /// </remarks>
 /// </summary>
 /// <param name="v"></param>
 /// <param name="e"></param>
 /// <returns></returns>
 protected BinNode <T> SearchIn(BinNode <T> v, T e)
 {
     if (v == null || Eq(v.Data, e))
     {
         return(v);
     }
     //更新Hot结点
     Hot = v;
     return(SearchIn((Lt(e, v.Data)) ? v.LChild : v.RChild, e));
 }
Esempio n. 11
0
        /// <summary>
        /// 从结点X为子树的所有结点的数目
        /// </summary>
        /// <param name="x">作为根节点的子树</param>
        /// <returns>结点的数目</returns>
        private int RemoveAt(BinNode <T> x)
        {
            if (x == null)
            {
                return(0);
            }
            int n = 1 + RemoveAt(x.LChild) + RemoveAt(x.RChild);

            return(n);
        }
Esempio n. 12
0
 /// <summary>
 /// 先遍历所有左子树
 /// </summary>
 /// <param name="x">当前子树的根节点</param>
 /// <param name="action">操作</param>
 /// <param name="s">栈</param>
 private static void VisitAlongLeftBranch(BinNode <T> x,
                                          Action <BinNode <T> > action, Stack <BinNode <T> > s)
 {
     while (x != null)
     {
         action(x);
         s.Push(x.RChild);
         x = x.LChild;
     }
 }
Esempio n. 13
0
 /// <summary>
 /// the constructor
 /// </summary>
 /// <param name="e">data</param>
 /// <param name="parent">parent</param>
 /// <param name="lChild">left child</param>
 /// <param name="rChild">right child</param>
 /// <param name="height">height</param>
 /// <param name="npl">null path length</param>
 /// <param name="color">the color</param>
 public BinNode(T e, BinNode <T> parent = null, BinNode <T> lChild = null,
                BinNode <T> rChild      = null, int height = 0, int npl = 1, RbColor color = RbColor.RbRed)
 {
     Data   = e;
     Parent = parent;
     LChild = lChild;
     RChild = rChild;
     Height = height;
     Npl    = npl;
     Color  = color;
 }
Esempio n. 14
0
 /// <summary>
 /// 从父节点的关系链中摘除
 /// </summary>
 /// <param name="x"></param>
 private void ClearFromParent(BinNode <T> x)
 {
     if (x.IsRChild)
     {
         x.Parent.RChild = null;
     }
     else
     {
         x.Parent.LChild = null;
     }
 }
Esempio n. 15
0
 private static void TravPost(BinNode <T> x, Action <BinNode <T> > trave)
 {
     if (x.LChild != null)
     {
         TravPost(x.LChild, trave);
     }
     if (x.RChild != null)
     {
         TravPost(x.RChild, trave);
     }
     trave(x);
 }
Esempio n. 16
0
        /// <summary>
        /// 将一棵树作为结点x的Right Child插入
        /// </summary>
        /// <param name="x">要插入的结点</param>
        /// <param name="tr">要插入的树</param>
        /// <returns>返回结点</returns>
        public BinNode <T> AttachAsRc(BinNode <T> x, BinTree <T> tr)
        {
            int xSize = RemoveAt(x.RChild);

            x.RChild        = tr.Root;
            x.RChild.Parent = x;
            Size            = Size - xSize + tr.Size;
            var backup = x;

            UpdateHeightAbove(x);
            return(backup);
        }
Esempio n. 17
0
        /// <summary>
        /// 从某个结点摘除出,并且以此为根节点创建一个树
        /// </summary>
        /// <param name="x">摘除的结点</param>
        /// <returns>摘除出来的树</returns>
        public BinTree <T> Secede(BinNode <T> x)
        {
            ClearFromParent(x);
            UpdateHeightAbove(x.Parent);
            var s = new BinTree <T> {
                Root = x
            };

            x.Parent = null;
            s.Size   = x.Size();
            Size    -= s.Size;
            return(s);
        }
Esempio n. 18
0
        protected BinNode <T> RemoveAt(BinNode <T> x)
        {
            BinNode <T> w = x;
            BinNode <T> succ;

            //如果没有左孩子
            if (!x.HasLChild)
            {
                if (x.IsLChild)
                {
                    Hot.LChild = x.RChild;
                }
                else
                {
                    Hot.RChild = x.RChild;
                }
                succ = x.RChild;
            }
            else if (!x.HasRChild)
            {
                if (x.IsLChild)
                {
                    Hot.LChild = x.LChild;
                }
                else
                {
                    Hot.RChild = x.LChild;
                }
                succ = x.LChild;
            }
            else
            {
                w = w.Succ();
                Swap(w, x);
                BinNode <T> u = w.Parent;
                if (u == x)
                {
                    u.RChild = succ = w.RChild;
                }
                else
                {
                    u.LChild = succ = w.RChild;
                }
            }
            Hot = w.Parent;
            if (succ != null)
            {
                succ.Parent = Hot;
            }
            return(succ);
        }
Esempio n. 19
0
        /// <summary>
        /// 迭代先序遍历
        /// </summary>
        /// <param name="x">开始根节点</param>
        /// <param name="action">操作</param>
        public void TravPre_I2(BinNode <T> x, Action <BinNode <T> > action)
        {
            Stack <BinNode <T> > s = StackVectorImpl <BinNode <T> > .StackFactory();

            while (true)
            {
                VisitAlongLeftBranch(x, action, s);
                if (s.Size == 0)
                {
                    break;
                }
                x = s.Pop();
            }
        }
Esempio n. 20
0
        /// <summary>
        /// 中序遍历的迭代算法
        /// </summary>
        /// <param name="x"></param>
        /// <param name="action"></param>
        public void TravIn_I1(BinNode <T> x, Action <BinNode <T> > action)
        {
            var s = StackVectorImpl <BinNode <T> > .StackFactory();

            while (true)
            {
                GoAlongLeftBranch(x, s);
                if (s.Size == 0)
                {
                    break;
                }
                x = s.Pop();
                action(x);
                x = x.RChild;
            }
        }
Esempio n. 21
0
        /// <summary>
        /// 将某个密文解码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string Decode(string code)
        {
            StringBuilder      sb  = new StringBuilder();
            BinNode <HuffChar> hot = _huffmanRoot;
            bool backToRoot        = true;

            foreach (char t in code)
            {
                if (backToRoot)
                {
                    hot = _huffmanRoot;
                }
                if (hot == null)
                {
                    throw new ArgumentException("the code is not illeagl");
                }
                if (t == '0')
                {
                    hot = hot.LChild;
                    if (!hot.HasBothChild)
                    {
                        sb.Append(hot.Data.Ch);
                        backToRoot = true;
                    }
                    else
                    {
                        backToRoot = false;
                    }
                }
                else
                {
                    hot = hot.RChild;
                    if (!hot.HasBothChild)
                    {
                        sb.Append(hot.Data.Ch);
                        backToRoot = true;
                    }
                    else
                    {
                        backToRoot = false;
                    }
                }
            }
            return(sb.ToString());
        }
Esempio n. 22
0
        public void TravPost_I(BinNode <T> x, Action <BinNode <T> > action)
        {
            Stack <BinNode <T> > s = StackVectorImpl <BinNode <T> > .StackFactory();

            if (x != null)
            {
                s.Push(x);
            }
            while (s.Size != 0)
            {
                if (s.Top == x.Parent)
                {
                    GotoHlvfl(s);
                }
                x = s.Pop();
                action(x);
            }
        }
Esempio n. 23
0
        private void BuildTree()
        {
            IVector <BinNode <HuffChar> > huffchars = InitHuffChars();

            while (huffchars.Size > 1)
            {
                Tuple <Int32, Int32> twoMin = FindTwoMin(huffchars, 0, huffchars.Size);
                int first  = Math.Min(twoMin.Item1, twoMin.Item2);
                int second = Math.Max(twoMin.Item1, twoMin.Item2);
                var node1  = huffchars.Remove(second);
                var node2  = huffchars.Remove(first);

                BinNode <HuffChar> newNode = new BinNode <HuffChar>(
                    new HuffChar('^', node1.Data.Weight + node2.Data.Weight), null, node1, node2);
                node1.Parent = newNode;
                node2.Parent = newNode;
                huffchars.Insert(newNode);
            }
            _huffmanRoot = huffchars[0];
        }
Esempio n. 24
0
 /// <summary>
 /// 如果是叶节点
 /// </summary>
 /// <param name="node"></param>
 private void BuildCodeMap(BinNode <HuffChar> node)
 {
     if (!node.HasBothChild)
     {
         char   code = node.Data.Ch;
         string s    = string.Empty;
         while (node != _huffmanRoot)
         {
             if (node.IsLChild)
             {
                 s += "0";
             }
             else
             {
                 s += "1";
             }
             node = node.Parent;
         }
         _charCodeMap.Add(code, new string(s.Reverse().ToArray()));
     }
 }
Esempio n. 25
0
        /// <summary>
        /// 迭代版先序遍历
        /// </summary>
        /// <param name="x">开始节点</param>
        /// <param name="action">遍历操作,委托</param>
        public void TravePre_I1(BinNode <T> x, Action <BinNode <T> > action)
        {
            var s = StackVectorImpl <BinNode <T> > .StackFactory();

            if (x != null)
            {
                s.Push(x);
            }
            while (s.Size != 0)
            {
                x = s.Pop();
                action(x);
                if (x.HasRChild)
                {
                    s.Push(x.RChild);
                }
                if (x.HasLChild)
                {
                    s.Push(x.LChild);
                }
            }
        }
Esempio n. 26
0
        /// <summary>
        /// 获取中序遍历中的下一个节点
        /// </summary>
        /// <returns>the successor node </returns>
        public BinNode <T> Succ()
        {
            BinNode <T> s = this;

            if (HasRChild)
            {
                s = RChild;
                while (s.HasLChild)
                {
                    s = s.LChild;
                }
            }
            else
            {
                while (s.IsRChild)
                {
                    s = s.Parent;
                }
                s = s.Parent;
            }
            return(s);
        }
Esempio n. 27
0
        /// <summary>
        /// 插入结点,不考虑重复key
        /// <remarks>
        /// 使用Virtual修饰函数修饰,使之能够AVL 和Splay树等能够重载
        /// </remarks>
        /// </summary>
        /// <param name="e"></param>
        /// <returns>
        /// 返回插入的结点的位置()
        /// </returns>
        public virtual BinNode <T> Insert(T e)
        {
            var x = Search(e);

            //如果查找到某一个已经存在的
            if (x != null)
            {
                return(x);
            }
            //如果插入的是根节点
            if (Hot == null)
            {
                Root = new BinNode <T>(e);
                Size++;
                UpdateHeightAbove(Root);
                return(Root);
            }
            //如果不是跟结点
            x = Lt(e, Hot.Data) ? Hot.InsertAsLc(e) : Hot.InsertAsRc(e);
            Size++;
            UpdateHeightAbove(x);
            return(x);
        }
Esempio n. 28
0
 public int CompareTo(BinNode <T> other)
 {
     return(Data.CompareTo(other.Data));
 }
Esempio n. 29
0
 /// <summary>
 /// 插入右孩子
 /// </summary>
 /// <param name="e">节点的值</param>
 /// <returns>返回插入的节点</returns>
 public BinNode <T> InsertAsRc(T e)
 {
     return(RChild = new BinNode <T>(e, this));
 }
Esempio n. 30
0
 /// <summary>
 /// 新的结点作为根节点插入
 /// </summary>
 /// <param name="e">结点的值</param>
 /// <returns>新的根节点</returns>
 public BinNode <T> InsertAsRoot(T e)
 {
     Size = 1;
     return(Root = new BinNode <T>(e));
 }