Esempio n. 1
0
        public void Delete(int key)
        {
            //if (key == null)
            //    throw new ArgumentNullException(nameof(key));
            BinTreeNode <int> current = root;

            while (current != null)
            {
                if (key.CompareTo(current.Data) == 0)
                {
                    BinTreeNode <int> node = current;
                    if (current.Lchild != null)
                    {
                        node = GetMaxNode(current.Lchild);
                    }
                    else if (current.Rchild != null)
                    {
                        node = GetMinNode(current.Rchild);
                    }
                    current.Data = node.Data;
                    Remove(node);
                    Length--;
                }
                else if (key.CompareTo(current.Data) < 0)
                {
                    current = current.Lchild;
                }
                else
                {
                    current = current.Rchild;
                }
            }
        }
Esempio n. 2
0
        private BinTreeNode <int> BalanceLeftRotate(BinTreeNode <int> current)
        {
            BinTreeNode <int> child = current.Rchild;

            if (child.Bf > 0)
            {
                BinTreeNode <int> grand = child.Lchild;
                RightRotate(grand, child);
                if (grand.Bf < 0)
                {
                    grand.Bf = -2;
                    child.Bf = 0;
                }
                else
                {
                    int temp = grand.Bf;
                    grand.Bf = -1 * child.Bf;
                    child.Bf = -1 * temp;
                }
                child = grand;
            }
            LeftRotate(child, current);
            current.Bf = -1 * child.Bf - 1;
            child.Bf   = child.Bf < -1 ? 0 : child.Bf + 1;
            return(child);
        }
Esempio n. 3
0
        private void AdjustRemove(BinTreeNode <int> node)
        {
            if (node.Bf > 1)
            {
                node = BalanceRightRotate(node);
            }
            else if (node.Bf < -1)
            {
                node = BalanceLeftRotate(node);
            }
            BinTreeNode <int> parent = node.Parent;

            if (node.Bf == 0 && parent != null)
            {
                if (node == parent.Lchild)
                {
                    parent.Bf--;
                }
                else
                {
                    parent.Bf++;
                }
                if (parent.Bf != 1 && parent.Bf != -1)
                {
                    AdjustRemove(parent);
                }
            }
        }
Esempio n. 4
0
        private SolidBrush drawBrush = new SolidBrush(Color.Black); //写字符串用的刷子
        public void Insert(int key)
        {
            //if (key == null) throw new ArgumentException(nameof(key));
            if (root == null)
            {
                root = new BinTreeNode <int>(key);
                return;
            }
            BinTreeNode <int> current = new BinTreeNode <int>(key);

            FindPosition(root, current);
            BinTreeNode <int> parent = current.Parent;

            if (parent != null)
            {
                Length++;
                //如果新插入的节点是做孩子 父亲节点平衡因子+1
                //如果新插入的节点是右孩子 父亲节点平衡因子-1
                if (current == parent.Lchild)
                {
                    parent.Bf++;
                }
                else
                {
                    parent.Bf--;
                }
                if (parent.Bf != 0)
                {
                    AdjustInsert(parent);
                }
            }
        }
Esempio n. 5
0
        private BinTreeNode <T> FindParent(BinTreeNode <T> current, BinTreeNode <T> find)
        {
            if (find == null)
            {
                throw new Exception("传入参数find为空");
            }
            if (current == null)
            {
                return(null);
            }
            if (current.Lchild != null && current.Lchild.Equals(find) == true)
            {
                return(current);
            }
            if (current.Rchild != null && current.Rchild.Equals(find) == true)
            {
                return(current);
            }
            BinTreeNode <T> temp = FindParent(current.Lchild, find);

            if (temp != null)
            {
                return(temp);
            }
            else
            {
                return(FindParent(current.Rchild, find));
            }
        }
Esempio n. 6
0
 private void AdjustInsert(BinTreeNode <int> node)
 {
     if (node.Bf > 1)
     {
         BalanceRightRotate(node);
     }
     else if (node.Bf < -1)
     {
         BalanceLeftRotate(node);
     }
     else
     {
         BinTreeNode <int> root = node.Parent;
         if (root != null)
         {
             if (node == root.Lchild)
             {
                 root.Bf++;
             }
             else
             {
                 root.Bf--;
             }
             if (root.Bf != 0)
             {
                 AdjustInsert(root);
             }
         }
     }
 }
