I'm having problem in Binary Search Tree. Print In-Order and adding a node

I'm making my own class of Binary Search Tree. I'm getting no output.

My header file
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
#include <iostream>
#include <string>

using namespace std;

class BinaryTree
{
private:
	class btNode
	{
	public:
		int key;
		btNode *left;
		btNode *right;
	};

	btNode *root;

	void AddLeafPrivate(int _key, btNode *btPtr);
	void printInOrderPrivate(btNode *btPtr);

public:
	BinaryTree();

	btNode *CreateLeaf(int _key);
	void AddLeaf(int _key);
	void printInOrder();

	~BinaryTree();
};


Member function definition file
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "BST.h"

BinaryTree::BinaryTree()
{
	root = NULL;
};

BinaryTree::btNode* BinaryTree::CreateLeaf(int _key)
{
	btNode *temp = new btNode;

	temp->key = _key;
	temp->left = temp->right = NULL;

	return temp;
}

void BinaryTree::AddLeaf(int _key)
{
	AddLeafPrivate(_key, root);
}

void BinaryTree::AddLeafPrivate(int _key, btNode *btPtr)
{
	if (root == NULL)
	{
		CreateLeaf(_key);
	}
	else if (_key < btPtr->key)		//if value is smaller
	{
		if (btPtr->left != NULL)
		{
			AddLeafPrivate(_key, btPtr->left);
		}
		else
		{
			btPtr->left = CreateLeaf(_key);
		}
	}
	else if (_key > btPtr->key)		//if value is greater
	{
		if (btPtr->right != NULL)
		{
			AddLeafPrivate(_key, btPtr->right);
		}
		else
		{
			btPtr->right = CreateLeaf(_key);
		}
	}
	else
	{
		cout << "The key " << _key << " is already present in the tree" << endl;
	}
}

void BinaryTree::printInOrder()
{
	printInOrderPrivate(root);
}

void BinaryTree::printInOrderPrivate(btNode *btPtr)
{
	if (root != NULL)
	{
		if (btPtr->left != NULL)
		{
			printInOrderPrivate(btPtr->left);
		}

		cout << btPtr->key;
		
		if (btPtr->right != NULL)
		{
			printInOrderPrivate(btPtr->right);
		}
	}
	else
	{
		cout << "Tree is empty" << endl;
	}
}

BinaryTree::~BinaryTree()
{
	cout << "Destructor is called" << endl;
}


Main file
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
#include "BST.h"

int main()
{
	int TreeKeys[16] = { 50, 76, 21, 4, 32, 64, 15, 52, 14, 100, 83, 2, 3, 70, 87, 80 };
	
	BinaryTree myTree;

	cout << "Printing the tree In-Order" << endl << "Before adding numbers" << endl;

	myTree.printInOrder();
	cout << endl;

	cout << "Printing the tree In-Order" << endl << "After adding numbers" << endl;

	for (int i = 0; i < 16; i++)
	{
		myTree.AddLeaf(TreeKeys[i]);
	}

	myTree.printInOrder();

	system("pause");
	return 0;
}


I'm not getting any output. Can't seem to understand why
Last edited on
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
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <initializer_list>

struct tree // note: may not be balanced (ergo, not a binary search tree).
{
    tree() = default ;
    tree( std::initializer_list<int> il ) { for( int key : il ) add_leaf(key) ; }
    // copy constructor etc. (rule of five)

    void add_leaf( int key )
    {
        if( root == nullptr ) root = new node{key} ;
        else add_leaf( key, root ) ;
    }

    void print_in_order() const
    {
        std::cout << "[ " ;
        print_in_order(root) ;
        std::cout << "]\n" ;
    }

    private:

        struct node
        {
            int key ;
            node* left = nullptr ;
            node* right = nullptr ;
        };

        node* root = nullptr ;

        void add_leaf( int key, node* p )
        {
            node*& where = key < p->key ? p->left : p->right ; // note: reference
            if( where == nullptr ) where = new node{key} ;
            else add_leaf( key, where ) ;
        }

        void print_in_order( const node* p ) const
        {
            if( p != nullptr )
            {
                print_in_order( p->left ) ;
                std::cout << p->key << ' ' ;
                print_in_order( p->right ) ;
            }
        }
};

int main()
{
    tree t { 50, 76, 21, 4, 32, 64, 15, 52, 14, 100, 83, 2, 3, 70, 87, 80 };
    t.print_in_order() ;

    for( int v : { 59, 19, 79, 99, 49, 29 } ) t.add_leaf(v) ;
    t.print_in_order() ;
}

http://coliru.stacked-crooked.com/a/21c62e7e4865e5f6
Last edited on
Thanks but i managed to find the mistake. It was in the member function definition file line 27. It should've been root = CreateLeaf(_key)
Topic archived. No new replies allowed.