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
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.