c++ help

JOHNGALT99

Senior member
Mar 26, 2001
431
0
71
if have have these classes and want to insert intergers into the binary tree which function would i call and what would it look like

in other words

how would i declare a new tree in my main program

and what would the function look like to insert some integers in itt?

include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <cassert>

using namespace std;

class TreeNode {
public:
typedef int datatype;

TreeNode(datatype x=0, TreeNode *left=NULL,TreeNode *right=NULL)
{
data=x;
this->left=left;
this->right=right;
};

datatype getData( )
{
return data;
};

TreeNode *getLeft( )
{
return left;
};

TreeNode *getRight( )
{
return right;
};

void setData(datatype x)
{
data=x;

};

void setLeft(TreeNode *ptr)
{
left=ptr;
};

void setRight(TreeNode *ptr)
{
right=ptr;
};

private:
datatype data; // different data type for other apps
TreeNode *left; // the pointer to left child
TreeNode *right; // the pointer to right child

};

class Tree {
public:
typedef int datatype;
Tree(TreeNode *rootPtr=NULL)
{
this->rootPtr=rootPtr;
};

TreeNode *search(datatype x);
bool insert(datatype x);
TreeNode * remove(datatype x);
TreeNode *getRoot()
{
return rootPtr;
};

Tree *getLeftSubtree();
Tree *getRightSubtree();
bool isEmpty(){return rootPtr == NULL;};
private:
TreeNode *rootPtr;

};

bool Tree::insert(datatype x)
{
if (isEmpty())
{
rootPtr = new TreeNode(x);
return true;
}
TreeNode *p=rootPtr;
while (p != NULL )
{
datatype a = p->getData();
if (a == x)
return false; // data is already there
else if (x<a)
{
if (p->getLeft() == NULL )
{ // place to insert
TreeNode *newNodePtr= new TreeNode(x);
p->setLeft(newNodePtr);
return true;
}
else
p=p->getLeft();
}
else
{ // a>a
if (p->getRight() == NULL )
{ // place to insert
TreeNode *newNodePtr= new TreeNode(x);
p->setRight(newNodePtr);
return true;
}
else p=p->getRight();
}
}
};


TreeNode * Tree::search(datatype x)
{
if (isEmpty())
{
return NULL;
}
TreeNode *p=rootPtr;
while (p != NULL)
{
datatype a = p->getData();
if (a == x)
return p;
else if (x<a)
p=p->getLeft();
else
p=p->getRight();

}
return NULL;
};

thanks
 

itachi

Senior member
Aug 17, 2004
390
0
0
...
you'd construct the tree, insert the items using the insert method. the insertion order would be sorted, so the left node will always be less than the current node, and the right node will always be greater.

it's all there in the code. if you still find it difficult, you can run a debugger and trace how each statement is executed.
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
1. Read your textbook
2. Review your notes from appropriate class lectures
3. Attempt the problem yourself
4. Report back with specific questions


This has been a standard "do your own damn homework" copy/paste.

Thank you.
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
Instead of:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>


Instead use:
#include <iostream>
#include <fstream>
#include <cstdlib>


The older .h files are really only there for legacy reasons.

This non-answer to your question is also a bump.