There is a threaded bst class program. I want to have a delete function for this class. every time I tried to write, not work. how can I have a delete func as simple as other funcs.
I googled a lot but all codes are written with structs not with class. is there any code of class. (school project)
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
class node
{
public:
friend class tree;
private:
node* left;
node* right;
int info;
int rthread;
};
class tree
{
public:
tree();
node* getroot() {return root;}
void insert(int num);
void inorder(node *s);
private:
node *root;
};
int main()
{
tree thread;
int num;
while(1)
{
cout << "Enter number: ";
cin >> num;
if(!num)
break;
thread.insert(num);
}
if(thread.getroot())
thread.inorder(thread.getroot());
getch();
return 0;
}
tree::tree()
{
root = NULL;
}
void tree::insert(int num)
{
node *q, *p, *r;
q = new node;
q -> left = q -> right = NULL;
q -> info = num;
q -> rthread = 1;
if(root == NULL)
root = q;
else
{
p = root;
while(p != NULL)
{
if(q -> info > p -> info)
{
if(p -> rthread)
{
p -> rthread = 0;
r = p -> right;
p -> right = q;
q -> left = NULL;
q -> right = r;
q -> rthread = 1;
break;
}
else
p = p-> right;
}
else
{
if(p -> left != NULL)
p = p -> left;
else
{
p -> left = q;
q -> left = NULL;
q -> right = p;
q -> rthread = 1;
break;
}
}
}
}
}
void tree::inorder(node *s)
{
node *p, *q;
cout << "tree: \n";
p = s;
do
{
q = NULL;
while(p != NULL)
{
q = p;
p = p -> left;
}
if(q != NULL)
{
cout << q -> info << " , ";
p = q -> right;
while(q -> rthread && p != NULL)
{
cout << p -> info << " , ";
q = p;
p = p -> right;
}
}
}while(q != NULL);
}