📜  avl tree c++ (1)

📅  最后修改于: 2023-12-03 14:39:24.557000             🧑  作者: Mango

AVL Tree in C++

Introduction

AVL Tree is a self-balancing binary search tree. It improves the performance of ordinary binary search trees (BSTs) by ensuring that the height difference between the left and right subtrees of any node is at most 1.

This self-balancing property allows AVL trees to provide efficient operations with a worst-case time complexity of O(log n), where n is the number of elements stored in the tree.

In this article, we will explore the AVL tree implementation in C++. We will discuss the structure, operations, and provide C++ code illustrating these concepts.

Structure of AVL Tree

Each node in an AVL tree contains a key (or value), a left child pointer, a right child pointer, and a height. The height of a node is defined as the longest path from the node to a leaf node.

The AVL tree has the following properties:

  1. The heights of the left and right subtrees of any node differ by at most 1.
  2. The left and right subtrees are also AVL trees.
Operations on AVL Tree
  1. Insertion: To insert an element into an AVL tree, we recursively search for the appropriate position based on the key value. After insertion, we balance the tree if any violation of the AVL tree property occurs.
  2. Deletion: To delete an element from an AVL tree, we recursively search for the node to delete based on the key value. After deletion, we balance the tree if necessary.
  3. Search: To search for an element in an AVL tree, we recursively compare the search key with the keys of the nodes and traverse left or right based on the comparison result until we find the desired value or reach a leaf node.
  4. Traversal: We can traverse an AVL tree in pre-order, in-order, or post-order. Pre-order traversal visits the current node before its children, in-order traversal visits the current node between its left and right children, and post-order traversal visits the current node after its children.
C++ Implementation

Here is a C++ implementation of an AVL tree:

// Node structure
struct Node {
    int key;
    Node* left;
    Node* right;
    int height;
};

// AVL tree class
class AVLTree {
private:
    Node* root;

    // Insertion helper function
    Node* insertNode(Node* root, int key);

    // Deletion helper function
    Node* deleteNode(Node* root, int key);

    // Utility functions for balancing and rotating nodes
    int getHeight(Node* node);
    int getBalanceFactor(Node* node);
    Node* rotateLeft(Node* node);
    Node* rotateRight(Node* node);
    Node* balanceNode(Node* node);

public:
    // Constructor
    AVLTree();

    // Insertion operation
    void insert(int key);

    // Deletion operation
    void remove(int key);

    // Search operation
    bool search(int key);

    // Traversal operations
    void preOrderTraversal();
    void inOrderTraversal();
    void postOrderTraversal();
};

For the complete implementation with method definitions, you can refer to this AVL Tree implementation in C++ GitHub repository.

Conclusion

AVL trees are a powerful data structure that provides efficient search, insert, and delete operations, all with a worst-case time complexity of O(log n). They are widely used in computer science and have various applications, such as in database indexing and compiler implementations. Understanding AVL trees and their implementation in C++ can greatly enhance a programmer's ability to solve complex problems efficiently.