m7 Technical
m7 Technical
CCS0015L
(DATA STRUCTURES AND ALGORITHMS)
EXERCISE
7
BINARY SEARCH TREE
Student Name / Group
Name: Vonn Kendrick C. Pedrena
Name Role
Members (if Group):
Section:
AN12
Professor:
Mark Anthony Cezar
A binary tree is a special kind of tree in which each node can have at most two children: they are
distinguished as a left child and a right child. The subtree rooted at the left child of a node is called its left
subtree and the subtree rooted at the right child of a node is called its right subtree.
V. LABORATORY ACTIVITY
Directions: Complete the program below that satisfy the given problem specification and output. Look for
the functions with //TODO. Paste your completed source code in the Code portion then highlight the
statement(s) that you filled in the incomplete program with color yellow.
• insert(int key): Inserts a new node with the given key value into the binary search tree. If the key
value already exists in the tree, the function does nothing.
• search(int key): Searches the binary search tree for a node with the given key value. If a node with
the given key value is found, the function returns a pointer to the node. Otherwise, the function
returns a NULL pointer.
• remove(int key): Removes the node with the given key value from the binary search tree. If the
node with the given key value is not found in the tree, the function does nothing.
• inorder(): Performs an in-order traversal of the binary search tree, printing the key values of each
node in ascending order.
• preorder(): Performs a pre-order traversal of the binary search tree, printing the key values of each
node in the order that they are visited.
• postorder(): Performs a post-order traversal of the binary search tree, printing the key values of
each node after visiting all of its children.
• display(): Displays the binary search tree as a tree structure, with each node represented by its key
value and indented according to its level in the tree.
Given Program:
#include <iostream>
class Node {
public:
int key;
Node* left;
Node* right;
Node(int key) {
this->key = key;
this->left = NULL;
this->right = NULL;
}
};
class BST {
private:
Node* root;
return node;
}
return node;
}
space += 5;
displayBST(node->right, space);
displayBST(node->left, space);
}
public:
BST() {
this->root = NULL;
}
void inorder() {
inorderTraversal(this->root);
cout << endl;
}
void preorder() {
preorderTraversal(this->root);
cout << endl;
}
void display() {
displayBST(this->root, 0);
}
};
int main() {
BST bst;
bst.insert(50);
bst.insert(30);
bst.insert(70);
bst.insert(20);
bst.insert(40);
bst.insert(60);
bst.insert(80);
bst.remove(30);
return 0;
}
Sample Output:
#include <iostream>
class Node {
public:
int key;
Node* left;
Node* right;
Node(int key) {
this->key = key;
this->left = NULL;
this->right = NULL;
}
};
class BST {
private:
Node* root;
return node;
}
postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->key << " ";
}
space += 5;
displayBST(node->right, space);
displayBST(node->left, space);
}
public:
BST() {
this->root = NULL;
void inorder() {
inorderTraversal(this->root);
cout << endl;
}
void preorder() {
preorderTraversal(this->root);
cout << endl;
}
void postorder() {
postorderTraversal(this->root);
cout << endl;
}
void display() {
displayBST(this->root, 0);
}
};
int main() {
BST bst;
bst.insert(50);
bst.insert(30);
bst.insert(70);
bst.insert(20);
bst.insert(40);
bst.insert(60);
bst.insert(80);
bst.remove(30);
return 0;
}
Binary Tree and Binary Search Tree (BST) are both types of tree data structures, but they
have different properties and purposes. Here are the key differences:
Binary Tree
1. Definition:
o A binary tree is a data structure in which each node has at most two
children, referred to as the left child and the right child.
2. Structure:
o The structure of a binary tree is not restricted to any specific order of the
nodes. The nodes can be organized in any manner.
3. Types:
4. Usage:
o Binary trees are used in various applications such as expression trees,
Huffman coding trees, and representing hierarchical data structures.
1. Definition:
o A binary search tree is a special type of binary tree where each node has at
most two children, and the nodes are organized in a specific order.
Specifically, for any given node:
The left subtree contains nodes with keys less than the node's key.
The right subtree contains nodes with keys greater than the node's
key.
2. Structure:
o The structure of a BST is ordered. It follows the property that all values in
the left subtree are less than the value of the root, and all values in the right
subtree are greater than the value of the root.
3. Types:
o BSTs can be further classified based on specific properties, such as:
Balanced BST: Includes AVL trees and Red-Black trees, where the
tree maintains a balanced height to ensure efficient operations.
Unbalanced BST: If insertions are made in a sorted order, the BST
can become a skewed tree, reducing efficiency.
4. Usage:
o BSTs are primarily used for efficient searching, insertion, and deletion
operations. They are used in applications like databases, file systems, and
many search algorithms.
Key Differences
Order:
Efficiency:
o Binary Tree: Can degrade to a linear structure (linked list) in the worst case,
making operations like search, insert, and delete O(n).
o BST: Average-case operations (search, insert, delete) are O(log n), but can
degrade to O(n) if unbalanced.
Applications:
o Binary Tree: Used in non-ordered data scenarios like hierarchical data
representation.
o BST: Used in ordered data scenarios for efficient searching and sorting
operations.
Discuss why searching for an element in the Binary search tree is easy.
Searching for an element in a Binary Search Tree (BST) is considered efficient and
straightforward due to the specific ordering properties that BSTs maintain
VII. REFERENCES
Wittenberg, Lee.(2018). Data structures and algorithms in C++. s.l.: Mercury Learning
Baka, Benjamin(2017). Python data structures and algorithms : improve the performance and speed of
your applications. Birmingham, U.K : Packt Publishing
Downey, Allen.(2017). Think data structures : algorithms and information retrieval in Java. Sebastopol,
CA: O'Reilly
Chang, Kung-Hua(2017). Data Structures Practice Problems for C++ Beginners. S.l : Simple and
Example
Hemant Jain(2017). Problem Solving in Data Structures & Algorithms Using C++: Programming
Interview Guide. USA: CreateSpace Independent Publishing Platform
RUBRIC:
Criteria 4 3 2 1 Score