赞
踩
- template <typename K,typename V>
- struct AVLTreeNode
- {
- K _key;
- V _value;
- int _bf; //平衡因子,只能取值为-1,1,0
- AVLTreeNode<K, V>* _left;
- AVLTreeNode<K, V>* _right;
- AVLTreeNode<K, V>* _parent;
- AVLTreeNode(const K& key, const V& value)
- :_key(key)
- , _value(value)
- , _bf(0)
- , _left(NULL)
- , _right(NULL)
- , _parent(NULL)
- {}
- };
- void _RotateL(Node* &parent)
- {
- //1.将要修改的结点标记起来
- Node* SubR = parent->_right;
- Node* SubRL = SubR->_left;
- Node* pparent = parent->_parent;
- //2.重新链上SubR结点
- SubR->_left = parent;
- SubR->_parent = pparent;
- SubR->_bf = 0;
- //4.重新链上parent结点
- parent->_right = SubRL;
- parent->_parent = SubR;
- parent->_bf = 0;
- //5.改变pparent的指向结点
- if (pparent == NULL)
- _root = SubR;
- else if (pparent->_left == parent)
- pparent->_left = SubR;
- else
- pparent->_right = SubR;
- //4.重新链上SubRL结点
- if (SubRL != NULL)
- SubRL->_parent = parent;
-
- }
- void _RotateR(Node* &parent)
- {
- //1.将要修改的结点标记起来
- Node* SubL = parent->_left;
- Node* SubLR = SubL->_right;
- Node* pparent = parent->_parent;
- //2.重新链上SubL结点
- SubL->_right = parent;
- SubL->_parent = parent->_parent;
- SubL->_bf = 0;
- //3.重新链上parent结点
- parent->_left = SubLR;
- parent->_parent = SubL;
- parent->_bf = 0;
- //4.改变pparent的指向结点
- if (pparent == NULL)
- _root = SubL;
- else if (pparent->_left == parent)
- pparent->_left = SubL;
- else
- pparent->_right = SubL;
- //5.重新链上SubRL结点
- if (SubLR != NULL)
- SubLR->_parent = parent;
- }
- void _RotateLR(Node* &parent)
- {
- //双旋的时候在某些情况下会导致bf发生异常
- Node* SubL = parent->_left;
- Node* SubLR = SubL->_right;
- int bf = SubLR->_bf;
- _RotateL(parent->_left);
- _RotateR(parent);
- if (bf == -1)
- {
- parent->_bf = 1;
- SubL->_bf = 0;
- }
- else if (bf == 1)
- {
- parent->_bf = 0;
- SubL->_bf = -1;
- }
- else
- {
- SubL->_bf = parent->_bf = 0;
- }
- SubLR->_bf = 0;
- }
- void _RotateRL(Node* &parent)
- {
- Node* SubR = parent->_right;
- Node* SubRL = SubR->_left;
- int bf = SubRL->_bf;
- _RotateR(parent->_right);
- _RotateL(parent);
- if (bf == 1)
- {
- parent->_bf = -1;
- SubR->_bf = 0;
- }
- else if (bf == -1)
- {
- parent->_bf = 0;
- SubR->_bf = 1;
- }
- else
- {
- SubR->_bf = parent->_bf = 0;
- }
- SubRL->_bf = 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。