The data structure of AVL in C + + to achieve __ data structure

Source: Internet
Author: User
Tags class definition comparable

Reference Link: http://blog.csdn.net/cjbct/article/details/53613436


The most important thing in the AVL tree is to rebalance the tree, which we call a process of rotation. The rotation consists of four kinds, mainly due to the insertion position. The process of rotation can be seen in the comments section of the code (569 lines-639 lines), with a detailed explanation.


In this writing process, the C + + template class definition and function implementation are separated (but still in the header file), encountered more problems. Look at the code first, and then summarize the problems I encountered.


First look at the AVL Tree Implementation code AVLTree.h:

[CPP]  View plain  copy #ifndef  AVL_TREE_H   #define  AVL_TREE_H      # include<iostream>   using namespace std;         Template  <typename Comparable>   class avltree   {      public:       typedef enum _order {PREORDER, INORDER,  postorder} order; //  define constants via enum       public:        avltree ()  :m_root (nullptr) {}       avltree (const avltree  &AMP;RHS)        {           m_ Root = clone (rhs.m_root);       }       ~ Avltree ()        {            MakeempTy ();       }          /**       *  returns the height of the tree. The height of the empty tree is defined as -1      */       int getheight ()  const        {           return m _root.height;       }          /**      *  find the minimum value in the tree by calling private findmin to implement recursion       */       const comparable & findmin ()  const        {           return findmin (m_root)-> element;       }          /**       *  find the maximum value in the tree and implement recursion       */& by calling private Findmaxnbsp;      const comparable & findmax ()  const       {           return findmax (m_ Root)->element;       }          /**       *  Returns True when X is found, otherwise returns false       *  calls private's that same name function. This is for recursive implementation       * (because private contains a avlnode pointer t)       */        bool contains (const comparable &x)  const       {  

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.