示例#1
0
 private T minNodeValue(MyTreeSetImpl <T> node)
 {
     if (node.getLeftChild() == null)
     {
         T value = node.getValue();
         if (node.getRightChild() != null)
         {
             if (this.left == false)
             {
                 this.replecementParent.setRightChild(node.getRightChild());
                 return(value);
             }
             else
             {
                 this.replecementParent.setLeftChild(node.getRightChild());
                 return(value);
             }
         }
         else
         {
             if (this.left == false)
             {
                 this.replecementParent.setRightChild(this.changeNode);
                 return(value);
             }
             else
             {
                 this.replecementParent.setLeftChild(this.changeNode);
                 return(value);
             }
         }
     }
     this.replecementParent = (MyTreeSetImpl <T>)node;
     if (this.right == false)
     {
         this.right = true;
         return(minNodeValue((MyTreeSetImpl <T>)node.getRightChild()));
     }
     else
     {
         this.left = true;
         return(minNodeValue((MyTreeSetImpl <T>)node.getLeftChild()));
     }
 }
示例#2
0
        override public bool contains(T value)
        {
            MyTreeSetImpl <T> t = this;

            if (this.root == true)
            {
                while (t != null)
                {
                    if (t.comparator.Compare(value, t.getValue()) == 0)
                    {
                        return(true);
                    }
                    else if (t.comparator.Compare(value, t.getValue()) < 0)
                    {
                        t = (MyTreeSetImpl <T>)t.getLeftChild();
                    }
                    else
                    {
                        t = (MyTreeSetImpl <T>)t.getRightChild();
                    }
                }
            }
            return(false);
        }
示例#3
0
 public bool deleteNode(MyTreeSetImpl <T> node, T value)
 {
     if (node.getRoot() == false)
     {
         return(false);
     }
     if (node.comparator.Compare(value, node.getValue()) == 0)
     {
         if (node.getLeftChild() == null && node.getRightChild() == null)
         {
             if (this.parent != null)
             {
                 if (this.right == true)
                 {
                     this.parent.setRightChild(this.changeNode);
                 }
                 else
                 {
                     this.parent.setLeftChild(this.changeNode);
                 }
             }
             else
             {
                 node.setRoot(false);
             }
             return(true);
         }
         if (null != node.getLeftChild() && null != node.getRightChild())
         {
             this.right = false;
             this.left  = false;
             node.setValue(minNodeValue(node));
             return(true);
         }
         if (node.getLeftChild() != null)
         {
             if (this.parent != null)
             {
                 if (this.left == true)
                 {
                     this.parent.setLeftChild(node.getLeftChild());
                 }
                 else
                 {
                     this.parent.setRightChild(node.getLeftChild());
                 }
                 return(true);
             }
             else
             {
                 node.setValue(node.getLeftChild().getValue());
                 node.setLeftChild(this.changeNode);
                 return(true);
             }
         }
         if (node.getRightChild() != null)
         {
             if (this.parent != null)
             {
                 if (this.right == true)
                 {
                     this.parent.setRightChild(node.getRightChild());
                 }
                 else
                 {
                     this.parent.setLeftChild(node.getRightChild());
                 }
                 return(true);
             }
             else
             {
                 node.setValue(node.getRightChild().getValue());
                 node.setRightChild(this.changeNode);
                 return(true);
             }
         }
     }
     this.parent = node;
     if (node.comparator.Compare(node.getValue(), value) > 0)
     {
         this.left  = true;
         this.right = false;
         return(deleteNode((MyTreeSetImpl <T>)node.getLeftChild(), value));
     }
     if (node.comparator.Compare(node.getValue(), value) < 0)
     {
         this.right = true;
         this.left  = false;
         return(deleteNode((MyTreeSetImpl <T>)node.getRightChild(), value));
     }
     return(false);
 }