예제 #1
0
        private Tuple <BinaryNode <int>, BinaryNode <int> > FindNode(int value, BinaryNode <int> node, BinaryNode <int> parent)
        {
            BinaryNode <int> target = null;

            if (node != null)
            {
                if (value == node.value)
                {
                    return(new Tuple <BinaryNode <int>, BinaryNode <int> >(node, parent));
                }

                else if (value > node.value)
                {
                    return(FindNode(value, node.right, node));
                }
                else
                {
                    return(FindNode(value, node.left, node));
                }
            }

            else
            {
                return(null);
            }
        }
예제 #2
0
        public bool RemoveNode(int value)
        {
            if (value < 0)
            {
                return(false);
            }

            bool             found        = false;
            bool             isRoot       = false;
            bool             isRightChild = false;
            BinaryNode <int> parent;
            BinaryNode <int> target              = FindWithParent(value, out parent);
            BinaryNode <int> newNode             = null;
            BinaryNode <int> leftMostChildParent = null;

            if (parent == null)
            {
                isRoot = true;
            }
            else
            {
                isRightChild = target.value > parent.value;
            }

            //if it has no right child
            if (target.right == null)
            {
                //and it hasn't left too
                if (target.left == null)
                {
                    if (isRoot)
                    {
                        Root = null;
                    }
                    else
                    {
                        if (isRightChild)
                        {
                            parent.right = null;
                        }
                        else
                        {
                            parent.left = null;
                        }
                    }
                    found = true;
                }

                //the target has a left child only (no right of course)
                else
                {
                    if (isRoot)
                    {
                        Root = target.left;
                    }

                    else
                    {
                        if (isRightChild)
                        {
                            parent.right = target.left;
                        }

                        else
                        {
                            target.right.left = target.left;
                        }
                    }
                    found = true;
                }
            }
            //has a right child
            //it can have left child of course but our focus is on the right child
            else
            {
                //that right child has a left child
                if (target.right.left != null)
                {
                    BinaryNode <int> leftMostChild = target.right;

                    //get most left child
                    while (leftMostChild.left.left != null)
                    {
                        leftMostChild = leftMostChild.left;
                    }

                    //to be able to get the parent of the left most child to remove it is pointer to our new target (which is its left child)
                    leftMostChildParent      = leftMostChild;
                    leftMostChild            = leftMostChild.left;
                    leftMostChildParent.left = null;
                    if (isRoot)
                    {
                        Root       = leftMostChild;
                        Root.left  = target.left;
                        Root.right = target.right;
                    }
                    else
                    {
                        if (isRightChild)
                        {
                            parent.right       = leftMostChild;
                            parent.right.right = target.right;
                            parent.right.left  = target.left;
                        }

                        else
                        {
                            parent.left       = leftMostChild;
                            parent.left.right = target.right;
                            parent.left.left  = target.left;
                        }
                    }
                    found = true;
                }

                //that right child doesn't have a left child
                else
                {
                    if (isRoot)
                    {
                        Root      = target.right;
                        Root.left = target.left;
                    }
                    else
                    {
                        if (isRightChild)
                        {
                            parent.right      = target.right;
                            parent.right.left = target.left;
                        }
                        else
                        {
                            //replace the node with its right child
                            parent.left = target.right;

                            //connects the node with any left child it was connected to before (parent.left is our new node)
                            parent.left.left = target.left;
                        }
                    }

                    found = true;
                }
            }

            return(found);
        }
예제 #3
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public BinaryTree()
 {
     count = 0;
     root  = null;
 }
예제 #4
0
 private void myRoot(BinaryNode <T> root)
 {
     this.root = root;
 }
