Closed Thread
Results 1 to 2 of 2

Thread: AVL Tree balance problem

  1. #1
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Unhappy AVL Tree balance problem

    Hello I'm new here and I need a little help. I have written and AVL tree class and I think my logic is wrong for balancing the tree after insert or deletion. I think it only computes the height and balance factors for the root and its immediate children instead of the entire tree. Can someone help me? I can provide the full class if needed.

    Code:
    //-------------INSERT NODE----------------//
    
    template<class Ktype, class Vtype> //done
    void AVLTree<Ktype, Vtype>::insertNode(Ktype key, Vtype value){
        AVLNode *p = root, *parent = 0;
    	while(p!=0) {
    		parent = p;
    		if (key < p->key){
    			p = p->left;
    		}
    		else {
    			p = p->right;
    		}
    	}
    
    	if (root == 0){
    		root = new AVLNode(key, value);
    	}
    	else if (key < parent->key) {
    		parent->left = new AVLNode(key, value);
    		parent->left->height = 0;
            findBalance(root);
    	}
    	else {
    		parent->right = new AVLNode(key, value);
    		parent->right->height = 0;
    	}
    	findBalance(root);
    	balanceTree(root);
    };
    
    //--------------DELETE NODE-----------------//
    
    template<class Ktype, class Vtype> // done?
    void AVLTree<Ktype, Vtype>::findAndDelete(Ktype key){
    	AVLNode *node = root, *parent = 0;
    	while (node != 0) {
    		if (node->key == key) {
    			break;
    		}
    		parent = node;
    		if (key < node->key) {
    			node = node->left;
    		}
    		else {
    			node = node->right;
    		}
    	}
    	if (node != 0 && node->key == key) {
    		if (node == root) {
    			deleteNode(root);
    			cout << "Deleted Key: " << key << endl;
    		}
    		else if (parent->left == node) {
    			deleteNode(parent->left);
    			cout << "Deleted Key: " << key << endl;
    		}
    		else {
    			deleteNode(parent->right);
    			cout << "Deleted Key: " << key << endl;
    		}
    	}
    	else if (root != 0) {
    		cout << "Key not found!" << endl;
    	}
    	else {
    		cout << "Tree is empty." << endl;
    	}
    };
    
    template<class Ktype, class Vtype> // done?
    void AVLTree<Ktype, Vtype>::deleteNode(AVLNode*& node){
    	AVLNode *parent, *tmpNode = node;
    	if (node->right == 0) {
    		node = node->left;
    	}
    	else if (node->left == 0) {
    		node = node->right;
    	}
    	else {
    		tmpNode = node->left;
    		parent = node;
    		while (tmpNode->right != 0) {
    			parent = tmpNode;
    			tmpNode = tmpNode->right;
    		}
    		node->key = tmpNode->key;
    		if (parent == node) {
    			parent->left = tmpNode->left;
    		}
    		else {
    			parent->right = tmpNode->left;
    		}
    	}
    	delete tmpNode;
            findBalance(root);
            balanceTree(root);
    };
    
    //----------------BALANCING--------------------//
    
    template<class Ktype, class Vtype> //done
    void AVLTree<Ktype, Vtype>::findBalance(AVLNode* node){
        int leftHeight, rightHeight;	
        if (node) {
    		if (node->left){
    			leftHeight = node->left->height;
    		}
    		else {
    			leftHeight = 0;
    		}
    		if (node->right) {
    			rightHeight = node->right->height;
    		}
    		else {
    			rightHeight = 0;
    		}
    	}
    
    	if (leftHeight > rightHeight){
    		node->height = 1 + leftHeight;
    	}
    	else {
    		node->height = 1 + rightHeight;
    	}
    
    	node->bfactor = rightHeight - leftHeight;
    };
    
    template<class Ktype, class Vtype> //done
    void AVLTree<Ktype, Vtype>::balanceTree(AVLNode*& node){
    	if (node->bfactor > 1){
    		rightBalance(node);
    	}
    	else if (node->bfactor < -1){
    		leftBalance(node);
    	}
    };
    
    template<class Ktype, class Vtype> // done
    void AVLTree<Ktype, Vtype>::rightBalance(AVLNode*& node){
    	if (node->right)
    	{
    		if (node->right->bfactor > 0) leftRotate(node);
    		else if (node->right->bfactor < 0)
    		{
    			rightRotate(node->right);
    			leftRotate(node);
    		}
    	}
    };
    
    template<class Ktype, class Vtype> // done
    void AVLTree<Ktype, Vtype>::leftBalance(AVLNode*& node){
    	if (node->left)
    	{
    		if (node->right->bfactor > 0) rightRotate(node);
    		else if (node->right->bfactor < 0)
    		{
    			leftRotate(node->right);
    			rightRotate(node);
    		}
    	}
    };
    
    template<class Ktype, class Vtype>
    void AVLTree<Ktype, Vtype>::leftRotate(AVLNode*& node){
    	AVLNode *tmpNode = node;
    	node = node->left;
    	tmpNode->left = node->right;
    	node->right = tmpNode;
    	findBalance(node->left);
    	findBalance(node->right);
    	findBalance(node);
    };
    
    template<class Ktype, class Vtype>
    void AVLTree<Ktype, Vtype>::rightRotate(AVLNode*& node)
    {
    	AVLNode* tmpNode = node;
    	node = node->left;
    	tmpNode->left = node->right;
    	node->right = tmpNode;
    
    	findBalance(node->left);
    	findBalance(node->right);
    	findBalance(node);
    };

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: AVL Tree balance problem

    I'm a little bit confused: what does key represent? When dealing with an AVLTree, I would only have one template parameter for the value to be stored. (I know, I need to get my AVLTree tutorial completed, but I'm lazy.)

    Your problem is that you aren't remeasuring the heights after an insert/delete. You may want to create a linked list of nodes as you perform an insert/delete so that you can work back up the path you took to remeasure the heights and determine if you need to rebalance at a node (possibly not root for deletes).
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. problem with a binary search tree
    By hadas_zr in forum C and C++
    Replies: 3
    Last Post: 07-14-2011, 07:43 AM
  2. C++ - Problem with a binary tree program; need help
    By Ratchet2246 in forum C and C++
    Replies: 4
    Last Post: 07-15-2008, 12:32 PM
  3. BSP Tree Problem
    By bananamilk in forum C# Programming
    Replies: 0
    Last Post: 06-26-2008, 01:28 PM
  4. Real Tree v1.0 - 3D tree rendering in Visual Basic
    By Kernel in forum Software Development Tools
    Replies: 0
    Last Post: 09-25-2006, 03:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts