Thread: pointer help
View Single Post
Old Apr 2nd, 2006, 9:02 AM   #51
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
When The Dark says you should do:
void insertSpot(int option)
{
	heapNode *newNode = new heapNode(option);
	// Increment counter first as the algorithm needs it
	counter++;

	if (root == NULL)
	{
		root = newNode;
	}
	else
	{

...........................

      int temp = counter;
      for(i = 0; i < 32; i++)
      {
          arrayValue = temp % 2;
          temp = temp / 2;
          ar = arrayValue;
      }
Then you should not do:
void insertSpot(int option)
{
	heapNode *newNode = new heapNode(option);
	int arrayValue;
	bool isOne = false;
	int ar[32];
	int i = 31;
	int temp = counter;
	counter++;

Just following what The Dark says gives me something that almost compiles. You still need to change some things. Please study carefully and do not just try to compile and tell us "my compiler gives errors". Read post #49 to see what to do with parent.
#include <iostream>
using namespace std;

class heapNode
{
private:
	int value;

public:
	heapNode *left;
	heapNode *right;

	heapNode(int newValue)
	{
		value = newValue;
		left = NULL;
		right = NULL;
	}

	int getValue()
	{
		return value;
	}

	heapNode* getRight()
	{
		return right;
	}

	heapNode* getLeft()
	{
		return left;
	}

	void setValue(int newValue)
	{
		value = newValue;
	}

	void setRight(heapNode *newRight)
	{
		right = newRight;
	}

	void setLeft(heapNode *newLeft)
	{
		left = newLeft;
	}
};

class heap
{
	private:
	heapNode *root;
	int counter;

public:
	heap()
	{
		counter = 0;
		root = NULL;
	}

	void realInsert(int ar[], int pos, heapNode *parent, heapNode *newNode)
	{
		if(newNode->left == NULL)
		{
			realInsert(ar, pos-1, parent, newNode);
			if(newNode->left->getValue() < newNode->getValue())
			{
				newNode->setValue(newNode->left->getValue());
				counter++;
			}
		}

		else if(newNode->right == NULL)
		{
			realInsert(ar, pos-1, parent, newNode);
			if(newNode->right->getValue() < newNode->getValue())
			{
				newNode->setValue(newNode->right->getValue());
				counter++;
			}
		}
	}

	void insertSpot(int option)
	{
		heapNode *newNode = new heapNode(option);
		// Increment counter first as the algorithm needs it
		counter++;

		if (root == NULL)
		{
			root = newNode;
		}
		else
		{
			int arrayValue;
			bool isOne = false;
			int ar[32];
			int i = 31;

			int temp = counter;

			for(i = 0; i < 32; i++)
			{
				arrayValue = temp % 2;
				temp = temp / 2;
				ar[i] = arrayValue; // changed from ar to ar[i]
			}

			for(int i = 0; i < 32; i++)
				cout << ar[i] << " ";
			cout << endl;

			while(i >= 0)
			{
				if(ar[i] == 1)
					isOne = true;

				if(isOne = true)
					break;
				i--;
			}
			realInsert(ar, i, parent, newNode); // still needs fixing
		}
	}

	void print()
	{
		printNode(root, 0);
	}

	void printNode(heapNode *node, int level)
	{
		if(node != NULL)
		{
			cout << "Level: " << " Val: " << node->getValue() << "\n";
			printNode(node->getLeft(), level + 1);
			printNode(node->getRight(), level + 1);
		}
	}

	heapNode* getRoot()
	{
		return root;
	}
};

int main()
{
	heap *myHeap;

	myHeap = new heap();

	int option = -1;
	while (option != 0)
	{
		cout << "Please enter a number: ";
		cin >> option;
		myHeap->insertSpot(option);
	}

	myHeap->print();
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates

Last edited by nnxion; Apr 2nd, 2006 at 9:18 AM.
nnxion is offline   Reply With Quote