예제 #5
0
        private void Add(BinaryNode <T> newElement, BinaryNode <T> root)
        {
            if (root == null)
            {
                this.SetRoot(newElement);
                count++;
                this.root.SetPadre(null);
            }
            else
            {
                BinaryNode <T> binaryNode = this.root;

                while (binaryNode != null)
                {
                    int compare = newElement.Value.CompareTo(binaryNode.Value);

                    if (compare < 0)
                    {
                        BinaryNode <T> left = binaryNode.GetLeft();

                        if (left == null)
                        {
                            binaryNode.SetLeft(newElement);
                            binaryNode.GetLeft().SetPadre(binaryNode);
                            count++;
                            InsertBalance(binaryNode, 1);
                            return;
                        }
                        else
                        {
                            binaryNode = left;
                        }
                    }
                    else
                    {
                        if (compare > 0)
                        {
                            BinaryNode <T> right = binaryNode.GetRight();

                            if (right == null)
                            {
                                binaryNode.SetRight(newElement);
                                binaryNode.GetRight().SetPadre(binaryNode);
                                count++;
                                InsertBalance(binaryNode, -1);
                                return;
                            }
                            else
                            {
                                binaryNode = right;
                            }
                        }
                        else
                        {
                            binaryNode.Value = newElement.Value;
                            return;
                        }
                    }
                }
            }
        }
예제 #6
0
파일: AVLTree.cs 프로젝트: Premsjce/DJP
 public void Delete(int data)
 {
     Root = RecursivelyDelete(Root, data);
 }
예제 #7
0
 public AVLTree()
 {
     count = 0;
     root  = null;
 }
 public BinaryNode(K key, V value, BinaryNode left, BinaryNode right)
 {
     this.Record = new KeyValuePair <K, V>(key, value);
     this.Left   = left;
     this.Right  = right;
 }
        public bool Remove(K key)
        {
            BinaryNode parent      = default(BinaryNode);
            BinaryNode lastVisited = default(BinaryNode);
            BinaryNode located     = this.TraverseTo(this.root, key, out parent, out lastVisited);

            if (located == null)
            {
                return(false);
            }
            else
            {
                if (parent == null)
                {
                    if (this.NodeIsLeaf(this.root))
                    {
                        this.root = null;
                    }
                    else if (this.root.Left != null && this.root.Right != null)
                    {
                        this.TwoChildRemove(this.root);
                    }
                    else if (this.root.Left == null)
                    {
                        this.root = this.root.Right;
                    }
                    else
                    {
                        this.root = this.root.Left;
                    }
                }
                else
                {
                    if (parent.Left != null && parent.Left.Record.Key.Equals(key))
                    {
                        if (this.NodeIsLeaf(located))
                        {
                            parent.Left = null;
                        }
                        else if (located.Left != null && located.Right != null)
                        {
                            this.TwoChildRemove(parent.Left);
                        }
                        else if (located.Left == null)
                        {
                            parent.Left = parent.Left.Right;
                        }
                        else
                        {
                            parent.Left = parent.Left.Left;
                        }
                    }
                    else
                    {
                        if (this.NodeIsLeaf(located))
                        {
                            parent.Right = null;
                        }
                        else if (located.Left != null && located.Right != null)
                        {
                            this.TwoChildRemove(parent.Right);
                        }
                        else if (located.Left == null)
                        {
                            parent.Right = parent.Right.Right;
                        }
                        else
                        {
                            parent.Right = parent.Right.Left;
                        }
                    }
                }

                this.Count--;
                return(true);
            }
        }
 public void Clear()
 {
     this.root  = null;
     this.Count = 0;
 }
        private BinaryNode TraverseTo(BinaryNode start, K key, out BinaryNode parent, out BinaryNode lastVisited)
        {
            BinaryNode previous = null;
            BinaryNode current  = start;

            while ((current != null && !current.Record.Key.Equals(key)) && !this.NodeIsLeaf(current))
            {
                if (key.CompareTo(current.Record.Key) < 0)
                {
                    if (current.Left == null)
                    {
                        break;
                    }

                    previous = current;
                    current  = current.Left;
                }
                else
                {
                    if (current.Right == null)
                    {
                        break;
                    }

                    previous = current;
                    current  = current.Right;
                }
            }

            parent      = previous;
            lastVisited = current;

            if (current != null && current.Record.Key.Equals(key))
            {
                return(current);
            }
            else
            {
                return(null);
            }
        }