<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                這里給出了C++代碼的實現。關于AVL樹的C語言的實現參見《[AVL樹(Adelson-Velskii-Landis tree)](http://blog.csdn.net/u013074465/article/details/44672567)》 ~~~ //3avl_tree_c++.h #ifndef AVL_TREE_cplusplus_H #define AVL_TREE_cplusplus_H #include <iostream> using namespace std; template <typename Comparable> class AvlTree { public: AvlTree():root(NULL) {} AvlTree(const AvlTree &rhs): root(NULL) { *this = rhs; } ~AvlTree() { makeEmpty(); } const Comparable &findMin() const { if (isEmpty()) cout << "樹為空" << endl; return findMin(root)->element; } const Comparable &findMax() const { if (isEmpty()) cout << "樹為空" << endl; return findMax(root)->element; } bool contains(const Comparable &x) const { return contains(x, root); } bool isEmpty() const { return root == NULL; } void printTree() const { if (isEmpty()) cout << "Empty tree." << endl; else { cout << "先序:"; printTreePreOrder(root); cout << endl << "中序:"; printTreeInOrder(root); cout << endl << "后序:"; printTreePostOrder(root); } cout << endl; } void makeEmpty() { makeEmpty(root); } void insert(const Comparable &x) { insert(x, root); } void remove(const Comparable &x) { remove(x, root); } const AvlTree &operator=(const AvlTree &rhs) { if (this != &rhs) { makeEmpty(); root = clone(rhs.root); } return this; } private: struct AvlNode { Comparable element; AvlNode *left; AvlNode *right; int height; AvlNode(const Comparable &theElement, AvlNode *l, AvlNode *r, int h = 0): element(theElement), left(l), right(r), height(h) {} }; AvlNode *root; void insert(const Comparable &x, AvlNode* &t) { if (t == NULL) t = new AvlNode(x, NULL, NULL); else if (x < t->element) { insert(x, t->left); if (height(t->left) - height(t->right) == 2) if (x < t->left->element) rotateWithLeftChild(t); else doubleWithLeftChild(t); } else if (x > t->element) { insert(x, t->right); if (height(t->right) - height(t->left) == 2) if (t->right->element < x) rotateWithRightChild(t); else doubleWithRightChild(t); } else ; //要往樹中插入已經存在的元素,什么也不做 t->height = max(height(t->left), height(t->right)) + 1; } void remove(const Comparable &x, AvlNode* &t) { if (t == NULL) return; if (x < t->element) { remove(x, t->left); if (height(t->right) - height(t->left) == 2) { if (height(t->right->left) > height(t->right->right)) doubleWithRightChild(t); else rotateWithRightChild(t); } } else if (x > t->element) { if (height(t->left) - height(t->right) == 2) { if (height(t->left->right) > height(t->left->left)) doubleWithLeftChild(t); else rotateWithLeftChild(t); } } else if (t->left && t->right) { //找到要刪除的位置,t有兩個孩子 t->element = findMin(t->right)->element; remove(t->element, t->right); t->height = max(height(t->left), height(t->right)) + 1; } else { AvlNode *oldNode = t; t = (t->left == NULL) ? t->right : t->left; delete oldNode; } if (t != NULL) t->height = max(height(t->left), height(t->right)) + 1; } AvlNode *findMin(AvlNode *t) const { if (t == NULL) return NULL; if (t->left == NULL) return t; return findMin(t->left); } AvlNode *findMax(AvlNode *t) const { //注釋部分為遞歸 //if (t == NULL) // return NULL; //if (t->right == NULL) // return t; //return findMax(t->right); //非遞歸形式 if (t != NULL) while (t->right != NULL) t = t->right; return t; } bool contains(const Comparable &x, AvlNode *t) const { if (t == NULL) return false; else if (x < t->element) return contains(x, t->left); else if (x > t->element) return contains(x, t->right); else return true; } /* //非迭代操作 bool contains(const Comparable &x, AvlNode *t) { while (t != NULL) { if (x < t->element) t = t->left; else if (x > t->element) t = t->right; else return true; } return false; } */ void makeEmpty(AvlNode* &t) { if (t != NULL) { makeEmpty(t->left); makeEmpty(t->right); delete t; } t = NULL; } void printTreePreOrder(AvlNode *t) const { if (t != NULL) { cout << t->element << " "; printTreePreOrder(t->left); printTreePreOrder(t->right); } } void printTreeInOrder(AvlNode * t) const { if (t != NULL) { printTreeInOrder(t->left); cout << t->element << " "; printTreeInOrder(t->right); } } void printTreePostOrder(AvlNode * t) const { if (t != NULL) { printTreePostOrder(t->left); printTreePostOrder(t->right); cout << t->element << " "; } } AvlNode *clone(AvlNode *t) const { if (t == NULL) return NULL; return new AvlNode(t->element, clone(t->left), clone(t->right)); } int height(AvlNode *t) const { return t == NULL ? -1 : t->height; } int max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; } //單旋轉,左-左 void rotateWithLeftChild(AvlNode * & k2) { AvlNode *k1 = k2->left; k2->left = k1->right; k1->right = k2; k2->height = max(height(k2->left), height(k2->right)) + 1; k1->height = max(height(k1->left), height(k1->right)) + 1; k2 = k1; } //單旋轉,右-右 void rotateWithRightChild(AvlNode * & k1) { AvlNode *k2 = k1->right; k1->right = k2->left; k2->left = k1; k1->height = max(height(k1->left), height(k1->right)) + 1; k2->height = max(height(k2->left), height(k2->right)) + 1; k1 = k2; } //雙旋轉,左-右 void doubleWithLeftChild(AvlNode * & k3) { rotateWithRightChild(k3->left); rotateWithLeftChild(k3); } //雙旋轉,右-左 void doubleWithRightChild(AvlNode * & k3) { rotateWithLeftChild(k3->right); rotateWithRightChild(k3); } }; #endif ~~~ 測試代碼: ~~~ #include "3avl_tree_c++.h" int main() { AvlTree<int> tree; int i = 0; while (i != 9) { tree.insert(i++); } tree.printTree(); cout << endl << "刪除11但11不存在:" << endl; tree.remove(11); tree.printTree(); cout << endl << "刪除 5和8:" << endl; tree.remove(5); tree.remove(8); tree.printTree(); return 0; } ~~~ ![](https://box.kancloud.cn/2016-06-07_575683a4e1142.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看