Thread: pointer help
View Single Post
Old Mar 31st, 2006, 1:04 PM   #28
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
wait so just this... I do not get what this is supposed to do? I get that the code gives a value to root and then to the right and left child and it prints it so it works but then how do i add more and make it look like a tree and stuff?
#include <iostream>
using namespace std;

class heapNode
{
	private:
		int value;
		//heapNode *left;
		//heapNode *right;

	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;
		}

}; // end of heapNode class

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

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

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

void realInsert(int ar, int pos, int newValue)
  {
    heapNode *newNode;
    newNode = new heapNode(newValue);   
    if(newNode->left != NULL)           
    {
      realInsert(ar, pos-1, newValue);

      if(newNode->left->getValue() < newNode->getValue())
        newNode->setValue(newNode->left->getValue());
    }
    else if(newNode->right != NULL)
    {
      realInsert(ar, pos-1, newValue);
      if(newNode->right->getValue() < newNode->getValue())
        newNode->setValue(newNode->right->getValue());
    }
  }
};

int main()
{
	heap *myHeap;

	myHeap = new heap();

	int value = 0, left = 0, right =0;

	cout << "Please enter a value for your first heap node: ";
	cin >> value;

	// get the root heapnode, and set the value of the root heapnode
	heapNode *hnRoot = myHeap->getRoot();
	hnRoot->setValue(value);

	cout << "Please enter a value for right child of your first heap node: ";
	cin >> right;
	
	// make root's right child and add it
	heapNode hnRight(0);
	hnRight.setValue(right);
	hnRoot->setRight(&hnRight);

	cout << "Please enter a value for right child of your first heap node: ";
	cin >> left;

	// make root's left child and add it
	heapNode hnLeft(0);
	hnRoot->setLeft(&hnLeft);
	hnLeft.setValue(left);

	// print values
	cout << "Value of root heapnode is: " << hnRoot->getValue() << endl;
	cout << "Value of root's right child is: " << hnRoot->getRight()->getValue() << endl;
	cout << "Value of root's left child is: " << hnRoot->getLeft()->getValue() << endl;

	return 0;
}
cwl157 is offline   Reply With Quote