AVL TREE insert, delete,rotation,algorithm
2012-05-15 12:06:49 来源:WEB开发网核心提示: .h文件#include <cstdio>class AVLNode{public:AVLNode(int key){this->key = key;left = NULL;right = NULL;balance = 0;}AVLNode *left, *right;int key;/* The
.h文件
#include <cstdio>
class AVLNode{
public:
AVLNode(int key){
this->key = key;
left = NULL;
right = NULL;
balance = 0;
}
AVLNode *left, *right;
int key;
/*
The record key stored on the tree nodes, can be of any comparable type, we use int for simplicity.
Usually the actual record data is also strored on the tree nodes, but we skip it for simplicity, too.
*/
int balance;
/*
balance=(height of right subtree)-(height of left subtree)
balance=0: entirely balanced
balance=1: right heavy, but still balanced
balance=-1: left heavy, but still balanced
|balance|>1: unbalanced
*/
};
class AVLTree
{
public:
AVLTree() {this->root = NULL;}
~AVLTree() {destroy(root);}
AVLNode* insert(int key);
void printTree();
int getHeight() {return rGetHeight(root);};
AVLNode* remove(int key);
//This method removes the node with the given key from the tree
//If such node does not exist, NULL will be returned
//Otherwise, a pointer to the removed node is returned
//You may use existing member functions or define other member functions to complete the task
private:
AVLNode *root;
AVLNode *tmp; //using to point to subroot in findMin function
AVLNode *temp; //using to point to subroot in rRemove function
AVLNode *rInsert(int key, AVLNode* &subRoot, bool &heightChanged);
AVLNode *rRemove(int key, AVLNode* &subroot, bool &heightChanged); //Remove the particular node with key
AVLNode *findMin(AVLNode * &subroot, bool &heightChanged); //Find the leftest node of right subtree
void balance1(AVLNode* &subRoot);//Re-balance for case 1 violation
void balance2(AVLNode* &subRoot);//Re-balance for case 2 violation
void balance3(AVLNode* &subRoot);//Re-balance for case 3 violation
void balance4(AVLNode* &subRoot);//Re-balance for case 4 violation
void balance5(AVLNode* &subRoot);//Re-balance for case 5 violation:left-left-balance
void balance6(AVLNode* &subRoot);//Re-balance for case 6 violation:right-right-balance
void destroy(AVLNode *subRoot);
int rGetHeight(AVLNode *subRoot);
};
//The Queue data structure is used to help print the tree
class QueueNode
{
public:
QueueNode(AVLNode *data, bool isLeading=false) {this->data = data; this->isLeading=isLeading; next=NULL;}
AVLNode *data;
//data is NULL if the node corresponds to a hole in the tree
bool isLeading;
//If the current node is the leading node in a level
QueueNode *next;
};
class Queue
{
public:
Queue() {front = NULL; rear = NULL;}
~Queue();
void enqueue(AVLNode *data, bool isLeading=false);
QueueNode* dequeue();
bool isEmpty() {return !front;}
private:
QueueNode *front, *rear;
};
更多精彩
赞助商链接
