Binary search tree printing out incorrect height
Feb 8, 2020 at 10:48pm UTC
I am trying to create a simple Binary Search Tree, then add several integers to the tree and finally print out the height of the tree. If I add integers of 10, 15 and 2 the tree should have a height of 2 i.e. 10 at the root, 15 to the right of 10 and 2 to the left of 10. Although the height is printed out as 3. Can anyone tell me what I am doing wrong please?
TreeNode.cpp
1 2 3 4 5 6 7 8 9
#include "TreeNode.h"
TreeNode::TreeNode(int theData)
{
theData = value;
}
BinaryTree.cpp
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"
#include "TreeNode.h"
BinaryTree::BinaryTree()
{
rootPtr = NULL;
}
void BinaryTree::add(int data)
{
if (rootPtr != NULL) {
add(rootPtr, data);
}
else {
rootPtr = new TreeNode(data);
rootPtr->left = NULL;
rootPtr->right = NULL;
}
}
void BinaryTree::add(TreeNode* toAdd, int data)
{
if (data < toAdd->value) {
if (toAdd->left != NULL) {
add(toAdd->left, data);
}
else {
toAdd->left = new TreeNode(data);
toAdd->left->left = NULL;
toAdd->left->right = NULL;
}
}
else {
if (toAdd->right != NULL) {
add(toAdd->right, data);
}
else {
toAdd->right = new TreeNode(data);
toAdd->right->left = NULL;
toAdd->right->right = NULL;
}
}
}
int BinaryTree::height()
{
return height(rootPtr);
}
int BinaryTree::height(TreeNode* node)
{
if (node == NULL) {
return 0;
}
else {
int leftSide = height(node->left);
int rightSide = height(node->right);
if (leftSide > rightSide)
return (leftSide + 1);
else return (rightSide + 1);
}
}
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "BinaryTree.h"
#include <iostream>
using namespace std;
int main()
{
BinaryTree tree = BinaryTree();
tree.add(10);
tree.add(15);
tree.add(2);
cout << tree.height();
return 0;
}
Last edited on Feb 8, 2020 at 10:53pm UTC
Feb 8, 2020 at 10:54pm UTC
Well, you haven't supplied the relevant .h files, but I can't see how this is right:
1 2 3 4
TreeNode::TreeNode(int theData)
{
theData = value;
}
Maybe you meant
1 2 3 4
TreeNode::TreeNode(int theData)
{
value = theData;
}
But difficult to say without the relevant code.
Feb 8, 2020 at 10:58pm UTC
I don't know how I managed to do that.. thanks a lot!
Feb 8, 2020 at 10:58pm UTC
You haven't shown your header files, but your TreeNode ctor isn't initializing the left and right pointers to nullptr (don't use NULL!). Also, the assignment that you do have in that ctor looks backwards. Usually we would use the initializer list, anyway:
1 2 3 4
TreeNode::TreeNode(int theData)
: value(theData), left(nullptr ), right(nullptr )
{
}
Last edited on Feb 8, 2020 at 10:59pm UTC
Feb 8, 2020 at 11:04pm UTC
thanks dutch
Feb 8, 2020 at 11:07pm UTC
dutch, so you suggest i change all of the NULL values in my BinaryTree.cpp file to nullptr also?
Feb 8, 2020 at 11:16pm UTC
Yes (unless you're compiling as pre-C++11, which you shouldn't!).
I'm not sure how simply reversing that assignment would affect the operation of height. (?)
Note that you can test for nulls with boolean operations, like so:
1 2 3 4 5 6 7 8 9
// instead of
if (ptr == nullptr ) { }
// just say
if (!ptr) { }
// and instead of
if (ptr != nullptr ) {}
// just say
if (ptr) {}
Also:
1 2 3 4 5
//You don't need to say
BinaryTree tree = BinaryTree();
// just say
BinaryTree tree;
I would also use spaces instead of tabs.
Here's the code in one file (with reconstructed headers). This way it can be run online by clicking the gear symbol beside the 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 68 69 70 71 72 73 74 75 76 77 78 79
// TreeNode.h ////////////////////////////////////////
struct TreeNode {
int value;
TreeNode *left, *right;
TreeNode(int theValue);
};
// TreeNode.cpp ////////////////////////////////////////
//#include "TreeNode.h"
TreeNode::TreeNode(int theValue)
: value(theValue), left(nullptr ), right(nullptr ) {
}
// BinaryTree.h ////////////////////////////////////////
class BinaryTree {
TreeNode *rootPtr;
static void add(TreeNode* toAdd, int data);
static int height(TreeNode* node);
public :
BinaryTree();
void add(int data);
int height();
};
// BinaryTree.cpp ////////////////////////////////////////
//#include "BinaryTree.h"
//#include "TreeNode.h"
BinaryTree::BinaryTree()
: rootPtr(nullptr ) {
}
void BinaryTree::add(int data) {
if (rootPtr)
add(rootPtr, data);
else
rootPtr = new TreeNode(data);
}
void BinaryTree::add(TreeNode* toAdd, int data) {
if (data < toAdd->value)
if (toAdd->left)
add(toAdd->left, data);
else
toAdd->left = new TreeNode(data);
else
if (toAdd->right)
add(toAdd->right, data);
else
toAdd->right = new TreeNode(data);
}
int BinaryTree::height() {
return height(rootPtr);
}
int BinaryTree::height(TreeNode* node) {
if (!node) return 0;
int leftSide = height(node->left);
int rightSide = height(node->right);
return (leftSide > rightSide ? leftSide : rightSide) + 1;
}
// main.cpp ////////////////////////////////////////
//#include "BinaryTree.h"
#include <iostream>
int main()
{
BinaryTree tree;
for (int n: {10, 15, 2})
tree.add(n);
std::cout << tree.height() << '\n' ;
}
Last edited on Feb 8, 2020 at 11:21pm UTC
Feb 8, 2020 at 11:29pm UTC
fantastic, thanks a lot for that!
Feb 9, 2020 at 12:00am UTC
me wrote:I'm not sure how simply reversing that assignment would affect the operation of height. (?)
I figured it out. If all the uninitialized values end up being 0 (which is very possible since the OS zeroes new pages that it allocates to a process) then the tree will be created in a "straight line" (basically a linked list) and will therefore have a height equal to its node count.
Last edited on Feb 9, 2020 at 12:01am UTC
Feb 9, 2020 at 12:06am UTC
makes sense! I could really not figure out why the height was incrementing linearly as i added nodes. thanks again for the help
Last edited on Feb 9, 2020 at 12:07am UTC
Topic archived. No new replies allowed.