資料結構中AVL的c++實現__資料結構

來源:互聯網
上載者:User

參考連結:http://blog.csdn.net/cjbct/article/details/53613436


AVL樹中,最重要的便是讓樹重新平衡,我們稱個過程為旋轉。旋轉包括四種,主要是由於插入位置的原因導致的。旋轉的過程可以看代碼中的注釋部分(569行-639行),有詳細的解釋。


這次編寫的過程中,將C++模板類的定義了和函數實現進行了分開(但是仍然在標頭檔中),遇到了比較多的問題。先看看代碼,然後對裡面我遇到的問題進行總結。


先看看AVL樹的實現代碼  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; // 通過enum定義常量      public:       AVLTree() :m_root(nullptr){}       AVLTree(const AVLTree &rhs)       {           m_root = clone(rhs.m_root);       }       ~AVLTree()       {           makeEmpty();       }          /**      * 返回樹的高度。空樹的高度定義為-1      */       int getHeight() const       {           return m_root.height;       }          /**      * 找到樹中的最小值,通過調用private的findMin實現遞迴      */       const Comparable & findMin() const       {           return findMin(m_root)->element;       }          /**      * 找到樹中的最大值,通過調用private的findMax實現遞迴      */       const Comparable & findMax() const       {           return findMax(m_root)->element;       }          /**      * 當x找到時返回真,否則返回假      * 調用了private的那個同名函數,這個是為了遞迴實現      *(因為private中包含了一個AVLNode的指標t)      */       bool contains(const Comparable &x) const       {  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.