Esempio n. 7
0
        //private void draw(PaintEventArgs e,Graphics dc,BinTreeNode<int> node,int x,int y)
        //{
        //    if (node == null)
        //    {
        //        throw new Exception("空字符串");
        //    }
        //    dc.DrawEllipse(blackPen, x, y, 25, 20);
        //    String drawString = node.Data.ToString();//要显示的字符串
        //    PointF drawPoint = new PointF(x+1, y+3);//显示的字符串左上角的坐标
        //    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
        //    delt /= 2;
        //    if (node.Lchild != null)
        //        draw(e, dc, node.Lchild,  delt, y + 40);
        //    if (node.Rchild != null)
        //        draw(e, dc, node.Rchild, x + delt, y + 40);


        //}
        private void draw(Graphics dc, BinTreeNode <int> node, int x, int y, int delt_f)
        {
            if (node == null)
            {
                return;
            }
            dc.DrawEllipse(blackPen, x, y, 25, 20);
            String drawString = node.Data.ToString();     //要显示的字符串
            PointF drawPoint  = new PointF(x + 1, y + 3); //显示的字符串左上角的坐标

            dc.DrawString(drawString, drawFont, drawBrush, drawPoint);
            // delt /= 2;
            if (node.Parent != null)
            {
                dc.DrawLine(blackPen, x + 10, y, delt_f + 10, y - 20);
            }
            if (node.Lchild != null)
            {
                draw(dc, node.Lchild, x - System.Math.Abs(x - delt_f) / 2, y + 40, x);
            }

            if (node.Rchild != null)
            {
                draw(dc, node.Rchild, x + System.Math.Abs(x - delt_f) / 2, y + 40, x);
            }
        }
Esempio n. 8
0
        private void Remove(BinTreeNode <int> node)
        {
            if (node == root)
            {
                root = null;
                return;
            }
            BinTreeNode <int> parent = node.Parent;

            if (node == parent.Lchild)
            {
                parent.Lchild = node.Lchild;
                if (node.Lchild != null)
                {
                    node.Lchild.Parent = parent;
                }
                parent.Bf--;
            }
            else
            {
                //parent.Rchild = null;
                parent.Rchild = node.Lchild;
                if (node.Lchild != null)
                {
                    node.Lchild.Parent = parent;
                }
                parent.Bf++;
            }
            if (parent.Bf != 1 && parent.Bf != -1)
            {
                AdjustRemove(parent);
            }
        }
Esempio n. 9
0
 //寻找node节点的父亲节点
 public void FindPosition(BinTreeNode <int> current, BinTreeNode <int> node)
 {
     if (node.Data < current.Data)
     {
         if (current.Lchild == null)
         {
             current.Lchild = node;
             node.Parent    = current;
         }
         else
         {
             FindPosition(current.Lchild, node);
         }
     }
     else if (node.Data > current.Data)
     {
         if (current.Rchild == null)
         {
             current.Rchild = node;
             node.Parent    = current;
         }
         else
         {
             FindPosition(current.Rchild, node);
         }
     }
     else
     {
         current.Data = node.Data;
     }
 }
Esempio n. 10
0
 private BinTreeNode <int> GetMinNode(BinTreeNode <int> current)
 {
     if (current.Lchild == null)
     {
         return(current);
     }
     return(GetMinNode(current.Lchild));
 }
Esempio n. 11
0
 public BinTreeNode <T> GetParent(BinTreeNode <T> find)
 {
     if (find == null)
     {
         throw new Exception("传入参数find为null");
     }
     return(FindParent(root, find));
 }
Esempio n. 12
0
 private void PostOrder(BinTreeNode <T> current)
 {
     if (current == null)
     {
         return;
     }
     PostOrder(current.Lchild);
     orderString += current.Data.ToString() + " ";
     PostOrder(current.Rchild);
 }
