LAB 7
BINARY SEARCH TREE
Data Structures
2026
1
AGENDA
• What is BST ?!
• Class TreeNode Declaration.
• Class Tree Declaration.
• Task 1: Insert function.
• Task 2: Find function.
• Task 3: GetLevel function.
• Main and testing.
•Problem 1: Remove a range
•Problem 2: Count how many elements within a
range
BINARY SEARCH TREE
(BST)
It is a binary tree where for each node x:
x’s left subtree values < x < x’s right
subtree values
46
63
17
25
19
55
97
CLASS DECLARATION
(TREENODE)
Class (TreeNode)
1. Has member variables of
?A pointer to NODE ( left)
?A pointer to NODE ( right)
?Value of the data
2. Has One constructor
?With parameters.
CLASS DECLARATION
(TREENODE.H)
TreeNode.h
TreeNode.cpp
// Tree.h
// Tree.cpp
template <class T>
#include "Tree.h“
class TreeNode {
public:
TreeNode<T>* left;
TreeNode<T>* right;
T data;
TreeNode(T);
template<class T>
};
}
TreeNode<T>::TreeNode(T
v) {
data=v;
left=NULL;
right=NULL;
CLASS DECLARATION
(TREE)
Class (Tree)
1. Has member variables of
?A pointer to NODE ( root)
?Size of tree
2. Has One constructor
?Default Constructor.
CLASS DECLARATION
(TREE)
Tree.h
template<class T>
class Tree {
private:
TreeNode<T> *root;
int size;
public:
Tree();
};
Tree.cpp
template<class T>
Tree<T>::Tree() {
size = 0;
root = NULL;
}
INSERT OPERATION
Example: insert 60 in the tree:
1. start at the root, 60 is greater than 25,
search in right subtree
2. 60 is greater than 50, search in 50’s right
subtree
3. 60 is less than 70, search in 70’s left subtree
4. 60 is less than 66, add 60 as 66’s left child
INSERT OPERATION
CONT.
Always insert new node as leaf node
Start at root node as current node
While the end of the tree is not reached
?If new node’s data < current’s data
? Then Search left
? If new node’s key > current’s key
? Then Search right
? Else
? Then, It already exists
End while
Add the node in its correct place, whether at the right or the left of the
targeted subtree.
Increment the size.
INSERT OPERATION
(TREE.H)
template<class T>
class Tree {
private:
TreeNode<T> * root;
int size;
public:
Tree();
void insert(T val);
};
30 minutes
Task : INSERT
OPERATION (TREE.CPP)
template<class T>
void Tree<T>::insert(T val) {
else {
cout<<“The value is
already exist"<<endl;
return;
}
if (root==NULL) {
root=new TreeNode<T>(val);
size++;
return;
}
}
TreeNode<T> * ptr = root;
TreeNode<T> * parentPtr = root;
while(ptr != NULL) {
if (val > ptr->data) {
parentPtr = ptr;
ptr = ptr->right;
} else if (val < ptr->data) {
parentPtr = ptr;
ptr = ptr->left;
}
ptr = new TreeNode<T>(val);
if(val > (parentPtr -> data)) {
parentPtr -> right = ptr;
} else {
parentPtr -> left = ptr;
}
size++;
}
FIND OPERATION
Example: search for 45 in the tree
1. start at the root, 45 is greater than 25,
search in right subtree
2. 45 is less than 50, search in 50’s left
subtree
3. 45 is greater than 35, search in 35’s right
subtree
4. 45 is greater than 44 , but 44 has no right
subtree so 45 is not in the BST
FIND OPERATION CONT.
Start at the root node as current node
While the node is not found and the tree did
not end
?
If the value is less than the data in the current
node
?
?
Then Search in the left sub tree
Else if the value is greater than the data in the
current node
?
?
Then Search in the right sub tree
Else
?
Then, The node is found
End while
FIND OPERATION (TREE.H)
template<class T>
class Tree {
private:
TreeNode<T> * root;
int size;
public:
Tree();
void insert(T val);
bool find(T val);
};
15 minutes
FIND OPERATION
(TREE.CPP)
template <class T>
bool Tree<T>::find(T val) {
TreeNode<T> * ptr = root;
while(ptr != NULL) {
if (val < ptr->data)
ptr = ptr->left;
else if (val > ptr->data)
ptr = ptr->right;
else
return true;
}
return false;}
GETLEVEL(VALUE)
OPERATION
? which takes the value and returns the
level of the tree in which the value exists
(the root is at level 0, children of the root
are at level 1).
GETLEVEL(VALUE)
OPERATION (TREE.H)
template<class T>
class Tree {
private:
TreeNode<T> * root;
int size;
public:
Tree();
void insert(T val);
bool find(T val);
int GetLevel(T val)
};
15 minutes
GETLEVEL(VALUE)
OPERATION
template <class T>
int Tree<T>::GetLevel(T
val)
else if (val >tmp->value)
{
tmp=tmp->right;
{
lvl++;
TreeNode<T>* tmp=
root;
int lvl =0;
while(tmp!= NULL)
}
else {
tmp=tmp->left;
lvl++; } }
{
if(tmp->value==val)
return lvl;
return -1; }
THE “MAIN” FUNCTION
#include "Tree.cpp“
#include <iostream>
using namespace std;
void main() {
Tree<int> BST;
BST.insert(10);
BST.insert(5);
BST.insert(15);
BST.insert(25);
cout<<BST.GetLevel(100)<<endl;
if(BST.find(7)== true)
cout<<"Found 7"<<endl;
else cout<<“Not Found"<<endl;
if(BST.find(10)== true)
cout<<"Found 10"<<endl;
else cout<<“Not Found"<<endl; }
Problem 1: Remove Range
15 minutes
Using the binary search tree STL , remove all elements
within a given range [L,R].
Remove Range : Pseudo
code
FUNCTION RemoveRange(S, L, R)
it ← S.begin
WHILE it ≠ S.end DO
IF value at it ≥ L AND value at it ≤ R THEN
it ← delete element at it
ELSE
move it to next element
END IF
END WHILE
END FUNCTION
Remove Range : Solution
void RemoveRange(set<int>& s, int L, int R) {
set<int>::iterator it ;
it= s.begin();
while (it != s.end())
{
if (*it >= L && *it <= R) {
it = s.erase(it);
}
else {
++it;
}
}
}
Problem 2: Count how many
elements within a range
15 minutes
Using the binary search tree STL , write a function to count
how many elements lie within a given range [L,R].
Count elements : Pseudo
code
FUNCTION CountRange(S, L, R)
count ← 0
it ← S.begin
WHILE it ≠ S.end DO
IF value at it ≥ L AND value at it ≤ R THEN
count ← count + 1
END IF
move it to next element
END WHILE
RETURN count
END FUNCTION
Count elements : Solution
int CountRange(set<int>& s, int L, int R)
{
int count = 0;
set<int>::iterator it;
for (it = s.begin(); it != s.end(); it++)
{
if (*it >= L && *it <= R) {
count++;
}
}
return count;
}
THE “MAIN” FUNCTION
set<int> s;
s.insert(1);
s.insert(3);
s.insert(5);
s.insert(7);
s.insert(9);
s.insert(11);
cout << "Original set: ";
set<int>::iterator it;
for (it = s.begin(); it != s.end();
it++) {
cout << *it << endl;
}
set<int>::iterator it;
for (it = s.begin(); it != s.end(); it++) {
cout << *it << endl;
}
int L = 4, R = 10;
int result = countRange(s, L, R);
cout << "Count in range << result <<
endl;
removeRange(s, L, R);
cout << "Set after removing range: ";
for (set<int>::iterator it = s.begin(); it
!= s.end(); it++) {
cout << *it << endl;
}
return 0;
}
Brainstorming Task: Find closest value
in BST
Brainstorming Question:
Imagine you have a Binary Search Tree of integers, and you want to build a tool that finds the
closest value to a given number x in the tree.
BST Elements: {2, 5, 9, 12, 18}
Input: X = 10
Output: Closest value = 9
Input: X = 13
Output: Closest value = 12
For Discussion:
•
•
How does the BST property (left < root < right) help reduce the number of comparisons?
How could an iterator or recursion be used to traverse the BST for this task?
27
Thank You ☺