Esempio n. 1
0
 public Node(ButtonClass i, Node l, Node r, int lv)
 {
     ButtonBST = i;
     lchild    = l;
     rchild    = r;
     level     = lv;
 }
Esempio n. 2
0
        public void insert(int element, ref int maxlv, FormBST F)
        {
            Node        newnode, currentNode = ROOT, parent = currentNode;
            Point       LocationNode;
            int         lvNode = 0;
            ButtonClass newBT  = new ButtonClass();

            newBT.Click   += new EventHandler(F.buttonClass_Click);
            newBT.Text     = Convert.ToString(element);
            newBT.Location = new Point(F.Width - 70, 100);
            F.Controls.Add(newBT);
            F.Update();
            Thread.Sleep(500);
            find(element, ref parent, ref currentNode, ref lvNode);
            if (lvNode > maxlv)
            {
                maxlv = lvNode;
            }
            if (ROOT != null)
            {
                parent.ButtonBST.BackColor = Color.Blue;
                parent.ButtonBST.Update();
                Thread.Sleep(500);
                movingTree(ROOT, maxlv, newBT, F);
            }
            if (currentNode != null)
            {
                MessageBox.Show("Duplicates words not allowed");
                F.Controls.Remove(newBT);
                newBT.Dispose();

                currentNode.ButtonBST.BackColor = SystemColors.ButtonFace;
                currentNode.ButtonBST.UseVisualStyleBackColor = true;
                currentNode.ButtonBST.Update();

                parent.ButtonBST.BackColor = SystemColors.ButtonFace;
                parent.ButtonBST.UseVisualStyleBackColor = true;
                parent.ButtonBST.Update();
            }
            else
            {
                newnode   = new Node(newBT, null, null, lvNode);
                newnode.g = F.CreateGraphics();
                if (parent == null)
                {
                    ROOT         = newnode;
                    LocationNode = new Point(F.Width / 2 - 30, 100);
                    F.Controls.Add(ROOT.ButtonBST);
                    move(ROOT, LocationNode, maxlv);
                }
                else if (element < Convert.ToInt32(parent.ButtonBST.Text))
                {
                    parent.lchild = newnode;
                    if (parent.ButtonBST.Location.Y + 100 >= F.Height)
                    {
                        F.Height += 50;
                        F.Update();
                        Thread.Sleep(500);
                    }

                    LocationNode = new Point(parent.ButtonBST.Location.X - (int)(30 * Math.Pow(2, (maxlv - parent.lchild.level))), parent.ButtonBST.Location.Y + 50);
                    F.Controls.Add(parent.lchild.ButtonBST);
                    move(parent.lchild, LocationNode, maxlv);
                    parent.lchild.g = F.CreateGraphics();
                    parent.lchild.g.DrawLine(new Pen(Color.Black), parent.ButtonBST.Location.X + 25, parent.ButtonBST.Location.Y + 25, parent.lchild.ButtonBST.Location.X + 25, parent.lchild.ButtonBST.Location.Y + 25);
                }
                else
                {
                    parent.rchild = newnode;
                    if (parent.ButtonBST.Location.Y + 100 >= F.Height)
                    {
                        F.Height += 50;
                        F.Update();
                        Thread.Sleep(500);
                    }
                    LocationNode = new Point(parent.ButtonBST.Location.X + (int)(30 * Math.Pow(2, (maxlv - parent.rchild.level))), parent.ButtonBST.Location.Y + 50);
                    F.Controls.Add(parent.rchild.ButtonBST);
                    move(parent.rchild, LocationNode, maxlv);
                }
                Thread.Sleep(500);
                if (parent != null)
                {
                    parent.ButtonBST.BackColor = SystemColors.ButtonFace;
                    parent.ButtonBST.UseVisualStyleBackColor = true;
                    parent.ButtonBST.Update();
                }
            }
        }