Example #1
0
 public void Add(int value)
 {
     if (value < Value)
     {
         if (Left == null)
         {
             Left = new RBTree(value, this);
         }
         else
         {
             Left.Add(value);
         }
     }
     else
     {
         if (Right == null)
         {
             Right = new RBTree(value, this);
         }
         else
         {
             Right.Add(value);
         }
     }
 }
Example #2
0
        private void AddInTree(RBTree tree, TreeNode parent = null)
        {
            var node = new TreeNode(tree.Value.ToString());

            node.ForeColor = Color.White;
            if (tree.Color == ColorNode.Black)
            {
                node.BackColor = Color.Black;
            }
            else
            {
                node.BackColor = Color.Red;
            }
            if (tree.Parent == null)
            {
                treeView1.Nodes.Add(node);
            }
            else
            {
                parent.Nodes.Add(node);
            }
            parent = node;
            if (tree.Left != null)
            {
                AddInTree(tree.Left, parent);
            }
            if (tree.Right != null)
            {
                AddInTree(tree.Right, parent);
            }
        }
Example #3
0
 public Form1()
 {
     InitializeComponent();
     tree = null;
     treeView1.ShowLines     = false;
     treeView1.ShowPlusMinus = false;
 }
Example #4
0
 private void Print(RBTree tree)
 {
     if (tree.Left != null)
     {
         Print(tree.Left);
     }
     Console.Write(tree.Value + " ");
     if (tree.Right != null)
     {
         Print(tree.Right);
     }
 }
Example #5
0
        private void button2_Click(object sender, EventArgs e)
        {
            int value = Int32.Parse(textBox1.Text);

            if (tree != null)
            {
                if (tree.Remove(value))
                {
                    tree = null; // удалили последний элемент
                }
            }
            RenderTree();
        }
Example #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            int value = Int32.Parse(textBox1.Text);

            if (tree == null)
            {
                tree = new RBTree(value);
            }
            else
            {
                tree.Add(value);
            }
            RenderTree();
        }
Example #7
0
        private bool Remove(RBTree tree)
        {
            if (tree == null)
            {
                return(false);
            }

            //Корень
            if (tree.Parent == null)
            {
                if (tree.Left != null)
                {
                    tree.Value = tree.Left.Value;
                    Remove(tree.Left);
                }
                else if (tree.Right != null)
                {
                    tree.Value = tree.Right.Value;
                    Remove(tree.Right);
                }
                else
                {
                    return(true);//TODO: удаление последнего элемента
                }
                return(false);
            }
            //Лист
            if (tree.Left == null && tree.Right == null)
            {
                if (tree.Parent.Left == tree)
                {
                    tree.Parent.Left = null;
                }
                else
                {
                    tree.Parent.Right = null;
                }
            }

            //Узел с левым поддеревом
            if (tree.Left != null && tree.Right == null)
            {
                tree.Left.Parent = tree.Parent;
                if (tree.Parent.Left == tree)
                {
                    tree.Parent.Left = tree.Left;
                }
                else
                {
                    tree.Parent.Right = tree.Left;
                }
            }

            ///Узел с правым поддеревом
            if (tree.Left == null && tree.Right != null)
            {
                tree.Right.Parent = tree.Parent;
                if (tree.Parent.Left == tree)
                {
                    tree.Parent.Left = tree.Right;
                }
                else
                {
                    tree.Parent.Right = tree.Right;
                }
            }

            ///Узел с правым и левым поддеревом
            if (tree.Left != null && tree.Right != null)
            {
                RBTree current = tree.Right;
                while (current.Left != null)
                {
                    current = current.Left;
                }

                tree.Value = current.Value;
                Remove(current);
            }

            return(false);
        }
Example #8
0
 public RBTree(int value, RBTree parent = null)
 {
     Value  = value;
     Parent = parent;
     Color  = ColorNode.Red;
 }
Example #9
0
        public bool Remove(int value)
        {
            RBTree tree = Search(value);

            return(Remove(tree));
        }