0

After tackling with this function for almost the whole day, I still cannot figure out why this code isn't working. My BTNode has three pointers(left, right, and parent). I went through this code on paper and it seems to work fine, but it does not work fine during execution. I am getting error often and I believe that one of my nodes is not pointing to the right object. Please help =). Thanks

void BTree::RotateLeft(int nodeLabel)
{
	BTNode *k1 = GetNode(nodeLabel); // GetNode() returns a pointer to the node to rotate
	bool isRoot = false, rightparent = false;
	if(k1 != NULL)
	{
		BTNode *temp = k1;
		if(temp->parent != NULL)
		{
			temp = temp->m_parent;
			if(temp->label == temp->left->label)
				rightparent = true;	
		}	//find which side parent is at
		else
			isRoot = true;	//rotate around root

		BTNode *k2 = k1->right;
		k1->right = k2->left;
		k2->left->parent = k1; //k2->parent;
		k2->left = k1;
		k2->parent = k1->parent;
		k1->parent = k2;
		if(k2->parent)
		{
			if(rightparent)
				k2->parent->left = k2;
			else
				k2->parent->right = k2;
		}
		if(isRoot)
			root = k2;		//set new root
	} //rotate left
}
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by red999
0

Heh, finally figured it out after I looked through the internet for an algorithm to print out my tree in level orders.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.