GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Aug-2005, 06:23
enggwaqas enggwaqas is offline
New Member
 
Join Date: May 2004
Posts: 20
enggwaqas is on a distinguished road

Pointers problem


im making a binary tree program but got stuck at insertion funtion, though i have implemented this function using while loop but now im trying to do same with a recursive function....the problem is that in recursive function im passing the pointer of a node but don know y function isnt taking the argument correctly and make pointer = NULL?!? the source code is below:

CPP / C++ / C Code:
#include<iostream>
using namespace std;

class treeNode
{
public:
	treeNode* right;
	treeNode* left;
	int keyValue;
	treeNode():right(NULL),left(NULL)
	{ }
};

class BinaryTree
{
private:
	treeNode* root, *ptr;
	int itr;
public:
	BinaryTree():root(NULL),ptr(NULL) //constructor
	{ itr=1; }

	void insert(int value);               //function to add nodes
	void checkList(treeNode*, treeNode*, int); //recursive func. which isn't working correctly

	void inOrder(treeNode* ptr);
	void traverse()
	{ inOrder(root); }
};
//---------------------------------------------------------
void BinaryTree::inOrder(treeNode* ptr)
{
	if( ptr == NULL )
		return;

	inOrder(ptr->left);
	cout<<ptr->keyValue<<endl;
	inOrder(ptr->right);
}
//---------------------------------------------------------
void BinaryTree::checkList(treeNode* myPtr, treeNode* newNode, int value)
{
             /* when prog call this function, this func make myPtr = NULL */
             /* though it is assigning value to newNode correctly */

	treeNode* previous = myPtr;
	
	if( myPtr != NULL )
	{	
	if( myPtr->keyValue > value )
		checkList(myPtr->left,newNode,value);
	else
		checkList(myPtr->right,newNode,value);
	}
	cout<<"Previous: "<<previous->keyValue<<endl;

	if( value<previous->keyValue )
		previous->left = newNode;
	else
		previous->left = newNode;
}
//---------------------------------------------------------
void BinaryTree::insert(int value)
{
	cout<<"Iteration: "<<itr++<<endl;
	treeNode* newNode = new treeNode;
	newNode->keyValue = value;

	ptr = root;

	if( root == NULL )
	{
		root = newNode;
		return;
	}
	else
		checkList(ptr,newNode,value);
}
//---------------------------------------------------------
void main()
{
	BinaryTree myTree;
	myTree.insert(13);
	myTree.insert(16);
	myTree.insert(15);
	myTree.insert(14);

	myTree.traverse();
}
Last edited by LuciWiz : 09-Aug-2005 at 06:27. Reason: Please insert your C++ code between [c++] & [/c++] tags
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03
pointers to pointers Atomical CPP / C++ Forum 8 30-Jul-2005 21:38
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:01.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.