BST insert problem
May 10, 2010 at 4:10pm UTC
Finally my fist post!
Forgive my newbieness, but i've been searching for an answer and didn't find one yet.
I am trying to implement a recursive binary search tree, and it went well so far, I get no compiling errors, but when I try to add nodes to the tree and then try to print it, I get that it has no nodes.
My header file is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#pragma once
#ifndef __BINTREE_H__
#define __BINTREE_H__
#include <iostream>
using namespace std;
//////NODE IMPLEMENTATION//////
template <class T>
class BSTNode{
public :
BSTNode<T> *left, *right;
T data;
BSTNode(){left=right=0;}
BSTNode(T &d){data=d; left=right=0;}
BSTNode(T &d, BSTNode<T> *l, BSTNode<T> *r){data=d; left=l; right=r;}
};
//////BINARY TREE IMPLEMENTATION//////
template <class T>
class BinTree{
private :
BSTNode<T> *p;
public :
BinTree(){p=0;}
BSTNode<T>* getRoot(){return p;}
BSTNode<T>* search(BSTNode<T> *p,T x){
if (!p) return 0;
else if (x == p->data) return p;
else if (x < p->data) return search(p-> left, x);
else return search(p->right, x);
}
void set(BSTNode<T> *p, T newD){
if (!p) p=new BSTNode<T>(newD);
else if (newD < p->data) set(p->left,newD);
else set(p->right,newD);
}
void inOrder(BSTNode<T> *t){
if (t){
inOrder(t->left);
cout<<t->data;
inOrder(t->right);
}
}
};
#endif
I try to add Nodes:
1 2 3 4 5 6 7 8 9
#include "BinTree.h"
void main(){
BinTree<int > T;
T.set(T.getRoot(),2);
T.set(T.getRoot(),1);
T.set(T.getRoot(),3);
T.inOrder(T.getRoot());
}
But it shows no output...
Thanks in advance,
Giakki
Topic archived. No new replies allowed.