Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8330271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:04:38+00:00 2026-06-09T02:04:38+00:00

I am implementing an AVL tree and trying to understand how template works so

  • 0

I am implementing an AVL tree and trying to understand how template works so I can store different types of data in my tree. Right now I can only store int, but I’d like to be able to store other types if I need to. How would I use template for this?

Right now this is the class I have:

struct Avlnode{
int data;
int balfact;
Avlnode *left;
Avlnode *right;
};

class Avltree{
public:
Avltree();
~Avltree( );
Avlnode* insert(int i, bool* j);
static Avlnode* buildtree ( Avlnode *root, int data, bool *h ) ;
void display( Avlnode *root );
Avlnode* deldata ( Avlnode* root, int data, bool *h );
static Avlnode* del ( Avlnode *node, Avlnode* root, bool *h );
static Avlnode* balright ( Avlnode *root, bool *h );
static Avlnode* balleft ( Avlnode* root, bool *h );
void setroot ( Avlnode *avl );
static void deltree ( Avlnode *root );
private:
Avlnode *root;
int items;
};

And they are defined:

Avltree::Avltree( ){
root = NULL;
items = 0;
}

Avlnode* Avltree::insert( int data, bool *h ){
root = buildtree( root, data, h );
return root;
}

Avlnode* Avltree::buildtree( Avlnode *root, int data, bool *h ){
Avlnode *node1, *node2;

if( root == NULL ){
    root = new Avlnode;
    root -> data = data;
    root -> left = NULL;
    root -> right = NULL;
    root -> balfact = 0;
    *h = true;
    return( root );
}
if ( data < root -> data ){
    root -> left = buildtree( root -> left, data, h );

    // If left subtree is higher
    if( *h ){
        switch ( root -> balfact ){
            case 1:
                node1 = root -> left;
                if ( node1 -> balfact == 1 ){
                    //cout << "\nRight rotation.";
                    root -> left = node1 -> right;
                    node1 -> right = root;
                    root -> balfact = 0;
                    root = node1;
                }
                else{
                    //cout << "\nDouble rotation, left then right." ;
                    node2 = node1 -> right;
                    node1 -> right = node2 -> left;
                    node2 -> left = node1;
                    root -> left = node2 -> right;
                    node2 -> right = root;
                    if ( node2 -> balfact == 1 )
                        root -> balfact = -1;
                    else
                        root -> balfact = 0;
                    if ( node2 -> balfact == -1 )
                        node1 -> balfact = 1;
                    else
                        node1 -> balfact = 0;
                    root = node2;
                }
                root -> balfact = 0;
                *h = false;
                break;

            case 0:
                root -> balfact = 1;
                break;
            case -1:
                root -> balfact = 0;
                *h = false;
        }
    }
}

if ( data > root -> data ){
    root -> right = buildtree ( root -> right, data, h );

    if ( *h ){
        switch ( root -> balfact ){
            case 1:
                root -> balfact = 0;
                *h = false;
                break;
            case 0:
                root -> balfact = -1;
                break;
            case -1:
                node1 = root -> right;
                if ( node1 -> balfact == -1 ){
                    //cout << "\nLeft rotation.";
                    root -> right = node1 -> left;
                    node1 -> left = root;
                    root -> balfact = 0;
                    root = node1;
                }
                else{
                    //cout << "\nDouble rotation, right then left.";
                    node2 = node1 -> left;
                    node1 -> left = node2 -> right;
                    node2 -> right = node1;
                    root -> right = node2 -> left;
                    node2 -> left = root;
                    if ( node2 -> balfact == -1 )
                        root -> balfact = 1;
                    else
                        root -> balfact = 0;
                    if ( node2 -> balfact == 1 )
                        node1 -> balfact = -1;
                    else
                        node1 -> balfact = 0;
                    root = node2;
                }
                root -> balfact = 0;
                *h = false;
        }
    }
}
return ( root );
}

void Avltree::display ( Avlnode* root ){
if ( root != NULL ){
    display ( root -> left );
    cout << root -> data << "\t";
    display ( root -> right );
}
}

Avlnode* Avltree::deldata ( Avlnode *root, int data, bool *h ){
Avlnode *node;
if ( root -> data == 13 )
    cout << root -> data ;
if ( root == NULL ){
    //cout << "\nNo such data.";
    return ( root );
}
else{
    if ( data < root -> data ){
        root -> left = deldata ( root -> left, data, h ) ;
        if ( *h )
            root = balright ( root, h ) ;
    }
    else{
        if ( data > root -> data ){
            root -> right = deldata ( root -> right, data, h ) ;
            if ( *h )
                root = balleft ( root, h );
        }
        else{
            node = root;
            if ( node -> right == NULL ){
                root = node -> left ;
                *h = true ;
                delete ( node ) ;
            }
            else{
                if ( node -> left == NULL ){
                    root = node -> right ;
                    *h = true ;
                    delete ( node ) ;
                }
                else{
                    node -> right = del ( node -> right, node, h ) ;
                    if ( *h )
                        root = balleft ( root, h ) ;
                }
            }
        }
    }
}
return ( root ) ;
}

