예제 #1
0
        AVLNode removeNode(AVLNode root, String wordToRemove)
        {
            // STEP 1: PERFORM STANDARD BST DELETE
            if (root == null)
            {
                return(root);
            }

            // If the key to be deleted is smaller than
            // the root's key, then it lies in left subtree
            if (strcmp(wordToRemove, root.word.word) < 0)
            {
                root.leftNode = removeNode(root.leftNode, wordToRemove);
            }

            // If the key to be deleted is greater than the
            // root's key, then it lies in right subtree
            else if (strcmp(wordToRemove, root.word.word) > 0)
            {
                root.rightNode = removeNode(root.rightNode, wordToRemove);
            }

            // if key is same as root's key, then this is the node
            // to be deleted
            else
            {
                // node with only one child or no child
                if ((root.leftNode == null) || (root.rightNode == null))
                {
                    AVLNode temp = null;
                    if (temp == root.leftNode)
                    {
                        temp = root.rightNode;
                    }
                    else
                    {
                        temp = root.leftNode;
                    }

                    // No child case
                    if (temp == null)
                    {
                        temp = root;
                        root = null;
                    }
                    else // One child case
                    {
                        root = temp; // Copy the contents of
                    }
                    // the non-empty child
                }
                else
                {
                    // node with two children: Get the inorder
                    // successor (smallest in the right subtree)
                    AVLNode temp = minValueNode(root.rightNode);

                    // Copy the inorder successor's data to this node
                    root.word = temp.word;

                    // Delete the inorder successor
                    root.rightNode = removeNode(root.rightNode, temp.word.word);
                }
            }

            // If the tree had only one node then return
            if (root == null)
            {
                return(root);
            }

            // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
            root.height = max(height(root.leftNode),
                              height(root.rightNode)) + 1;

            // STEP 3: GET THE BALANCE FACTOR
            // OF THIS NODE (to check whether
            // this node became unbalanced)
            int balance = getBalance(root);

            // If this node becomes unbalanced,
            // then there are 4 cases
            // Left Left Case
            if (balance > 1 && getBalance(root.leftNode) >= 0)
            {
                return(rightRotate(root));
            }

            // Left Right Case
            if (balance > 1 && getBalance(root.leftNode) < 0)
            {
                root.leftNode = leftRotate(root.leftNode);
                return(rightRotate(root));
            }

            // Right Right Case
            if (balance < -1 && getBalance(root.rightNode) <= 0)
            {
                return(leftRotate(root));
            }

            // Right Left Case
            if (balance < -1 && getBalance(root.rightNode) > 0)
            {
                root.rightNode = rightRotate(root.rightNode);
                return(leftRotate(root));
            }

            return(root);
        }
예제 #2
0
 public void insert(String word, Location location)
 {
     this.root = insertUtility(this.root, word, location);
 }
예제 #3
0
        AVLNode insertUtility(AVLNode node, String wordToAdd, Location location)
        {
            /* 1. Perform the normal BST insertion */
            if (node == null)
            {
                Word word = new Word(wordToAdd, location);
                return(new AVLNode(word));
            }
            // MessageBox.Show(wordToAdd + " " + node.word.word);
            if (strcmp(wordToAdd, node.word.word) < 0)
            {
                node.leftNode = insertUtility(node.leftNode, wordToAdd, location);
            }
            else if (strcmp(wordToAdd, node.word.word) > 0)
            {
                node.rightNode = insertUtility(node.rightNode, wordToAdd, location);
            }
            else
            {
                node.word.count++;
                node.word.locations.AddLast(location);
            }


            /* 2. Update height of this ancestor node */
            node.height = 1 + max(height(node.leftNode),
                                  height(node.rightNode));

            /* 3. Get the balance factor of this ancestor
             *  node to check whether this node became
             *  unbalanced */
            int balance = getBalance(node);

            // If this node becomes unbalanced, then there
            // are 4 cases Left Left Case
            if (balance > 1 && strcmp(wordToAdd, node.leftNode.word.word) < 0)
            {
                return(rightRotate(node));
            }

            // Right Right Case
            if (balance < -1 && strcmp(wordToAdd, node.rightNode.word.word) > 0)
            {
                return(leftRotate(node));
            }

            // Left Right Case
            if (balance > 1 && strcmp(wordToAdd, node.leftNode.word.word) > 0)
            {
                node.leftNode = leftRotate(node.leftNode);
                return(rightRotate(node));
            }

            // Right Left Case
            if (balance < -1 && strcmp(wordToAdd, node.rightNode.word.word) < 0)
            {
                node.rightNode = rightRotate(node.rightNode);
                return(leftRotate(node));
            }

            /* return the (unchanged) node pointer */
            return(node);
        }