0% found this document useful (0 votes)
62 views15 pages

m7 Technical

Uploaded by

cvbenterata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views15 pages

m7 Technical

Uploaded by

cvbenterata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

COLLEGE OF COMPUTER STUDIES AND MULTIMEDIA ARTS

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

I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE


 Identify, analyze and solve computing problems using fundamental principles of mathematics and
computing sciences. [PO: B]
II. COURSE LEARNING OUTCOME/S (CLO) ADDRESSED BY THE LABORATORY EXERCISE
 Apply the fundamental principles of data structures and algorithms: concepts of abstract data; types of
common data structures used; description, properties, and storage allocation of data structures. [CLO: 2]

III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE


At the end of this exercise, students must be able to:
 Learn how to create a template version of the Binary Search Tree ADT.
 Apply different operations on Binary Search Tree.
 Solve programming problems that implement Binary Search Tree.

IV. BACKGROUND INFORMATION

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.

Searching a Binary Search Tree


Suppose we wish to search for an element with key x. We are at the root. If the root is 0, then the
search tree contains no elements, and the search terminates unsuccessfully. Otherwise, we compare x
with key in root. If x equals key in root, then search terminates successfully. If x is less than key in root,
then no element in right sub tree can have key value x, and only left subtree is to be searched. If x is larger
than key in root, the no element in left subtree can have the key x, and only right subtree is to be searched.
The subtrees can be searched recursively.

Insertion into a Binary Search Tree


To insert an element x, we must first verify that its key is different from those of existing elements. To
do this, a search is carried out. If the search is unsuccessful, then the element is inserted at the point
where the search terminated.

Deleting from a Binary Search Tree


Deletion from a leaf element is achieved by simply removing the leaf node and making its parent’s child
field to be null. Other cases are deleting a node with one subtree and two subtrees.

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.

ACTIVITY 7.1: Binary Search Tree

CCS0015L-Data Structures and Algorithms Page 2 of 15


Binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers. It is
called a binary tree because each tree node has a maximum of two children.

The binary search tree operations are:

• 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>

using namespace std;

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;

CCS0015L-Data Structures and Algorithms Page 3 of 15


void insertNode(Node*& node, int key) {
if (node == NULL) {
node = new Node(key);
return;
}

if (key < node->key) {


insertNode(node->left, key);
} else if (key > node->key) {
insertNode(node->right, key);
}
}

Node* searchNode(Node* node, int key) {


//TODO
}

Node* findMin(Node* node) {


while (node->left != NULL) {
node = node->left;
}

return node;
}

Node* deleteNode(Node*& node, int key) {


if (node == NULL) {
return node;
}

if (key < node->key) {


node->left = deleteNode(node->left, key);
} else if (key > node->key) {
node->right = deleteNode(node->right, key);
} else {
if (node->left == NULL) {
Node* temp = node->right;
delete node;
return temp;
} else if (node->right == NULL) {
Node* temp = node->left;
delete node;
return temp;
}

Node* temp = findMin(node->right);


node->key = temp->key;
node->right = deleteNode(node->right, temp->key);
}

return node;
}

void inorderTraversal(Node* node) {

CCS0015L-Data Structures and Algorithms Page 4 of 15


//TODO
}

void preorderTraversal(Node* node) {


//TODO
}

void postorderTraversal(Node* node) {


//TODO
}

void displayBST(Node* node, int space) {


if (node == NULL) {
return;
}

space += 5;
displayBST(node->right, space);

cout << endl;


for (int i = 5; i < space; i++) {
cout << " ";
}
cout << node->key << "\n";

displayBST(node->left, space);
}

public:
BST() {
this->root = NULL;
}

void insert(int key) {


insertNode(this->root, key);
}

Node* search(int key) {


return searchNode(this->root, key);
}

void remove(int key) {


deleteNode(this->root, key);
}

void inorder() {
inorderTraversal(this->root);
cout << endl;
}

void preorder() {
preorderTraversal(this->root);
cout << endl;
}

CCS0015L-Data Structures and Algorithms Page 5 of 15


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);

cout << "Inorder traversal: ";


bst.inorder();

cout << "Preorder traversal: ";


bst.preorder();

cout << "Postorder traversal: ";


bst.postorder();

cout << "Displaying BST:\n";


bst.display();

Node* searchedNode = bst.search(60);


if (searchedNode == NULL) {
cout << "Node not found in the BST\n";
} else {
cout << "Node found in the BST: " << searchedNode->key << endl;
}

bst.remove(30);

cout << "Inorder traversal after deleting a node: ";


bst.inorder();

return 0;
}

Sample Output:

CCS0015L-Data Structures and Algorithms Page 6 of 15


Code:

#include <iostream>

using namespace std;

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;

void insertNode(Node*& node, int key) {


if (node == NULL) {
node = new Node(key);
return;
}

if (key < node->key) {

CCS0015L-Data Structures and Algorithms Page 7 of 15


insertNode(node->left, key);
}
else if (key > node->key) {
insertNode(node->right, key);
}
}

