GithubHelp home page GithubHelp logo

avltree's Introduction

AVL Tree

AVL Tree implementation in C++ using classes and templates.
This tree is a special case of augmented BST. AVL tree is a self-balancing tree, ie it prevents skewness while the insertion and deletion operation. Height of each subtree rooted at the current node is stored with the current node.
For each node:

height = 1 + max( height( left_child ), height( right_child ) )

Basic Operation

AVL tree always maintains a loose balance ie the heights of the subtrees on both sides of a node can differ by atmost one.
The balance in the tree heights is achieved by the following two operations:

Right Rotation

        y                              x
       / \      Right Rotation        / \
      x   C     -------------->      A   y
     / \                                / \
    A   B                              B   C

Here, x and y are nodes while A, B and C are AVL trees.
It can be observed that the right height increases by one while the left length may or may not decrease by one for an AVL tree.

Left Rotation

        x                              y
       / \       Left Rotation        / \
      A   y      ------------->      x   C
         / \                        / \
        B   C                      A   B

Here, x and y are nodes while A, B and C are AVL trees.
It can be observed that the left height increases by one while the right length may or may not decrease by one for an AVL tree.

Balancing

Following are the states of nodes and the way to balance the tree. Notice that the height difference at the root node is 2 (loose balance is maintained).
Here x and y are nodes while h,h+1 denote the height of tree.

Case I

        y                              x
       / \      Right Rotation        / \
      x   h     -------------->     h+1  y
     / \             on y               / \
   h+1 h/h+1                         h/h+1 h

Case II

        y                              y
       / \       Left Rotation        / \
      x   h      ------------->      z   h
     / \             on x           / \
    h  h/h+1                      h+1 h/h+1

Then,

        y                              z
       / \      Right Rotation        / \
      z   h     -------------->     h+1  y
     / \             on y               / \
   h+1 h/h+1                         h/h+1 h

Case III

        x                              y
       / \       Left Rotation        / \
      h   y      ------------->      x   h+1
         / \         on x           / \
     h/h+1 h+1                     h h/h+1

Case IV

        y                              y
       / \      Right Rotation        / \
      h   x     -------------->      h   z
         / \        on x                / \
      h/h+1 h                       h/h+1 h+1

Then,

        y                              z
       / \       Left Rotation        / \
      h   z      ------------->      y  h+1
         / \         on y           / \
      h/h+1 h+1                    h h/h+1

avltree's People

Contributors

ekan5h avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.