If value is on Left side of Binary tree

I don't understand what value I'm supposed to be checking against so that the function checks whether it's there or not. My question is what do i need to compare for it to evaluate true or false?

I get that it has to check whether a value is on the left or right and the value can't be a nullptr. why can't i use n->getLeftside() or n->getRightside() to check if the value is on respective sides?


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
//BinaryTree.cpp
template <class Object>
BinaryTree<Object>::BinaryTree() {
	root = nullptr;
}

template<class Object>
bool BinaryTree<Object>::isOnLeftSide(const Object& value)
{
	BinaryTreeNode<Object> * n = new BinaryTreeNode<Object>(value);
	
	if (n == nullptr){

		return false;
	}	
	if (n->getLeftSide())
	{
		cout << "true " << endl;
		return true;		
	}

}

//Main.cpp


void testone(int n)
{
        int one = 1, two = 2, three = 3, four = 4, five = 5;
	BinaryTree<int> t1 = BinaryTree<int>(0);
	BinaryTree<int> t2 = BinaryTree<int>();

	//       0
	//     1     2
	//   3   4     5
	//

	BinaryTreeNode<int> * root = t1.getRoot();

	BinaryTreeNode<int> * nodeOne = new BinaryTreeNode<int>(one);
	BinaryTreeNode<int> * nodeTwo = new BinaryTreeNode<int>(two);

	root->setLeftSide(nodeOne);
	root->setRightSide(nodeTwo);

	nodeOne->setLeftSide(new BinaryTreeNode<int>(three));
	nodeOne->setRightSide(new BinaryTreeNode<int>(four));
	nodeTwo->setRightSide(new BinaryTreeNode<int>(five));
switch (n)
	{
case  1:
        assert(t1.isOnLeftSide(0) == false);
	assert(t1.isOnLeftSide(1) == true);
	assert(t1.isOnLeftSide(2) == false);
	assert(t1.isOnLeftSide(3) == true);
	assert(t1.isOnLeftSide(4) == false);
	assert(t1.isOnLeftSide(5) == false);
break;
}
	default:
		cout << "Bad argument" << endl;
		break;
	}



//BinaryTreeNode.cpp


template <class Object>
const Object& BinaryTreeNode<Object>::getElement() const {
	return( element );
}

template <class Object>
BinaryTreeNode<Object>* BinaryTreeNode<Object>::getLeftSide() const {
	return( leftSide );
}

template <class Object>
BinaryTreeNode<Object>* BinaryTreeNode<Object>::getRightSide() const {
	return( rightSide );
}
Last edited on
why can't i use n->getLeftside() or n->getRightside() to check if the value is on respective sides?
Because n in this case is a newly created node where it is guaranteed that there is nothing left or right.

Instead initialize n with root and check reqursively whether a value is on the left side.
I set it to root.

I still run into the same issue where assert fails at "1"
well i got it to work. it works while 1 &3 but fails on 2,4,5
whenever it goes to check if it's on the right side it fails.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	BinaryTreeNode<Object> * k = root->getLeftSide();
	BinaryTreeNode<Object> * l = root->getRightSide();

	if (k)
	{
		cout << "it's on the  left" << endl;
		return true;
	}
	if (l)
	{
		cout << "it's on the right" << endl;
		return false;
	}
	else
	{
		cout << "it's something...." << endl;
		return false;
	}
In order to tell whether an object is in the tree you need to compare it. So without that comparison it cannot work.

Here is one way to do this. A tree always requires recursion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template<class Object>
bool BinaryTree<Object>::isOnLeftSide(const Object& value)
{
bool is_ok = false;
if(root)
{
	BinaryTreeNode<Object> *left_node = root->getLeftSide();
	
	if (left_node) // if there is no left node the object cannot be found
	{
		if (left_node->getElement() == value) // check whether the object matches
		  is_ok = true;
		else // if the object does not match it might be deeper in the tree
		{
		  BinaryTree<Object> bt{left_node}; // this is a new tree with the left node as root
		  is_ok = bt.isOnLeftSide(value); // check the next level of the tree. This is recursion!
		}
	}
}
return is_ok;
}
I see the need of the nested if statements and why it works. although you lost me on
BinaryTree<Object> bt{left_node};
I tried to find a way that is easier for me to understand and would work similarly, so i tried this
is_ok = root->getRightSide()->getLeftSide();
but it fails at the last left node.
why does it take issue with the last left node but not the first left node yet is able to keep going right...only if the last node is not asserted?

Last edited on
although you lost me on
A temporary binary tree is created that uses on the current node for recursive traversing. It requires that the tree does not delete the nodes when destroyed. Maybe not the best approach.

so i tried this
That will crash if getRightSide() returns null. The whole expression however checks only the existance of the left branch not whether the value in question exists. You need to compare the element.
Topic archived. No new replies allowed.