implement a AVL tree use c++ (Non_Recursive),write by baihacker

来源:互联网 发布:聚美 乐蜂 知我 排名 编辑:程序博客网 时间:2024/06/06 04:27
  1. //by baihacker 
  2. //link:  http://blog.csdn.net/baihacker/archive/2008/08/20/2803385.aspx
  3. #define BALANCE_CHECK 
  4. #ifndef NULL 
  5. #define NULL 0 
  6. #endif 
  7. /* 
  8.     Default Value Control 
  9. */ 
  10. template<typename Type> 
  11. struct DefaultValue 
  12. {static Type Value;}; 
  13. template<typename Type> 
  14. Type DefaultValue<Type>::Value; 
  15. #define DECLARE_DEFAULT_VALUE(T, value) template<>/ 
  16. struct DefaultValue<T>{static T Value;};/ 
  17. template<>/ 
  18. T DefaultValue<T>::Value = value; 
  19. DECLARE_DEFAULT_VALUE(int, 0) 
  20. DECLARE_DEFAULT_VALUE(double, 0) 
  21. /* 
  22.     Node 
  23.         if BALANCE_CHECK is defined, LHeight, RHeight and Height are used; 
  24. */ 
  25. template<typename Type> 
  26. struct Node 
  27.     Type    Data; 
  28.     Node*   LChild, *RChild; 
  29.     int     BalanceFactor; 
  30.      
  31. #ifdef BALANCE_CHECK 
  32. int     LHeight, RHeight, Height; 
  33. #endif 
  34.      
  35.     Node(const Type& _Data = DefaultValue<Type>::Value, 
  36.         Node* _LChild = NULL, 
  37.         Node* _RChild = NULL, 
  38.         int   _BalanceFactor = 0): 
  39.         Data(_Data),  
  40.         LChild(_LChild),  
  41.         RChild(_RChild), 
  42.         BalanceFactor(_BalanceFactor) 
  43.     {} 
  44. }; 
  45. /* 
  46.     AVLTree 
  47.         if BALANCE_CHECK is defined, you can check the balance; 
  48. */ 
  49. template<typename Type> 
  50. class AVLTree 
  51. public
  52.      
  53.         typedef Type              DataType; 
  54.         typedef Node<Type>        NodeType; 
  55.         typedef NodeType*         pNodeType; 
  56.         typedef DefaultValue<Type> DefaultValueType; 
  57.          
  58.         enum {LEFT, RIGHT}; 
  59.          
  60. public
  61.      
  62.         AVLTree(): head(new NodeType) {} 
  63.         ~AVLTree() {Destroy();} 
  64.          
  65. public
  66.      
  67.         int     Insert(const Type& it); 
  68.         int     Delete(const Type& it); 
  69.         int     GetMin(Type& it); 
  70.         int      GetMax(Type& it); 
  71. public
  72.          
  73.         template<typename OP> 
  74.         void    PreOrder(OP); 
  75.          
  76.         template<typename OP> 
  77.         void    InOrder(OP); 
  78.          
  79.     template<typename OP> 
  80.         void    PreInOrder(int type, OP); 
  81.          
  82. private
  83.         inline  void        ReplaceChild(pNodeType  Parent, pNodeType Child, pNodeType NewChild); 
  84.          
  85.         inline  void        RRotateFactorI(pNodeType Parent, pNodeType Tree); 
  86.         inline  void        RRotateFactorD(pNodeType Parent, pNodeType Tree); 
  87.         inline  pNodeType   RRotate(pNodeType Parent, pNodeType Tree); 
  88.          
  89.         inline  void        LRRotateFactorI(pNodeType Parent, pNodeType Tree); 
  90.         inline  void        LRRotateFactorD(pNodeType Parent, pNodeType Tree); 
  91.         inline  pNodeType   LRRotate(pNodeType Parent, pNodeType Tree); 
  92.          
  93.         inline  void        LRotateFactorI(pNodeType Parent, pNodeType Tree); 
  94.         inline  void        LRotateFactorD(pNodeType Parent, pNodeType Tree); 
  95.         inline  pNodeType   LRotate(pNodeType Parent, pNodeType Tree); 
  96.          
  97.          
  98.         inline  void        RLRotateFactorI(pNodeType Parent, pNodeType Tree); 
  99.         inline  void        RLRotateFactorD(pNodeType Parent, pNodeType Tree); 
  100.         inline  pNodeType   RLRotate(pNodeType Parent, pNodeType Tree); 
  101.          
  102.         void                Destroy(); 
  103.         void                Destroy(pNodeType Tree); 
  104. private
  105.         NodeType* head;  
  106.          
  107.          
  108. #ifdef BALANCE_CHECK 
  109. public
  110.         int     BalanceCheck(); 
  111.         int     BalanceCheck(pNodeType tree); 
  112. #endif 
  113. }; 
  114. template<typename Type> 
  115. int AVLTree<Type>::Insert(const Type& it) 
  116.     pNodeType Parent = head, Curr = head->LChild; 
  117.     pNodeType LastNotBalance = NULL, ParentLastNotBalance = NULL; 
  118.      
  119.     while (Curr) 
  120.     { 
  121.         if (Curr->Data == it) return 0; 
  122.         if (Curr->BalanceFactor)  
  123.             ParentLastNotBalance = Parent, LastNotBalance = Curr; 
  124.         Parent = Curr; 
  125.         Curr = it < Curr->Data ? Curr->LChild : Curr->RChild; 
  126.     } 
  127.      
  128.     pNodeType NewNode = new NodeType(it); 
  129.      
  130.     if (Parent == head) 
  131.     { 
  132.         head->LChild = NewNode; 
  133.         return 1; 
  134.     } 
  135.     else 
  136.     { 
  137.         it < Parent->Data ?  
  138.             Parent->LChild = NewNode : 
  139.             Parent->RChild = NewNode; 
  140.     } 
  141.      
  142.     for (Curr = LastNotBalance ? LastNotBalance : head->LChild; 
  143.         Curr != NewNode;) 
  144.     { 
  145.         it < Curr->Data ? 
  146.             (++Curr->BalanceFactor, Curr = Curr->LChild): 
  147.             (--Curr->BalanceFactor, Curr = Curr->RChild); 
  148.     } 
  149.      
  150.     if (!LastNotBalance || LastNotBalance->BalanceFactor < 2 & 
  151.             LastNotBalance->BalanceFactor > -2) 
  152.         return 1; 
  153.      
  154.     if (it < LastNotBalance->Data) 
  155.     { 
  156.         if (it < LastNotBalance->LChild->Data ) 
  157.         { 
  158.             RRotateFactorI(ParentLastNotBalance, LastNotBalance); 
  159.             ReplaceChild( 
  160.                         ParentLastNotBalance,  
  161.                         LastNotBalance, 
  162.                         RRotate(ParentLastNotBalance, LastNotBalance) 
  163.                         ); 
  164.         } 
  165.         else 
  166.         { 
  167.             LRRotateFactorI(ParentLastNotBalance, LastNotBalance);       
  168.             ReplaceChild( 
  169.                         ParentLastNotBalance,  
  170.                         LastNotBalance, 
  171.                         LRRotate(ParentLastNotBalance, LastNotBalance) 
  172.                         ); 
  173.         } 
  174.     } 
  175.     else 
  176.     { 
  177.         if (it > LastNotBalance->RChild->Data) 
  178.         { 
  179.             LRotateFactorI(ParentLastNotBalance, LastNotBalance); 
  180.             ReplaceChild( 
  181.                         ParentLastNotBalance,  
  182.                         LastNotBalance, 
  183.                         LRotate(ParentLastNotBalance, LastNotBalance) 
  184.                         ); 
  185.         } 
  186.         else 
  187.         { 
  188.              
  189.             RLRotateFactorI(ParentLastNotBalance, LastNotBalance); 
  190.             ReplaceChild( 
  191.                         ParentLastNotBalance,  
  192.                         LastNotBalance, 
  193.                         RLRotate(ParentLastNotBalance, LastNotBalance) 
  194.                         ); 
  195.         } 
  196.     } 
  197.     return 1; 
  198. template<typename Type> 
  199. int AVLTree<Type>::Delete(const Type& it) 
  200.     pNodeType SearchHistory[64]; 
  201.     int       SearchDirection[64]; 
  202.     int       CurrIdx; 
  203.      
  204.     pNodeType Curr = head->LChild; 
  205.     SearchHistory[0] = head; 
  206.     SearchDirection[0] = 0; 
  207.     CurrIdx = 1; 
  208.      
  209.     while (Curr && Curr->Data != it) 
  210.     { 
  211.         SearchHistory[CurrIdx] = Curr; 
  212.         it < Curr->Data ? 
  213.             (Curr = Curr->LChild, SearchDirection[CurrIdx] = LEFT):  
  214.             (Curr = Curr->RChild, SearchDirection[CurrIdx] = RIGHT); 
  215.         ++CurrIdx; 
  216.     } 
  217.      
  218.     if (!Curr) return 0; 
  219.      
  220.     pNodeType ToBeDeleted = Curr; 
  221.     if (Curr->LChild && Curr->RChild) 
  222.     { 
  223.         pNodeType  CurrT = Curr->LChild; 
  224.         SearchHistory[CurrIdx] = Curr; 
  225.         SearchDirection[CurrIdx++] = LEFT; 
  226.         while (CurrT->RChild) 
  227.         { 
  228.             SearchHistory[CurrIdx] = CurrT; 
  229.             SearchDirection[CurrIdx++] = RIGHT; 
  230.             CurrT = CurrT->RChild; 
  231.         } 
  232.         Curr->Data = CurrT->Data; 
  233.         ToBeDeleted = CurrT; 
  234.     } 
  235.      
  236.      
  237.     // There exists at least one child that is NULL 
  238.     { 
  239.         pNodeType Temp =  
  240.             ToBeDeleted->LChild ? 
  241.                 ToBeDeleted->LChild : 
  242.                     (ToBeDeleted->RChild  ? ToBeDeleted->RChild : NULL); 
  243.         delete ToBeDeleted; 
  244.         --CurrIdx;//    Indidate the pointer to the parent of the node to be deleted 
  245.         SearchDirection[CurrIdx] == RIGHT ? 
  246.             SearchHistory[CurrIdx]->RChild = Temp: 
  247.             SearchHistory[CurrIdx]->LChild = Temp; 
  248.     } 
  249.     for (int HeightChanged = 1; HeightChanged && CurrIdx; --CurrIdx) 
  250.     {    
  251.         pNodeType ToBeAdjusted = SearchHistory[CurrIdx]; 
  252.         pNodeType ParentToBeAdjusted = SearchHistory[CurrIdx-1]; 
  253.         int       ChangedChild = SearchDirection[CurrIdx]; 
  254.         if (ToBeAdjusted->BalanceFactor == 0) 
  255.         { 
  256.             ToBeAdjusted->BalanceFactor = ChangedChild == LEFT? -1 : 1; 
  257.             HeightChanged = 0; 
  258.         } 
  259.         else if (ToBeAdjusted->BalanceFactor == -1) 
  260.         { 
  261.             if (ChangedChild == RIGHT) 
  262.             { 
  263.                 ToBeAdjusted->BalanceFactor = 0; 
  264.                 HeightChanged = 1; 
  265.             } 
  266.             else 
  267.             { 
  268.                 switch (ToBeAdjusted->RChild->BalanceFactor) 
  269.                 { 
  270.                 case 0: 
  271.                     HeightChanged = 0; 
  272.                     LRotateFactorD(ParentToBeAdjusted, ToBeAdjusted); 
  273.                     ReplaceChild(ParentToBeAdjusted, ToBeAdjusted, 
  274.                                 LRotate(ParentToBeAdjusted, ToBeAdjusted)); 
  275.                     break
  276.                 case -1: 
  277.                     HeightChanged = 1; 
  278.                     LRotateFactorD(ParentToBeAdjusted, ToBeAdjusted); 
  279.                     ReplaceChild(ParentToBeAdjusted, ToBeAdjusted, 
  280.                                 LRotate(ParentToBeAdjusted, ToBeAdjusted)); 
  281.                     break
  282.                 case 1: 
  283.                     HeightChanged = 1; 
  284.                     RLRotateFactorD(ParentToBeAdjusted, ToBeAdjusted); 
  285.                     ReplaceChild(ParentToBeAdjusted, ToBeAdjusted, 
  286.                                 RLRotate(ParentToBeAdjusted, ToBeAdjusted)); 
  287.                 } 
  288.             } 
  289.         } 
  290.         else 
  291.         { 
  292.             if (ChangedChild == LEFT) 
  293.             { 
  294.                 ToBeAdjusted->BalanceFactor = 0; 
  295.                 HeightChanged = 1; 
  296.             } 
  297.             else 
  298.             { 
  299.                 switch (ToBeAdjusted->LChild->BalanceFactor) 
  300.                 { 
  301.                 case 0: 
  302.                     HeightChanged = 0; 
  303.                     RRotateFactorD(ParentToBeAdjusted, ToBeAdjusted); 
  304.                     ReplaceChild(ParentToBeAdjusted, ToBeAdjusted, 
  305.                                 RRotate(ParentToBeAdjusted, ToBeAdjusted)); 
  306.                     break
  307.                 case 1: 
  308.                     HeightChanged = 1; 
  309.                     RRotateFactorD(ParentToBeAdjusted, ToBeAdjusted); 
  310.                     ReplaceChild(ParentToBeAdjusted, ToBeAdjusted, 
  311.                                 RRotate(ParentToBeAdjusted, ToBeAdjusted)); 
  312.                     break
  313.                 case -1: 
  314.                     HeightChanged = 1; 
  315.                     LRRotateFactorD(ParentToBeAdjusted, ToBeAdjusted); 
  316.                     ReplaceChild(ParentToBeAdjusted, ToBeAdjusted, 
  317.                                 LRRotate(ParentToBeAdjusted, ToBeAdjusted)); 
  318.                 } 
  319.             } 
  320.         }        
  321.     } 
  322.      
  323.     return 1; 
  324. template<typename Type> 
  325. int     AVLTree<Type>::GetMin(Type& it) 
  326.     pNodeType   Curr = head->LChild; 
  327.     if (!Curr) return 0; 
  328.     while (Curr->LChild) Curr = Curr->LChild; 
  329.     it = Curr->Data; 
  330.     return 1; 
  331. }    
  332. template<typename Type> 
  333. int     AVLTree<Type>::GetMax(Type& it) 
  334.     pNodeType   Curr = head->LChild; 
  335.     if (!Curr) return 0; 
  336.     while (Curr->RChild) Curr = Curr->RChild; 
  337.     it = Curr->Data; 
  338.     return 1; 
  339. }     
  340. template<typename Type> 
  341. template<typename OP> 
  342. void AVLTree<Type>::PreOrder(OP ope) 
  343.     PreInOrder(0, ope); 
  344. template<typename Type> 
  345. template<typename OP> 
  346. void AVLTree<Type>::InOrder(OP ope) 
  347.     PreInOrder(1, ope); 
  348. template<typename Type> 
  349. template<typename OP> 
  350. void AVLTree<Type>::PreInOrder(int type, OP ope) 
  351.     pNodeType Curr = head->LChild; 
  352.     pNodeType R = NULL; 
  353.      
  354.     while (true
  355.     { 
  356.         if (Curr == NULL) break
  357.          
  358.         pNodeType q = Curr->LChild; 
  359.         if (q == NULL) 
  360.         { 
  361.             ope(Curr->Data); 
  362.             R = Curr; 
  363.             Curr = Curr->RChild; 
  364.         } 
  365.         else 
  366.         { 
  367.             while (q->RChild != NULL && q->RChild != Curr) 
  368.                 q = q->RChild; 
  369.             if (q->RChild == NULL) 
  370.             { 
  371.                 q->RChild = Curr; 
  372.                 if (type == 0) ope(Curr->Data);; 
  373.                 Curr = Curr->LChild; 
  374.             } 
  375.             else 
  376.             {    
  377.                 R = Curr; 
  378.                 if (type == 1) ope(Curr->Data); 
  379.                 Curr = Curr->RChild; 
  380.                 q->RChild = NULL; 
  381.             } 
  382.              
  383.         } 
  384.     } 
  385. template<typename Type> 
  386. inline  void         
  387. AVLTree<Type>::ReplaceChild(pNodeType  Parent, pNodeType Child, pNodeType NewChild) 
  388.     Parent->LChild == Child ? 
  389.         Parent->LChild = NewChild : 
  390.         Parent->RChild = NewChild ; 
  391. // Right rotate the tree 
  392. template<typename Type> 
  393. inline void  
  394. AVLTree<Type>::RRotateFactorI(pNodeType Parent, pNodeType Tree) 
  395.     Tree->BalanceFactor = 0; 
  396.     Tree->LChild->BalanceFactor = 0; 
  397. template<typename Type> 
  398. inline void 
  399. AVLTree<Type>::RRotateFactorD(pNodeType Parent, pNodeType Tree) 
  400.     pNodeType A = Tree; 
  401.     pNodeType B = Tree->LChild; 
  402.     if (B->BalanceFactor == 0) 
  403.     { 
  404.         A->BalanceFactor = 1; 
  405.         B->BalanceFactor = -1; 
  406.     } 
  407.     else 
  408.     { 
  409.         A->BalanceFactor = 0; 
  410.         B->BalanceFactor = 0; 
  411.     } 
  412. template<typename Type> 
  413. inline typename AVLTree<Type>::pNodeType  
  414. AVLTree<Type>::RRotate(pNodeType Parent, pNodeType Tree) 
  415.     pNodeType A = Tree; 
  416.     pNodeType B = Tree->LChild; 
  417.     A->LChild = B->RChild; 
  418.     B->RChild = A; 
  419.     return B; 
  420. // Left rotate the LChild of the tree and right rotate the tree 
  421. template<typename Type> 
  422. inline void 
  423. AVLTree<Type>::LRRotateFactorI(pNodeType Parent, pNodeType Tree) 
  424.     pNodeType A = Tree; 
  425.     pNodeType B = Tree->LChild; 
  426.     pNodeType C = B->RChild; 
  427.      
  428.     switch (C->BalanceFactor) 
  429.     { 
  430.     case  1: 
  431.                 B->BalanceFactor = 0; 
  432.                 A->BalanceFactor = -1; 
  433.                 C->BalanceFactor = 0; 
  434.                 break
  435.     case -1: 
  436.                 B->BalanceFactor = 1; 
  437.                 A->BalanceFactor = 0; 
  438.                 C->BalanceFactor = 0; 
  439.                 break
  440.     case 0: 
  441.                 B->BalanceFactor = 0; 
  442.                 A->BalanceFactor = 0; 
  443.                 C->BalanceFactor = 0; 
  444.     } 
  445. template<typename Type> 
  446. inline void 
  447. AVLTree<Type>::LRRotateFactorD(pNodeType Parent, pNodeType Tree) 
  448.     pNodeType A = Tree; 
  449.     pNodeType B = Tree->LChild; 
  450.     pNodeType C = B->RChild; 
  451.      
  452.     switch (C->BalanceFactor) 
  453.     { 
  454.     case  -1: 
  455.                 A->BalanceFactor = 0; 
  456.                 B->BalanceFactor = 1; 
  457.                 C->BalanceFactor = 0; 
  458.                 break
  459.     case 0: 
  460.                 A->BalanceFactor = 0; 
  461.                 B->BalanceFactor = 0; 
  462.                 C->BalanceFactor = 0; 
  463.                 break
  464.     case 1: 
  465.                 A->BalanceFactor = -1; 
  466.                 B->BalanceFactor = 0; 
  467.                 C->BalanceFactor = 0; 
  468.     } 
  469. template<typename Type> 
  470. inline typename AVLTree<Type>::pNodeType 
  471. AVLTree<Type>::LRRotate(pNodeType Parent, pNodeType Tree) 
  472.     pNodeType A = Tree; 
  473.     pNodeType B = Tree->LChild; 
  474.     pNodeType C = B->RChild; 
  475.      
  476.     B->RChild = C->LChild; 
  477.     A->LChild = C->RChild; 
  478.     C->LChild = B; 
  479.     C->RChild = A; 
  480.                  
  481.     return C; 
  482. // Left rotate the tree 
  483. template<typename Type> 
  484. inline void 
  485. AVLTree<Type>::LRotateFactorI(pNodeType Parent, pNodeType Tree) 
  486.     Tree->BalanceFactor = 0; 
  487.     Tree->RChild->BalanceFactor = 0; 
  488. template<typename Type> 
  489. inline void 
  490. AVLTree<Type>::LRotateFactorD(pNodeType Parent, pNodeType Tree) 
  491.     pNodeType A = Tree; 
  492.     pNodeType B = Tree->RChild; 
  493.     if (B->BalanceFactor == 0) 
  494.     { 
  495.         A->BalanceFactor = -1; 
  496.         B->BalanceFactor = 1; 
  497.     } 
  498.     else 
  499.     { 
  500.         A->BalanceFactor = 0; 
  501.         B->BalanceFactor = 0; 
  502.     } 
  503. template<typename Type> 
  504. inline typename AVLTree<Type>::pNodeType 
  505. AVLTree<Type>::LRotate(pNodeType Parent, pNodeType Tree) 
  506.     pNodeType A = Tree; 
  507.     pNodeType B = Tree->RChild; 
  508.      
  509.     A->RChild = B->LChild; 
  510.     B->LChild = A; 
  511.      
  512.     return B; 
  513. // Right rotate the RChild of the tree and left rotate the tree 
  514. template<typename Type> 
  515. inline void 
  516. AVLTree<Type>::RLRotateFactorI(pNodeType Parent, pNodeType Tree) 
  517.     pNodeType A = Tree; 
  518.     pNodeType B = Tree->RChild; 
  519.     pNodeType C = B->LChild; 
  520.      
  521.     switch (C->BalanceFactor) 
  522.     { 
  523.     case  1: 
  524.             B->BalanceFactor = -1; 
  525.             A->BalanceFactor = 0; 
  526.             C->BalanceFactor = 0; 
  527.             break
  528.     case -1: 
  529.             B->BalanceFactor = 0; 
  530.             A->BalanceFactor = 1; 
  531.             C->BalanceFactor = 0; 
  532.             break
  533.     case 0: 
  534.             B->BalanceFactor = 0; 
  535.             A->BalanceFactor = 0; 
  536.             C->BalanceFactor = 0; 
  537.     } 
  538. template<typename Type> 
  539. inline void 
  540. AVLTree<Type>::RLRotateFactorD(pNodeType Parent, pNodeType Tree) 
  541.      
  542.     pNodeType A = Tree; 
  543.     pNodeType B = Tree->RChild; 
  544.     pNodeType C = B->LChild; 
  545.      
  546.     switch (C->BalanceFactor) 
  547.     { 
  548.     case  -1: 
  549.                 A->BalanceFactor = 1; 
  550.                 B->BalanceFactor = 0; 
  551.                 C->BalanceFactor = 0; 
  552.                 break
  553.     case 0: 
  554.                 A->BalanceFactor = 0; 
  555.                 B->BalanceFactor = 0; 
  556.                 C->BalanceFactor = 0; 
  557.                 break
  558.     case 1: 
  559.                 A->BalanceFactor = 0; 
  560.                 B->BalanceFactor = -1; 
  561.                 C->BalanceFactor = 0; 
  562.     } 
  563. template<typename Type> 
  564. inline typename AVLTree<Type>::pNodeType 
  565. AVLTree<Type>::RLRotate(pNodeType Parent, pNodeType Tree) 
  566.     pNodeType A = Tree; 
  567.     pNodeType B = Tree->RChild; 
  568.     pNodeType C = B->LChild; 
  569.              
  570.     B->LChild = C->RChild; 
  571.     A->RChild = C->LChild; 
  572.     C->LChild = A; 
  573.     C->RChild = B; 
  574.              
  575.     return C; 
  576. template<typename Type> 
  577. void    AVLTree<Type>::Destroy() 
  578.     if (head) Destroy(head->LChild); 
  579.     head = NULL; 
  580. }     
  581. template<typename Type> 
  582. void    AVLTree<Type>::Destroy(pNodeType Tree) 
  583.     if (!Tree) return
  584.     Destroy(Tree->LChild); 
  585.     Destroy(Tree->RChild); 
  586.     delete Tree; 
  587. #ifdef BALANCE_CHECK 
  588. template<typename Type> 
  589. int AVLTree<Type>::BalanceCheck() 
  590.     return BalanceCheck(head->LChild); 
  591. template<typename Type> 
  592. int AVLTree<Type>::BalanceCheck(pNodeType tree) 
  593.     int LBalance = 1, RBalance = 1; 
  594.     if (tree->LChild) 
  595.     {    
  596.         LBalance = BalanceCheck(tree->LChild); 
  597.         tree->LHeight = tree->LChild->Height; 
  598.     } 
  599.     else 
  600.     { 
  601.         tree->LHeight = 0; 
  602.     } 
  603.     if (tree->RChild) 
  604.     {    
  605.         RBalance = BalanceCheck(tree->RChild); 
  606.         tree->RHeight = tree->RChild->Height; 
  607.     } 
  608.     else 
  609.     { 
  610.         tree->RHeight = 0; 
  611.     } 
  612.      
  613.     tree->Height = max(tree->LHeight, tree->RHeight) + 1; 
  614.     return LBalance && RBalance & 
  615.         tree->BalanceFactor == tree->LHeight - tree->RHeight; 
  616. #endif 
  617. #include <iostream> 
  618. #include <cstdlib> 
  619. #include <ctime> 
  620. using namespace std; 
  621. template<typename T> 
  622. void visit(T i) 
  623. {cout << i << " ";}     
  624. int main() 
  625.     AVLTree<int> it; 
  626.     int m; 
  627.      
  628.     it.Insert(1); 
  629.     for (int i = 0; i < 24; ++i) 
  630.         it.Insert(rand()); 
  631.     it.InOrder(visit<int>); 
  632.     it.GetMax(m); 
  633.     cout << endl << m << endl; 
  634.     it.GetMin(m); 
  635.     cout << m << endl; 
  636.     system("pause"); 
  637.     return 0; 
  638. //write by baihacker
  639. //link:   http://blog.csdn.net/baihacker/archive/2008/08/20/2803385.aspx
原创粉丝点击