示例#1
0
 /// <summary>
 /// Рисует узел дерева (<see cref = "BinarySearchTree"/>)
 /// </summary>
 /// <param name="g"></param>
 /// <param name="node"></param>
 /// <param name="S"></param>
 private void DrawBST_Node(Graphics g, BST_Node node, Point S, int Dist)
 {
     if (node != null)
     {
         //int D = (GetParent(node) == null ? 100 : 50);
         Pen P  = new Pen(Color.Brown, 5);
         Pen P2 = new Pen(Color.Brown, 3);
         g.DrawEllipse(P, S.X, S.Y, 30, 30);
         P.Color = Color.Brown;
         if (node.Left != null)
         {
             Point S2 = new Point(S.X - (Dist -= 45), S.Y + 90);
             g.DrawLine(P2, S.X + 15, S.Y + 15, S2.X + 15, S2.Y + 15);
             DrawBST_Node(g, node.Left, S2, Dist);
         }
         if (node.Right != null)
         {
             Point S2 = new Point(S.X + (node.Left == null ? (Dist -= 45) : Dist), S.Y + 90);
             g.DrawLine(P2, S.X + 15, S.Y + 15, S2.X + 15, S2.Y + 15);
             DrawBST_Node(g, node.Right, S2, Dist);
         }
         SolidBrush B = new SolidBrush(node.Chosen ? Color.Yellow : Color.White);
         g.FillEllipse(B, S.X, S.Y, 30, 30);
         int d = 10 - (node.Value / 10 == 1 ? 3 : 0) - (node.Value < 0 ? 3 : 0);
         g.DrawString(node.Value.ToString(), Form1.DefaultFont,
                      new SolidBrush(Color.Black), S.X + d, S.Y + 10);
     }
 }
示例#2
0
 private BST_Node(double weight, double value)
 {
     _weight = weight;
     _value  = value;
     _height = 0;
     _left   = null;
     _right  = null;
 }
示例#3
0
            private static BST_Node RotateRight(BST_Node node)
            {
                BST_Node node0 = node._left;

                node._left   = node0._right;
                node0._right = node;
                ResetHeight(node);
                ResetHeight(node0);
                return(node0);
            }
示例#4
0
        private BST_Node GetParent(BST_Node N)
        {
            List <BST_Node> listOfNodes = new List <BST_Node>();

            BinarySearchTree.FillListInOrder(BST.Root, listOfNodes);
            foreach (BST_Node A in listOfNodes)
            {
                if (A.Left == N || A.Right == N)
                {
                    return(A);
                }
            }
            return(null);
        }
示例#5
0
 public static BST_Node Insert(BST_Node node, double value, double weight)             // вставка ключа k в дерево с корнем p
 {
     if (node == null)
     {
         return(new BST_Node(weight, value));
     }
     if (weight < node._weight)
     {
         node._left = Insert(node._left, value, weight);
     }
     else
     {
         node._right = Insert(node._right, value, weight);
     }
     return(Balance(node));
 }
示例#6
0
 public static double GetLowerOrEqual(BST_Node node, double weight)
 {
     while (true)
     {
         if (node._weight > weight)
         {
             if (node._left != null)
             {
                 node = node._left;
                 continue;
             }
         }
         else if (node._right != null)
         {
             node = node._right;
             continue;
         }
         return(node._value);
     }
 }
示例#7
0
 private static BST_Node Balance(BST_Node node)             // балансировка узла p
 {
     ResetHeight(node);
     if (BalanceFactor(node) == 2)
     {
         if (BalanceFactor(node._right) < 0)
         {
             node._right = RotateRight(node._right);
         }
         return(RotateLeft(node));
     }
     if (BalanceFactor(node) == -2)
     {
         if (BalanceFactor(node._left) > 0)
         {
             node._left = RotateLeft(node._left);
         }
         return(RotateRight(node));
     }
     return(node);                // балансировка не нужна
 }
示例#8
0
 private void BST_Reset(BST_Node N)
 {
     N.Left  = null;
     N.Right = null;
     N       = null;
 }
示例#9
0
 private static void ResetHeight(BST_Node node)
 {
     node._height = Math.Max(Height(node._left), Height(node._right)) + 1;
 }
示例#10
0
 private static int BalanceFactor(BST_Node node)
 {
     return(Height(node._right) - Height(node._left));
 }
示例#11
0
 private static int Height(BST_Node node)
 {
     return(node == null ? 0 : node._height);
 }
示例#12
0
 public void Add(double weight, double value)
 {
     _root = BST_Node.Insert(_root, value, weight);
 }
示例#13
0
 public double GetLowerOrEqual(double weight)
 {
     return(BST_Node.GetLowerOrEqual(_root, weight));
 }