Node* searchNode(Node* node, int key) {


if (node == NULL || node->key == key) {
return node;
}

if (key < node->key) {


return searchNode(node->left, key);
}
else {
return searchNode(node->right, key);
}
}

Node* findMin(Node* node) {


while (node->left != NULL) {
node = node->left;
}

return node;
}

Node* deleteNode(Node*& node, int key) {


if (node == NULL) {
return node;
}

if (key < node->key) {


node->left = deleteNode(node->left, key);
}
else if (key > node->key) {
node->right = deleteNode(node->right, key);
}
else {
if (node->left == NULL) {
Node* temp = node->right;
delete node;
return temp;
}
else if (node->right == NULL) {
Node* temp = node->left;
delete node;
return temp;
}

Node* temp = findMin(node->right);


node->key = temp->key;
node->right = deleteNode(node->right, temp->key);
}

CCS0015L-Data Structures and Algorithms Page 8 of 15


return node;
}

void inorderTraversal(Node* node) {


if (node == NULL) {
return;
}
inorderTraversal(node->left);
cout << node->key << " ";
inorderTraversal(node->right);
}

void preorderTraversal(Node* node) {


if (node == NULL) {
return;
}

cout << node->key << " ";


preorderTraversal(node->left);
preorderTraversal(node->right);
}

void postorderTraversal(Node* node) {


if (node == NULL) {
return;
}

postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->key << " ";
}

void displayBST(Node* node, int space) {


if (node == NULL) {
return;
}

space += 5;
displayBST(node->right, space);

cout << endl;


for (int i = 5; i < space; i++) {
cout << " ";
}
cout << node->key << "\n";

displayBST(node->left, space);
}

public:
BST() {
this->root = NULL;

CCS0015L-Data Structures and Algorithms Page 9 of 15


}

void insert(int key) {


insertNode(this->root, key);
}

Node* search(int key) {


return searchNode(this->root, key);
}

void remove(int key) {


deleteNode(this->root, key);
}

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);

cout << "Inorder traversal: ";


bst.inorder();

cout << "Preorder traversal: ";


bst.preorder();

cout << "Postorder traversal: ";


bst.postorder();

cout << "Displaying BST:\n";


bst.display();

CCS0015L-Data Structures and Algorithms Page 10 of 15


Node* searchedNode = bst.search(60);
if (searchedNode == NULL) {
cout << "Node not found in the BST\n";
}
else {
cout << "Node found in the BST: " << searchedNode->key << endl;
}

bst.remove(30);

cout << "Inorder traversal after deleting a node: ";


bst.inorder();

return 0;
}

Output:(screenshot of the output)

CCS0015L-Data Structures and Algorithms Page 11 of 15


VI. QUESTION AND ANSWER

Directions: Briefly answer the questions below.

 Differentiate binary tree and binary search tree.

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:

CCS0015L-Data Structures and Algorithms Page 12 of 15


o Binary trees can be classified into various types, such as:
 Full Binary Tree: Every node other than the leaves has two
children.
 Complete Binary Tree: All levels, except possibly the last, are
fully filled, and all nodes are as far left as possible.
 Perfect Binary Tree: All internal nodes have two children, and all
leaves are at the same level.
 Balanced Binary Tree: The height of the left and right subtrees of
any node differ by at most one.

4. Usage:
o Binary trees are used in various applications such as expression trees,
Huffman coding trees, and representing hierarchical data structures.

Binary Search Tree (BST)

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:

CCS0015L-Data Structures and Algorithms Page 13 of 15


o Binary Tree: No specific order is required among the nodes.
o BST: Nodes must follow the left child < parent < right child rule.

 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

CCS0015L-Data Structures and Algorithms Page 14 of 15


A completed
A completed solution is An incomplete
A completed
solution is implemented solution is
solution runs
tested and runs on the required implemented
without errors.
but does not platform, and on the required
It meets all the
meet all the uses the platform. It
specifications
specifications compiler does not
and works for
nd/or work for specified. It compile and/or
all test data.
all test data. runs, but has run.
logical errors.
Solution(x5)

The program Not all of the


Few of the
The program design selected
selected
design uses generally uses structures are
structures are
appropriate appropriate appropriate.
appropriate.
structures. The structures. Some of the
Program
overall program Program program
elements are
design is elements elements are
not well
appropriate. exhibit good appropriately
designed.
Program design. designed.
Design(x3)

All required There are few


All required
parts of the parts of the Most of the
parts in the
document are document are parts of the
document are
complete and missing but the document are
present and
correct(code, rest are missing and
correct but not
output of complete and incorrect.
complete.
Completeness screenshots) correct.
of
Document(x2)
Total

CCS0015L-Data Structures and Algorithms Page 15 of 15

You might also like