DIY implementation of data Structure--AVL tree (c + + implementation)

Source: Internet
Author: User
Tags int size

This kind of tutorial has a lot of, in C + + simple implementation for the record and reference, and later there is a supplemental version.

Realize the search and insert, delete operation is a bit complex, feel the efficiency of personal implementation is not very high, and then add, the first thing to record down.


Avl.h

#ifndef __avl_h#define __avl_h#include<stddef.h> #include <vector>template< class t>struct avlnode{    T data;    int height;    Avlnode* left;    avlnode* right; Avlnode (T value):d ata (value), left (null), Right (null), height (1) {};};        template< class T >class avltree{public:avltree ():p tree (NULL) {};        ~avltree ();        int height (avlnode<t>* node) const;        int max_height (int a, int b);        order void Preordertree (avlnode<t>* tree) const;        void Inordertree (avlnode<t>* tree) const;        void Postordertree (avlnode<t>* tree) const;        rotate void Singlerotatel (avlnode<t>* &parent);        void Singlerotater (avlnode<t>* &parent);        void Doublerotatelr (avlnode<t>* &parent);        void Doublerotaterl (avlnode<t>* &parent);        find void Find_min (avlnode<t>* node);        void Find_max (avlnode<t>* node); Insert VoiD Insert (avlnode<t>* &root, T value); Avlnode<t>* PTree;}; #endif

avl.cc

#include "Avl.h" #include <iostream> #include <cmath>using namespace std;template< class T>void Avltree<t>::insert (avlnode<t>* &root, T value) {if (NULL = = root) {root = new avlnode<t&        gt; (value);        if (NULL = = root) {cout << "Init failed" <<endl;        }} else if (value < root-> data) {Insert (root-> left, value);                if (2 = = height (root->left)-height (root->right)) {if (Value < Root->left->data)            Singlerotater (root);        else DOUBLEROTATELR (root);        }} else{Insert (root->right, value);                if (2 = = height (root->right)-height (root->left)) {if (Value < Root->right->data)            Doublerotaterl (root);        else Singlerotatel (root); }} root->height = Max_height (height (root->left), Height (root->right)) + 1;} template< class T>void Avltree<t>::singlerotater (avlnode<t>* &parent) {AvlNode<T>* child    = parent->left;    Parent->left = child->right;    Child->right = parent;    Parent->height = max_height (height (parent->left), height (parent->right)) +1;    Child->height = max_height (height (child->left), height (child->right)) +1; parent = child;} template< class T>void Avltree<t>::singlerotatel (avlnode<t>* &parent) {AvlNode<T>* child    = parent->right;    Parent->right = child->left;    Child->left = parent;    Parent->height = max_height (height (parent->left), height (parent->right)) +1;    Child->height = max_height (height (child->left), height (child->right)) +1; parent = child;} template< class T >void avltree<t>::D oublerotatelr (avlnode<t>* &parent) {SingleRotateL (parent    , left); Singlerotater (parent);} template< class T &Gt;void avltree<t>::D Oublerotaterl (avlnode<t>* &parent) {singlerotater (parent-right); Singlerotatel (parent);}     Template<class t>int Avltree<t>::height (avlnode<t>* node) const{if (NULL = = node) return 0; else return node->height;} Template<class t>int avltree<t>::max_height (int a, int b) {return a > B? a:b;} Template<class t>void avltree<t>::P reordertree (avlnode<t>* tree) const{if (NULL! = tree) {Cou        T << tree->data << "";        Preordertree (Tree->left);    Preordertree (Tree->right); }}template<class t>void avltree<t>::inordertree (avlnode<t>* tree) const{if (NULL! = tree) {I        Nordertree (Tree->left);        cout << tree->data << "";    Inordertree (Tree->right);        }}template<class t>void avltree<t>::P ostordertree (avlnode<t>* tree) const{if (NULL! = tree) { PostOrdertree (Tree->left);        Postordertree (Tree->right);    cout << tree->data << "";    }}int Main (void) {int input;    int size;    int i = 0;    avltree<int>* tree = new avltree<int> ();    cout << "Input the size" <<endl;    CIN >> size;    cout << "Input the value" <<endl;        while (I < size) {cin >> input;        Tree->insert (tree->ptree, input);    ++i;    } tree->preordertree (Tree->ptree);    cout<<endl;    Tree->inordertree (Tree->ptree);    cout<<endl;            Tree->postordertree (Tree->ptree); return 0;}

Test:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

DIY implementation of data Structure--AVL tree (c + + implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.