Esempio n. 13
0
 private void LeftRotate(BinTreeNode <int> node, BinTreeNode <int> parent)
 {
     OperateForBothRotate(node, parent);
     parent.Rchild = node.Lchild;
     if (node.Lchild != null)
     {
         node.Lchild.Parent = parent;
     }
     node.Lchild = parent;
 }
Esempio n. 14
0
        //private BinTree<int> binTree;
        //public AVLTree(int data)
        //{
        //    BinTreeNode<int> root = new BinTreeNode<int>();
        //    root.Data = data;
        //    binTree = new BinTree<int>(root);
        //}

        ////右旋处理
        //public void RRotate(BinTreeNode<int> P)
        //{
        //    BinTreeNode<int> L;
        //    L = P.Lchild;
        //    P.Lchild = L.Rchild;
        //    L.Rchild = P;
        //    P = L;
        //}
        ////左旋处理
        //public void LRotate(BinTreeNode<int> P)
        //{
        //    BinTreeNode<int> L;
        //    L = P.Rchild;
        //    P.Rchild = L.Lchild;
        //    L.Lchild = P;
        //    P = L;
        //}
        //public void LeftBalance(BinTreeNode<int> T)
        //{
        //    BinTreeNode<int> L, Lr;
        //    L = T.Lchild;            //L指向T左子树根节点
        //    switch (L.Bf)
        //    {
        //        case 1:
        //            T.Bf = L.Bf = 0;
        //            RRotate(T);
        //            break;
        //        case -1:
        //            Lr = L.Rchild;
        //            switch (Lr.Bf)
        //            {
        //                case 1:
        //                    T.Bf = -1;
        //                    L.Bf = 0;
        //                    break;
        //                case 0:
        //                    T.Bf = L.Bf = 0;
        //                    break;
        //                case -1:
        //                    T.Bf = 0;
        //                    L.Bf = 1;
        //                    break;
        //            }
        //            Lr.Bf = 0;
        //            LRotate(T.Lchild);
        //            RRotate(T);
        //            break;
        //    }
        //}
        private void MidOrder(BinTreeNode <int> current)
        {
            if (current == null)
            {
                return;
            }
            MidOrder(current.Lchild);
            orderString += current.Data.ToString() + " ";
            MidOrder(current.Rchild);
        }
Esempio n. 15
0
 private int GetValue(BinTreeNode <int> current, int key)
 {
     if (current == null)
     {
         throw new Exception("节点为空");
     }
     if (current.Data > key)
     {
         return(GetValue(current.Lchild, key));
     }
     if (current.Data < key)
     {
         return(GetValue(current.Rchild, key));
     }
     return(current.Data);
 }
Esempio n. 16
0
        private void OperateForBothRotate(BinTreeNode <int> node, BinTreeNode <int> parent)
        {
            BinTreeNode <int> grand = parent.Parent;

            node.Parent   = grand;
            parent.Parent = node;
            if (grand == null)
            {
                root = node;
            }
            else if (parent == grand.Rchild)
            {
                grand.Rchild = node;
            }
            else
            {
                grand.Lchild = node;
            }
        }
Esempio n. 17
0
 public string LevelTraversal()
 {
     orderString = string.Empty;
     if (root != null)
     {
         Queue <BinTreeNode <T> > LQ = new Queue <BinTreeNode <T> >();
         LQ.Enqueue(root);
         while (LQ.Count == 0)
         {
             BinTreeNode <T> temp = LQ.Dequeue();
             orderString += temp.Data.ToString() + " ";
             if (temp.Lchild != null)
             {
                 LQ.Enqueue(temp.Lchild);
             }
             if (temp.Rchild != null)
             {
                 LQ.Enqueue(temp.Rchild);
             }
         }
     }
     return(orderString.Trim());
 }
Esempio n. 18
0
 public BinTreeNode(T key)
 {
     this.data   = key;
     this.rchild = this.lchild = null;
     this.bf     = 0;
 }
Esempio n. 19
0
 public BinTree(BinTreeNode <T> root)
 {
     this.root = root;
 }