AVL tree dictionary

So far, I have drawn up an attack plan to see how I can do this, and this is what I have:

bool isEmpty() const - returns true if empty, false if not

int getSize() - returns the number of words stored in the dictionary

void insert (String word) - insert words into the dictionary, if they are not already present, and then update.

boolfind(String word, WordNode & x) - returns true if the word is present and puts the data in x.

void printSorted() - prints words in a tree in lexicographical order (indicated)

void remove (String word) - implements lazy delete node

I have a concept of what I want to do, and I understand how AVL trees work. But I was completely stuck when it came to writing code, can someone help me get started?

+5
source share
1

( ) . , . ; .

() :

/*
 * Insert a new key into a binary tree, or find an existing one.
 *
 * Return the new or existing node whose key is equal to the one requested.
 * Update *node with the new (sub)tree root.
 */
Node *insert(Node **node, String key)
{
    if (*node == NULL) {
        *node = new Node(key);
        return *node;
    }

    if (key < (*node)->key)
        return insert(&(*node)->left, key);

    if (key > (*node)->key)
        return insert(&(*node)->right, key);

    return *node;
}

, , AVL.

AVL:

  • node ( ) -1, 0 +1.
  • .

AVL . , .

, a node , .. 1.

node? , :

  • height node node, .
  • balance node. , ( ).
  • . , , AVL. , .

3- , .

, "" " ", :

int height(Node *node)
{
    if (node == NULL)
        return -1;
    return std::max(height(node->left), height(node->right)) + 1;
}

int balanceFactor(Node *node)
{
    assert(node != NULL);
    return height(node->right) - height(node->left);
}

, , , , .

, !

+2

All Articles