Avlnode* Avltree :: del ( Avlnode *succ, Avlnode *node, bool *h ){
Avlnode *temp = succ ;

if ( succ -> left != NULL ){
    succ -> left = del ( succ -> left, node, h ) ;
    if ( *h )
        succ = balright ( succ, h ) ;
}
else{
    temp = succ ;
    node -> data = succ -> data ;
    succ = succ -> right ;
    delete ( temp ) ;
    *h = true ;
}
return ( succ ) ;
}

Avlnode* Avltree :: balright ( Avlnode *root, bool *h ){
Avlnode *temp1, *temp2 ;
switch ( root -> balfact ){
    case 1 :
        root -> balfact = 0 ;
        break ;
    case 0 :
        root -> balfact = -1 ;
        *h  = false ;
        break ;
    case -1 :
        temp1 = root -> right ;
        if ( temp1 -> balfact <= 0 ){
            cout << "\nLeft rotation." ;
            root -> right = temp1 -> left ;
            temp1 -> left = root ;
            if ( temp1 -> balfact == 0 ){
                root -> balfact = -1 ;
                temp1 -> balfact = 1 ;
                *h = false ;
            }
            else{
                root -> balfact = temp1 -> balfact = 0 ;
            }
            root = temp1 ;
        }
        else{
            cout << "\nDouble rotation, right then left." ;
            temp2 = temp1 -> left ;
            temp1 -> left = temp2 -> right ;
            temp2 -> right = temp1 ;
            root -> right = temp2 -> left ;
            temp2 -> left = root ;
            if ( temp2 -> balfact == -1 )
                root -> balfact = 1 ;
            else
                root -> balfact = 0 ;
            if ( temp2 -> balfact == 1 )
                temp1 -> balfact = -1 ;
            else
                temp1 -> balfact = 0 ;
            root = temp2 ;
            temp2 -> balfact = 0 ;
        }
}
return ( root ) ;
}

Avlnode* Avltree::balleft ( Avlnode *root, bool *h ){
Avlnode *temp1, *temp2 ;
switch ( root -> balfact ){
    case -1 :
        root -> balfact = 0 ;
        break ;

    case 0 :
        root -> balfact = 1 ;
        *h = false ;
        break ;

    case 1 :
        temp1 = root -> left ;
        if ( temp1 -> balfact >= 0 ){
            cout << "\nRight rotation." ;
            root -> left = temp1 -> right ;
            temp1 -> right = root ;

            if ( temp1 -> balfact == 0 ){
                root -> balfact = 1 ;
                temp1 -> balfact = -1 ;
                *h = false ;
            }
            else{
                root -> balfact = temp1 -> balfact = 0 ;
            }
            root = temp1 ;
        }
        else{
            cout << "\nDouble rotation, left then right." ;
            temp2 = temp1 -> right ;
            temp1 -> right = temp2 -> left ;
            temp2 -> left = temp1 ;
            root -> left = temp2 -> right ;
            temp2 -> right = root ;
            if ( temp2 -> balfact == 1 )
                root -> balfact = -1 ;
            else
                root -> balfact = 0 ;
            if ( temp2-> balfact == -1 )
                temp1 -> balfact = 1 ;
            else
                temp1 -> balfact = 0 ;
            root = temp2 ;
            temp2 -> balfact = 0 ;
        }
}
return ( root ) ;
}

void Avltree::setroot ( Avlnode *avl ){
root = avl ;
}
Avltree::~Avltree( ){
deltree ( root ) ;
}

void Avltree::deltree ( Avlnode *root ){
if ( root != NULL ){
    deltree ( root -> left ) ;
    deltree ( root -> right ) ;
}
delete ( root ) ;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:04:39+00:00Added an answer on June 9, 2026 at 2:04 am

    First make Avlnode a class template:

    template<typename T>
    struct Avlnode{
      typedef value_type T;
      value_type data;
      value_type balfact;
      Avlnode<T> *left;
      Avlnode<T> *right;
    };
    

    Then, make Avltree a class template:

    template <typename T>
    class Avltree{
    public:
    typedef T value_type;
    typedef node_type Avlnode<T>;
    Avltree();
    ~Avltree( );
    node_type* insert(const T& i, bool* j);
    static node_type* buildtree ( node_type*root, const T& data, bool *h ) ;
    void display( node_type*root );
    node_type* deldata ( node_type* root, const T& data, bool *h );
    static node_type* del ( node_type* node, node_type* root, bool *h );
    ....
    private:
    node_type *root;
    T items;
    };
    

    Then, to instantiate:

    Avltree<float> treeF;
    bool smth = true;
    typename AvlTree<float>::node_type* node = treeF.insert(0.5, &smth);
    

    Although I would recommend passing the bools by reference rather than pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing an AVL binary tree data structure in C# .NET 2.0 (possibly moving
I am implementing an avl tree for my assignment. #include <string.h> #include <stdlib.h> #include
I'm implementing some classes to handle the common data structures (Tree, BinaryTree, Search Binary
I'm working on implementing an AVL Search tree. So far I've finished the coding
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
I'm working on an AVL Tree. The tree itself seems to be working but
Implementing a custom VM and I've come to use registers (these will store pointers
Implementing Kings' Corners (glorified multiplayer Solitaire) in Java. I'm trying to allow a player
Implementing 8bit ALU in VHDL with unsigned numbers only. When the result of the
I'm looking for the best way to calculate a nodes balance in an AVL-tree

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.