Hi I having problem when I try to implement my delete tree function in my Bst class. This is what i have implemented. Currently only have the insert and deletetree functions.
struct nodeType
{
int data;
nodeType * LeftLink;
nodeType * RightLink;
};
class Bst
{
public:
/**
* Default constructor
*/
Bst();
~Bst();
bool isEmpty() const;
void Insert(int insertItem);
void Destroy(nodeType* &p);
void DeleteTree();
bool Search(int searchItem);
void inOrderTraversal() const;
void preOrderTraversal() const;
void postOrderTraversal() const;
void inOrderTraversal(void(*visit) (int& item)) const;
private:
nodeType * root;
void inOrder(nodeType *p, void(*visit) (int &item)) const;
};
Bst::Bst()
{
root = nullptr;
}
Bst::~Bst()
{
Destroy(root);
}
bool Bst::isEmpty() const
{
return (root == nullptr);
}
void Bst::Insert(int insertItem)
{
nodeType* NewNode = new nodeType;
nodeType* trailCurrent = nullptr;
nodeType* current;
NewNode->data = insertItem;
NewNode->LeftLink = NULL;
NewNode->RightLink = NULL;
if(root == NULL)
{
root = NewNode;
}
else
{
current = root;
while(current != nullptr)
{
trailCurrent = current;
if(insertItem == current->data)
{
cout << "item already inserted, No duplicates allowed." << endl;
return;
}
else if(insertItem > current->data)
{
current = current->RightLink;
}
else
{
current = current->LeftLink;
}
}//endwhile
if(insertItem > trailCurrent->data)
{
trailCurrent->RightLink = NewNode;
cout << NewNode->data << " Inserted" << endl;
}
else
{
trailCurrent->LeftLink = NewNode;
cout << NewNode->data << " Inserted" << endl;
}
}
}
//Delete tree function
void Bst::Destroy(nodeType * &p)
{
if(p != NULL)
{
Destroy(p->LeftLink);
Destroy(p->RightLink);
delete p;
p = NULL;
}
}
void Bst::DeleteTree()
{
Destroy(root);
}
main
int main()
{
Bst intTree;
intTree.Insert(5);
intTree.Insert(3);
intTree.Insert(7);
intTree.Insert(10);
intTree.DeleteTree();
cout << "is tree empty: " << intTree.isEmpty() << endl;
}
When i try to call the DeleteTree function my programs ends without printing out the "is tree empty:" line and ends with Process returned -1073741571 (0xC00000FD).
Can anyone figure out what is happening?
EDIT::
Thanks for all the help! I have changed the insert function as it looks like I was doing the inserting wrongly. I have updated my code to the corrected version.
Yet again thanks for the help.
Please edit as follows:
while(current != nullptr)
{
trailCurrent = current;
if(current->data == insertItem)
{
cout << "item already inserted, No duplicates allowed." << endl;
return;
}
else if(current->data > insertItem)
{
current = current->RightLink;
}
else
{
current = current->LeftLink;
}
}
if(trailCurrent->data > insertItem)
{
trailCurrent->LeftLink = NewNode;
}
else
trailCurrent->RightLink = NewNode;
The problem is not with your DeleteTree method. Instead the Insert method is inserting same node at multiple places which is causing issue of dangling pointers because when two or more pointers point to single node and one is deleted then the other is pointing to a deleted node and that causes the crash when we try to access it.
Here is a simplified version of insert. This fixes the issue, although I am not sure if that is your intended implementation requirement.
class Bst
{
public:
Bst();
bool isEmpty() const;
nodeType* Insert(int insertItem, nodeType *tmpRoot=NULL);
void Destroy(nodeType*& p);
void DeleteTree();
bool Search(int searchItem);
void inOrderTraversal() const;
void preOrderTraversal() const;
void postOrderTraversal() const;
private:
nodeType* root;
};
nodeType* Bst::Insert(int insertItem, nodeType* tmpRoot)
{
nodeType* NewNode = new nodeType;
NewNode->data = insertItem;
NewNode->LeftLink = NewNode->RightLink = nullptr;
if (tmpRoot == nullptr)
{
tmpRoot = NewNode;
if (root == nullptr)
root = tmpRoot;
}
else
{
if (tmpRoot->data == insertItem)
{
cout << "item already inserted, No duplicates allowed." << endl;
return tmpRoot;
}
else if (tmpRoot->data > insertItem)
{
tmpRoot->LeftLink = Insert(insertItem, tmpRoot->LeftLink);
}
else
{
tmpRoot->RightLink = Insert(insertItem, tmpRoot->RightLink);
}
}
return tmpRoot;
}
After updating this code it should work just fine.
Related
The following is a new programmer's attempt at a Queue. It seg faults in the Push() function, when I try to print the data in the first node. Looks like front_ptr is not actually getting set in head_insert. What am I doing wrong here, is this a completely wrong approach?
Thanks.
#include <cstdlib>
#include <iostream>
using namespace std;
class node {
public:
typedef double data_type;
node(data_type init_data = 0, node * init_next = NULL) {
data = init_data;
next_node = init_next;
}
void set_data(data_type new_data) { data = new_data; }
void set_next(node * new_next) { next_node = new_next; }
data_type get_data() { return data; }
node * get_next() { return next_node; }
private:
data_type data;
node * next_node;
};
void head_insert(node::data_type val, node* head_ptr) {
node* insert_ptr = new node(val, head_ptr);
head_ptr = insert_ptr;
}
void list_insert(node::data_type val, node* prev_ptr) {
node* insert_ptr = new node(val, prev_ptr->get_next());
prev_ptr->set_next(insert_ptr);
}
void head_remove(node* head_ptr) {
node* remove_ptr = head_ptr;
head_ptr = head_ptr->get_next();
delete remove_ptr;
}
void list_remove(node * prev_ptr) {
node* remove_ptr = prev_ptr->get_next();
prev_ptr->set_next(remove_ptr->get_next());
delete remove_ptr;
}
void list_clear(node* head_ptr) {
while (head_ptr != NULL) {
head_remove(head_ptr);
}
}
class queue {
public:
queue() {
size = 0;
front_ptr = NULL;
rear_ptr = NULL;
}
//~queue() {}
bool empty() { return (size == 0);}
void push(node::data_type val) {
if (empty()) {
head_insert(val, front_ptr);
cout << "Here: " << front_ptr->get_data() << endl;
rear_ptr = front_ptr;
}
else {
list_insert(val, rear_ptr);
}
size++;
}
void pop() {
if (!empty()) {
head_remove(front_ptr);
size--;
}
}
private:
node* front_ptr;
node* rear_ptr;
int size;
};
int main() {
cout << "START" << endl;
double testVal = 1;
queue* qList = new queue();
qList->push(testVal);
cout << "END" << endl;
return 0;
}
Any help is greatly appreciated.
Your front_ptr remains null pointer in push, because head_insert accepts it by value. Dereferencing null pointer then crashes the program. Make parameters that you want to be modified by a function reference parameters like void head_insert(node::data_type val, node*& head_ptr).
Also, you can avoid crash of null pointer dereferencing by checking it before, for example like that:
cout << "Here: " << (front_ptr ? front_ptr->get_data() : 0./0.) << endl;
So I am attempting to create a binary search tree that stores an ID (T value) an age (int age) and a name (string) per each "bubble" located within the tree and is sorted by the ID.
For some reason, I cannot get my code to correctly store all 3 values as 1 structure per node. Is there something I am doing wrong?
Here's my code.
#ifndef _TREE_GUARD
#define _TREE_GUARD 1
#include <iostream>
#include <math.h>
using namespace std;
template <class T>
struct Node {
T value;
int age;
string name;
Node *left;
Node *right;
Node(T val) {
this->value = val;
}
Node(T val, int age, string name, Node<T> left, Node<T> right) {
this->value = val;
this->age = age;
this->name = name;
this->left = left;
this->right = right;
}
};
template <class T>
class BST {
private:
Node<T> *root;
void addHelper(Node<T> *root, T val) {
if (root->value > val) {
if (!root->left) {
root->left = new Node<T>(val);
}
else {
addHelper(root->left, val);
}
}
else {
if (!root->right) {
root->right = new Node<T>(val);
}
else {
addHelper(root->right, val);
}
}
}
void printHelper(Node<T> *root) {
if (!root) return;
printHelper(root->left);
cout << root->value << ' ';
cout << root->age << ' '; // ADDED
cout << root->name << ' '; //ADDED
printHelper(root->right);
}
int nodesCountHelper(Node<T> *root) {
if (!root) return 0;
else return 1 + nodesCountHelper(root->left) + nodesCountHelper(root->right);
}
int heightHelper(Node<T> *root) {
if (!root) return 0;
else return 1 + max(heightHelper(root->left), heightHelper(root->right));
}
void printMaxPathHelper(Node<T> *root) {
if (!root) return;
cout << root->value << ' ';
if (heightHelper(root->left) > heightHelper(root->right)) {
printMaxPathHelper(root->left);
}
else {
printMaxPathHelper(root->right);
}
}
bool deleteValueHelper(Node<T>* parent, Node<T>* current, T value) {
if (!current) return false;
if (current->value == value) {
if (current->left == NULL || current->right == NULL) {
Node<T>* temp = current->left;
if (current->right) temp = current->right;
if (parent) {
if (parent->left == current) {
parent->left = temp;
}
else {
parent->right = temp;
}
}
else {
this->root = temp;
}
}
else {
Node<T>* validSubs = current->right;
while (validSubs->left) {
validSubs = validSubs->left;
}
T temp = current->value;
current->value = validSubs->value;
validSubs->value = temp;
return deleteValueHelper(current, current->right, temp);
}
delete current;
return true;
}
return deleteValueHelper(current, current->left, value) ||
deleteValueHelper(current, current->right, value);
}
public:
void insert(T val) {
if (root) {
this->addHelper(root, val);
}
else {
root = new Node<T>(val);
}
}
void print() {
printHelper(this->root);
}
int nodesCount() {
return nodesCountHelper(root);
}
int height() {
return heightHelper(this->root);
}
void printMaxPath() {
printMaxPathHelper(this->root);
}
bool Delete(T value) {
return this->deleteValueHelper(NULL, this->root, value);
}
};
#endif
I created the struct using these 3 values, and a pointer to a right and left node, however, all my other functions don't seem to work with the age and name values, only the ID. This is most problamatic in my add and print function.
I'm relatively new to C++, so any help would be appreciated.
EDIT: I'm assuming my problem lies around here somewhere. The code runs just fine, but it doesn't add the age and name to the string, and I can't properly print these values, just the ID
Node<T> *root;
void addHelper(Node<T> *root, T val) {
if (root->value > val) {
if (!root->left) {
root->left = new Node<T>(val);
}
else {
addHelper(root->left, val);**
}
}
else {
if (!root->right) {
root->right = new Node<T>(val);
}
else {
addHelper(root->right, val);
}
}
}
and here
void printHelper(Node<T> *root) {
if (!root) return;
printHelper(root->left);
cout << root->value << ' ';
cout << root->age << ' '; // ADDED
cout << root->name << ' '; //ADDED
printHelper(root->right);
}
Thanks in advance!
root->left = new Node(val);
root->right = new Node(val);
It seems that you create a new node and only give it a "val", no age, no name etc, so it only has the value.
edit: I never used "Template" before, so it took me a while to figure it out. Anyways...
Solution:
When you insert a node, you need to copy all the information, not just the "value", otherwise you'll only get the "value".
void addHelper(Node<T> *root, Node<T>* n) {
if (root->value > n->value) {
if (!root->left) {
root->left = new Node<T>(n->value,n->age,n->name,NULL,NULL);
}
else {
addHelper(root->left, n);
}
}
else {
if (!root->right) {
root->right = new Node<T>(n->value,n->age,n->name,NULL,NULL);
}
else {
addHelper(root->right, n);
}
}
}
Also in "insert":
void insert(Node<T>* n) {
if (root) {
this->addHelper(root, n);
}
else {
root = new Node<T>(n->value,n->age,n->name,NULL,NULL);
}
}
Several other things you may try and see the difference:
I would initialize *root to be NULL, otherwise it may be some random stuff, and sometime I got seg fault because of that.
class BST {
private:
Node<T> *root=NULL;
If the node you're about to insert has the same "value" with an already existing node, what do you do? Your program will just add it to the right. I don't know if that's what you want, but that's what it will do.
I tested with this and it worked. Hope it will work for you, too.
int main(){
BST<int> tree;
Node<int> node1(1,10,"test1",NULL,NULL);
Node<int> node2(5,50,"test2",NULL,NULL);
Node<int> node3(3,30,"test3",NULL,NULL);
Node<int> node4(3,30,"test4",NULL,NULL);
Node<int> node5(3,30,"test5",NULL,NULL);
Node<int> node6(8,80,"test5",NULL,NULL);
tree.insert(&node1);
tree.insert(&node2);
tree.insert(&node3);
tree.insert(&node4);
tree.insert(&node5);
tree.insert(&node6);
tree.print();
cout<<endl;
return 0;
}
output:
1 10 test1 3 30 test3 3 30 test4 3 30 test5 5 50 test2 8 80 test5
I have been trying to implement binary search tree using classes. Every time I try to compile and run the program, the program ends. I have tried many things like making the *root public to access it in main so I can update the root, but somehow it becomes null every time.
Help will be appreciated.
This is for my university project.
#include <iostream>
using namespace std;
class tree;
class Node {
friend class tree;
private:
Node *lchild,*rchild;
int data;
public:
Node (int x) {
data = x;
lchild = rchild = NULL;
}
};
class tree {
protected:
Node* root;
void inorder(const Node* root)const;
public:
tree () {
root = NULL;
}
bool insert(int item);
void inorder() const {inorder(root);};
Node* getroot() {
return root;
}
};
bool tree :: insert(int item) {
if (root == NULL) {
Node *temp = new Node(item);
root = temp;
return (bool) root;
}
if (item < root -> data) {
insert(item);
}
if (item > root -> data) {
insert(item);
}
else if (item == root -> data) {
cout<<"Duplicate";
exit (0);
}
return (bool) root;
}
void tree :: inorder(const Node *root)const {
if (root != NULL) {
inorder(root -> lchild);
cout<<root -> data;
inorder(root -> rchild);
}
}
int main()
{
tree obj1;
obj1.insert(3);
//obj1.insert(4);
obj1.insert(1);
//obj1.insert(5);
obj1.inorder();
}
/* Program to implement Binary Search Tree in c++ using classes and objects */
#include<iostream>
#include<stdlib.h>
#include<cstdlib>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
class BinaryTree {
private:
struct Node* root;
public:
BinaryTree() {
root = NULL;
}
Node* createNode(int);
Node* insertNode(Node*, int);
Node* deleteNode(Node*, int);
void inOrder(Node*);
void preOrder(Node*);
void postOrder(Node*);
Node* findMinimum(Node*);
/* accessor function helps to
get the root node in main function
because root is private data member direct access is not possible */
Node* getRoot() {
return root;
}
/* mutator method helps to update root ptr after insertion
root is not directly updatable in the main because its private data member */
void setRoot(Node* ptr) {
root = ptr;
}
};
/* Helper function to create a new node in each function call of insertNode */
Node* BinaryTree :: createNode(int n) {
Node* newNode = new struct Node();
newNode->data = n;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
/* Helps to get inorder predessor to delete the node from tree */
Node* BinaryTree :: findMinimum(Node* rootPtr) {
while(rootPtr->left != NULL) {
rootPtr = rootPtr->left;
}
return rootPtr;
}
/* insertion of the Node */
Node* BinaryTree :: insertNode(Node* rootPtr, int n) {
if(rootPtr == NULL) {
return createNode(n);
}
if(n < rootPtr->data) {
rootPtr->left = insertNode(rootPtr->left, n);
}
if(n > rootPtr->data) {
rootPtr->right = insertNode(rootPtr->right, n);
}
return rootPtr;
}
/* function to delete the Node */
Node* BinaryTree :: deleteNode(Node* rootPtr, int n) {
if(rootPtr == NULL) {
cout<<"Node to be deleted is not present.!"<<endl;
return rootPtr;
}
else if(n < rootPtr->data) {
rootPtr->left = deleteNode(rootPtr->left, n);
} else if(n > rootPtr->data) {
rootPtr->right = deleteNode(rootPtr->right, n);
} else {
if(rootPtr->left == NULL && rootPtr->right == NULL) {
delete rootPtr;
rootPtr = NULL;
}
else if(root->left == NULL) {
struct Node* temp = rootPtr;
rootPtr = rootPtr->right;
delete temp;
}
else if(rootPtr->right == NULL) {
struct Node* temp = rootPtr;
rootPtr = rootPtr->left;
delete temp;
} else {
Node* temp = findMinimum(rootPtr->right);
rootPtr->data = temp->data;
rootPtr->left = deleteNode(rootPtr->right, temp->data);
}
}
return rootPtr;
}
/* all traversal technique */
void BinaryTree :: inOrder(Node* root) {
if(root == NULL) {
return;
}
inOrder(root->left);
cout<<root->data<<"\t";
inOrder(root->right);
}
void BinaryTree :: preOrder(Node* root) {
if(root == NULL) return;
cout<<root->data<<"\t";
preOrder(root->left);
preOrder(root->right);
}
void BinaryTree :: postOrder(Node* root) {
if(root == NULL) return;
postOrder(root->left);
postOrder(root->right);
cout<<root->data<<"\t";
}
int main() {
BinaryTree l1;
int ch, ele, res;
Node* ptr;
do {
cout<<"1 - Insert Node\n";
cout<<"2 - IN-ORDER Traversal\n";
cout<<"3 - PRE-ORDER Traversal\n";
cout<<"4 - POST-ORDER Traversal\n";
cout<<"Enter choice\n";
cin>>ch;
switch(ch) {
case 1:
cout<<"Entre element to insert to the List\n";
cin>>ele;
/* calling insertNode function by passing root ptr to the function,
root ptr can be obtained by accessor function getRoot() */
ptr = l1.insertNode(l1.getRoot(), ele);
/* updating the root ptr*/
l1.setRoot(ptr);
break;
case 2:
cout<<"---IN-ORDER TRAVERSAL---"<<endl;
l1.inOrder(l1.getRoot());
cout<<endl;
break;
case 3:
cout<<"---PRE-ORDER TRAVERSAL---"<<endl;
l1.preOrder(l1.getRoot());
cout<<endl;
break;
case 4:
cout<<"---POST-ORDER TRAVERSAL---"<<endl;
l1.postOrder(l1.getRoot());
cout<<endl;
break;
case 5:
cout<<"Enter node to be deleted."<<endl;
cin>>ele;
ptr = l1.deleteNode(l1.getRoot(), ele);
l1.setRoot(ptr);
default: cout<<"Invalid choice"<<endl;
}
} while(ch >=1 && ch <= 5);
return 0;
}
The reason why root gets NULL again and again is that it actually never changes its value to something else than NULL.
Maybe you have introduced this behaviour in your code in the course of fixing some other issues; yet you assign root=NULL in the constructor; afterwards, you assign only obj.root1 = ..., while you return root in getroot() { return root; }. Further, you pass Node *root as parameter in you insert function; Note that this local variable named root hides data member root, such that root->... in these functions will always address the local variable and not the data member.
Before diving around in code that's interface needs a redesign, I'd suggest to adapt the design and then adapt the code; I'm pretty sure the errors will simply go away. I'd suggest to adapt the interface of class tree as follows and write the code around it.
Member function inorder() should be const to indicate that it does not alter the object's state. Note that const-member functions can - in contrast to other non-static member functions - be called on const-objects.
class Node {
friend class tree;
private:
Node *lchild,*rchild;
int data;
public:
Node (int x) {
data = x;
lchild = rchild = NULL;
}
};
class tree {
public:
tree () { root = NULL; }
bool insert(int item) { return insert(item,root); };
void inorder() const { inorder(root);};
protected:
Node* root;
void inorder(const Node* curr) const;
bool insert(int item, Node* curr);
};
bool tree :: insert(int item, Node *currNode) {
if (root == NULL) {
root = new Node(item);
return true;
}
else if (item < currNode->data) {
if (currNode->lchild == NULL) {
currNode->lchild = new Node(item);
return true;
}
else {
return insert(item, currNode->lchild);
}
}
else if (item > currNode->data) {
if (currNode->rchild == NULL) {
currNode->rchild = new Node(item);
return true;
}
else {
return insert(item, currNode->rchild);
}
}
else // item == currNode->data
return false; // duplicate; do not insert
}
The biggest problem with your code are the following lines:
if (item < root -> data) {
insert(item);
}
if (item > root -> data) {
insert(item);
}
Basically you are saying that if the item is larger or smaller than the root data you will call the function again with the same item, you never changed the item and you will basically do this an infinity amount of times.....
I have the following code. It creates a binary tree class. The functions are insert(), pre_order(), post_order(), in_order(). But when I debug it I get zeros values. Also I insert 9 values but only have 7 zeros. Why I did wrong?
#include <iostream>
using namespace std;
//Begin the construction of the BINARY TREE
struct tree_node {
tree_node *left;
tree_node *right;
int data;
};
//Declaring the class
class bst {
tree_node *root; //creating the root of the binary tree
public:
bst() {
root = NULL; //intialize the default construction, set the root to NULL
}
int is_empty() { //check for empty graph
return (root == NULL);
}
//Manipulating the Binary Tree
void insert(int item);
void remove_it(int value); //difficult implementation
//Graph Traversal of Binary Tree
void in_order_trav();
void in_order(tree_node *);
void pre_order_trav();
void pre_order(tree_node *);
void post_order_trav();
void post_order(tree_node *);
};
void bst::insert(int item) {
tree_node *p = new tree_node;
tree_node *parent;
p->left = NULL;
p->right = NULL;
parent = NULL;
if (is_empty()) {
root = p;
}
else {
tree_node *ptr;
ptr = root;
while (ptr != NULL) {
parent = ptr;
if (item > ptr->data)
ptr = ptr->right;
else
ptr = ptr->left;
}
if (item < parent->data)
parent->left = p;
else
parent->right = p;
}
}
/*************In Order Traversal*****************************/
// Begin
void bst::in_order_trav() {
in_order(root);
}
void bst::in_order(tree_node *ptr) {
if (ptr!=NULL) {
in_order(ptr->left);
cout << " " << ptr->data << " ";
in_order(ptr->right);
}
}
// End
/***********************************************************/
/*************Pre Order Traversal*****************************/
// Begin
void bst::pre_order_trav() {
pre_order(root);
}
void bst::pre_order(tree_node *ptr) {
if (ptr!=NULL) {
cout << " " << ptr->data << " ";
pre_order(ptr->left);
pre_order(ptr->right);
}
}
// End
/***********************************************************/
/*************Post Order Traversal*****************************/
// Begin
void bst::post_order_trav() {
post_order(root);
}
void bst::post_order(tree_node *ptr) {
if(ptr!=NULL) {
post_order(ptr->left);
post_order(ptr->right);
cout << " " << ptr->data << " ";
}
}
// End
/***********************************************************/
int main() {
bst bin_tree; //create the Binary Tree
bin_tree.insert(20);
bin_tree.insert(30);
bin_tree.insert(52);
bin_tree.insert(254);
bin_tree.insert(2);
bin_tree.insert(24);
bin_tree.insert(25);
bin_tree.insert(42);
bin_tree.insert(59);
bin_tree.in_order_trav(); //in order traversal
bin_tree.pre_order_trav(); //pre order traversal
bin_tree.post_order_trav(); //post order traversal
}
The node value should be initialized(p->data = item) at function insert() as below
void bst::insert(int item) {
tree_node *p = new tree_node;
tree_node *parent;
p->left = NULL;
p->right = NULL;
p->data = item;
parent = NULL;
... ...
}
Ok the solution is silly! -.-
I forgot to add that line in insert routine!
p->data = item;
I'm a beginner to c++ and am having problems with finding the minimal element of a BST. The BST is implemented in this way:
class Tree{
struct Node {
int Element;
Node *Left, *Right;
Node(int Element) : Element(Element), Left(0), Right(0){}
};
Node *Root;
void InOrder(void(*Action)(int&), Node *Current);
void Destroy(Node *Current);
public:
Tree() : Root(0){}
void Insert(int Element);
void InOrder(void(*Action)(int&)) {InOrder(Action,Root);}
void Destroy() {Destroy(Root);}
};
The InOrder, Destroy and Insert methods are implemented like this:
void Tree::Insert(int Element) {
Node *NewElement = new Node(Element);
if(!Root) Root = NewElement;
else {
Node *Previous, *Current = Root;
while(Current) {
Previous = Current;
if(Element < Current->Element) Current = Current->Left;
else Current = Current->Right;
}
if(Element < Previous->Element) Previous->Left = NewElement;
else Previous->Right = NewElement;
}
}
void Tree::InOrder(void(*Action)(int&),Node *Current) {
if(Current) {
InOrder(Action,Current->Left);
Action(Current->Element);
InOrder(Action,Current->Right);
}
}
void Tree::Destroy(Node *Current) {
if(Current) {
Destroy(Current->Left);
Destroy(Current->Right);
delete Current;
}
}
And the main function and function which I use to print the numbers look like this:
void Print(int &e) {
cout << e << endl;
}
int main() {
Tree t;
while(1) {
int Number;
cout << "Insert number (insert 0 to end): ";
cin >> Number;
if(Number == 0) break;
t.Insert(Number);
}
t.InOrder(Print);
t.Destroy();
getch();
}
As you may noticed, the InOrder method is implemented also, maybe it can be used in some way to help solve my problem... Sorry for my bad English :/
The minimal value would be the first value that calls Action in the above code. Go left as far as you can, and the minimal value you shall find...