Binary Search Tree: Insert Function Not Working

I am working on programming a binary search tree.

The problem that I am having is that my root node doesn't ever seem to actually end up pointing to anything. I had my driver perform a series of inserts, and when I went into debugging mode and watched what happened the node was created to add to the tree, but it never actually gets put on the tree. I'm not sure what I did wrong.

Here is my Binary Search Tree class:

Node Struct
1
2
3
4
5
6
7
8
9
10
11
#ifndef NODE
#define NODE

struct node {
	int value;
	node* leftChild;
	node* rightChild;
};

#endif //NODE

Header
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
#ifndef BINARY_TREE
#define BINARY_TREE

#include "Node.h"
#include <iostream>

using std::cout;
using std::cin;

class BinaryTree {
public:
	BinaryTree();
	~BinaryTree();
	void inOrderTraversal();
	void postOrderTraversal();
	void preOrderTraversal();
	void add(int);
	void add(node*);
	void deleteValue(int);
	bool search(int);
private:
	node* root;
	void initializeTree();
	void destroyTree();
	void destroy(node*);
	void addHelper(int, node*);
	void addHelper(node*, node*);
	void deleteHelper(int);
	void inOrderHelper(node *);
};

#endif //BINARY_TREE 

Source Code
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
#include "BinaryTree.h"

/*Creates an empty Binary Search Tree
  Postcondition: The root node is set to a null pointer */
BinaryTree::BinaryTree() {
	root = nullptr;
}

/*Deallocates all elements in the binary search tree
  Postcondition: The root node is set to a null pointer */
BinaryTree::~BinaryTree() {
	destroyTree();
}

void BinaryTree::inOrderTraversal() {
	inOrderHelper(root);
}

/*Inserts the input integer into the tree
  Postcondition: The input is inserted into the proper position */
void BinaryTree::add(int i) {
	addHelper(i, root);
}

/*Destroys all elements in the Binary Search Tree
  Postcondition:  The root node is set to a null pointer */
void BinaryTree::destroyTree() {
	destroy(root);
}


/*Deallocates the inputted node and all of its children
  Postcondition: The inputted note is set to a null pointer */
void BinaryTree::destroy(node* n) {
	if(n != nullptr) {
		destroy(n->leftChild);
		destroy(n->rightChild);
		delete n;
		n = nullptr;
	}
}

void BinaryTree::addHelper(int input, node* n) {
	if(n == nullptr) {
		n = new node;
		n->value = input;
		n->leftChild = nullptr;
		n->rightChild = nullptr;
	}
	else if(n->value == input) {
		cout << "Value already in tree.\n";
		cin.get();
	}
	else if(input > n->value) addHelper(input, n->rightChild);
	else if(input < n->value) addHelper(input, n->leftChild);
}

void BinaryTree::deleteHelper(int) {

}

void BinaryTree::inOrderHelper(node* n) {
	if(n == nullptr) return;
	inOrderHelper(n->leftChild);
	cout << n->value << "\n";
	inOrderHelper(n->rightChild);
}


And here is my driver:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "BinaryTree.h"

int main() {
	BinaryTree testTree;
	testTree.add(5);
	testTree.add(5);
	testTree.add(20);
	testTree.add(80);
	testTree.add(22);
	testTree.add(13);
	testTree.add(1);
	testTree.inOrderTraversal();
	cin.get();
}
Topic archived. No new replies allowed.