0% found this document useful (0 votes)
11 views

Projectdoc

This document describes a binary search tree (BST) data structure project assigned to students at Mutah University. The project is due on January 9, 2024 and involves implementing BST operations recursively and iteratively. The document provides an abstract, table of contents, introduction on BSTs, specification of BST operations, and outlines an initial class for implementing the BST as a linked structure with recursive functions.

Uploaded by

Mohammad Qassas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Projectdoc

This document describes a binary search tree (BST) data structure project assigned to students at Mutah University. The project is due on January 9, 2024 and involves implementing BST operations recursively and iteratively. The document provides an abstract, table of contents, introduction on BSTs, specification of BST operations, and outlines an initial class for implementing the BST as a linked structure with recursive functions.

Uploaded by

Mohammad Qassas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Mutah University

Computer engineering department


Binary search tree(BST)
Supervisor: Dr.Hani Q. Al-Zoubi

Students:
Abd-Alrahman Al-Rashdan
Thaer Al-Jaafrah
Omar albustanji
Huthaifa Al-Khateeb
Muhammad Al-Qassas

Due Date: 9 / Jan / 2024


1
Abstract
A Binary Search Tree (BST) is a hierarchical data structure in which each node has at most two
children, referred to as the left child and the right child. In a BST, the value of nodes in the left subtree
is less than the value of the root node, and the value of nodes in the right subtree is greater than the
value of the root node. This property allows for efficient searching, insertion, and deletion of
elements.

Each node in a BST typically contains the following components:

- Value/Data: The value associated with the node.

- Left Child: A pointer/reference to the left child node.

- Right Child: A pointer/reference to the right child node.

The BST supports various operations, including:

- Insertion: Adding a new node to the tree while maintaining the BST property.

- Deletion: Removing a node from the tree while maintaining the BST property.

- Searching: Finding a specific value within the tree efficiently.

- Traversal: Visiting and processing all nodes in a specific order (in-order, pre-order, post-order).

BSTs are commonly used in computer science due to their ability to provide relatively fast operations
for searching and sorting data.

Understanding the structure and rules of a BST is crucial for implementing it in any programming
language like C++, Java, Python, etc. This structure allows for the efficient organization and retrieval
of data.

2
TABLE OF CONTENTS

Abstract ------------------------------------------------------------------------------------------------------2
Table of contents ------------------------------------------------------------------------------------------3
Introduction-------------------------------------------------------------------------------------------------4
Specification-------------------------------------------------------------------------------------------------5
Implementation --------------------------------------------------------------------------------------------6
Recursive Binary Search Tree Operations------------------------------------------------------------7
2.1 The Functions IsFull and IsEmpty ---------------------------------------------------------------- 8

2.2 The Function LengthIs ------------------------------------------------------------------------------ 8

2.3 The Function RetrieveItem ------------------------------------------------------------------------ 9

2.4 The Function InsertItem ----------------------------------------------------------------------------9

2.5 The Function DeleteItem ---------------------------------------------------------------------------10

2.6 The Function Print ---------------------------------------------------------------------------------- 10

2.7 The Class Constructor and Destructor ----------------------------------------------------------10

2.8 The Functions ResetTree and GetNextItem ---------------------------------------------------12

3 conclusion -----------------------------------------------------------------------------------------------13

3
introduction
In the realm of data structures, the linear linked list has proven advantageous for
storing sorted information. However, a notable drawback surfaces when confronted with the
time complexity of searching through lengthy lists. The sequential or linear search, with its
O(N) time complexity, becomes a potential bottleneck for efficiency, particularly as the list
grows. Fortunately, in the pursuit of enhancing search speed while preserving the flexibility of
linked structures, the binary search tree (BST) emerges as a compelling solution.

Unlike arrays, where binary search efficiently navigates a sorted list by finding
midpoints, applying the same strategy to a linked list poses challenge. The practicality of
determining the midpoint in a linked list remains elusive. Enter the binary search tree — a
structure that not only maintains the adaptability of linked lists but also facilitates rapid
O(log2N) access to any node within the collection.

A binary search tree is a structure with two properties: a shape property and a
property that relates the keys of the elements in the structure. We look first at the shape
property. Each node in a singly linked list may point to one other node: the one that follows
it. Thus, a singly linked list is a linear structure; each node in the list (except the last) has a
unique successor. In contrast, a binary tree is a structure in which each node can have two
successor nodes, called children. Each of the children, being nodes in the binary tree, can also
have two child nodes, and these children can also have two children, and so on, giving the
tree its branching structure. The “beginning” of the tree is a unique starting node called the
“root”. If a node in the tree has no children, it is called a “leaf”.

4
Specification

Structure: The placement of each element in the binary tree must satisfy the binary search property: The
value of the key of an element is greater than the value of the key of any element in its left subtree, and
less than the value of the key of any element in its right subtree.

Operations (provided by TreeADT):


Assumption: Before any call is made to a tree operation, the tree has been declared and a constructor
has been applied.

 MakeEmpty
Function: Initializes tree to empty state.

Postcondition: Tree exists and is empty.

 Boolean IsEmpty
Function: Determines whether tree is empty.

Postcondition: Function value = (tree is empty).

 Boolean IsFull
Function: Determines whether tree is full.

Postcondition: Function value = (tree is full).

 int LengthIs
Function: Determines the number of elements in tree.

Postcondition: Function value = number of elements in tree.

 RetrieveItem(ItemType& item, Boolean& found)


Function: Retrieves item whose key matches item’s key (if present).

Precondition: Key member of item is initialized.

Postconditions: If there is an element someItem whose key matches item’s key, then found = true and
item is a copy of someItem; otherwise, found = false and item is unchanged. Tree is unchanged.

 InsertItem(ItemType item)
Function: Adds item to tree.

Preconditions: Tree is not full. item is not in tree.

Postconditions: item is in tree. Binary search property is maintained.

5
 DeleteItem(ItemType item)
Function: Deletes the element whose key matches item’s key.

Preconditions: Key member of item is initialized. One and only one element in tree has a key matching
item’s key.

Postcondition: No element in tree has a key matching item’s key.

 Print(ofstream& outFile)
Function: Prints the values in the tree in ascending key order on outFile.

Precondition: outFile has been opened for writing.

Postconditions: Items in the tree have been printed in ascending key order. outFile is still open.

 ResetTree(OrderType order)
Function: Initializes current position for an iteration through the tree in OrderType order.

Postcondition: Current position is prior to root of tree.

 GetNextItem(ItemType& item, OrderType order, Boolean& finished)


Function: Gets the next element in tree.

Preconditions: Current position is defined. Element at current position is not last in tree.

Postconditions: Current position is one position beyond current position at entry to GetNextItem.

finished = (current position is last in tree). item is a copy of element at current position.

Implementation
We develop the algorithms for the operations specified for the Binary Search Tree ADT and represent the
tree as a linked structure whose nodes are allocated dynamically. Because the binary search tree is
inherently a recursive structure, we first implement the algorithms using recursive solutions. We then
take the InsertItem and DeleteItem functions and show how they can be implemented iteratively. Here is
the first approximation for the class TreeType. If we need more data members, we can add them at a
later time. As with our first brush with linked lists, we choose not to make the class generic. Let’s make
this implementation be a tree of char values.

6
#include <iostream>
struct TreeNode;
typedef char ItemType;
// Assumption: ItemType is a type for which the operators "<"
// and "==" are defined--either an appropriate built-in type or
// a class that overloads these operators.
enum OrderType {PRE_ORDER, IN_ORDER, POST_ORDER};
class TreeType
{
public:
TreeType(); // Constructor.
~TreeType(); // Destructor.
TreeType(const TreeType& originalTree); // Copy constructor.
void operator=(TreeType& originalTree);
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
int LengthIs() const;
void RetrieveItem(ItemType& item, bool& found) const;
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void ResetTree(OrderType order);
void GetNextItem(ItemType& item, OrderType order,
bool& finished);
void Print(std::ofstream& outFile) const;
private:
TreeNode* root;
};

Recursive Binary Search Tree Operations


The TreeType class contains the external pointer to the list of nodes as a data member (root). The
recursive implementations of the tree operations should recurse on nodes. Therefore each member
function calls a secondary recursive function that takes root as a parameter. The names of the secondary
functions make it clear which operations they help to implement. Because we must declare a function
before we can use it, we must list each recursive function before the class function or we must list its
prototype.

7
The Functions IsFull and IsEmpty
⮚ IsFull: This function checks whether the binary search tree is full or not. A full binary tree is a tree
where every node other than the leaves has two children. So, if the tree is completely filled with
nodes, except possibly for the bottom level which is filled from left to right, it is considered full.
⮚ IsEmpty: This function checks if the binary search tree is empty, meaning it has no nodes.

Example:
bool TreeType::IsFull() const
// Returns true if the free store has no room for another node
// and false otherwise.
{
TreeNode* location;
try
{
location = new TreeNode;
delete location;
return false;
}
catch(std::bad_alloc exception)
{
return true;
}
}
bool TreeType::IsEmpty() const
// Returns true if the tree is empty and false otherwise.
{
return root == NULL;
}

The Function LengthIs


⮚ LengthIs: This function calculates the number of nodes in the binary search tree. It traverses the
tree and counts the nodes to determine its length.

Example:
int CountNodes(TreeNode* tree);
int TreeType::LengthIs() const
// Calls the recursive function CountNodes to count the
// nodes in the tree.
{
return CountNodes(root);
}
int CountNodes(TreeNode* tree)
// Post: Returns the number of nodes in the tree.
{
if (tree == NULL)
return 0;
else

8
return CountNodes(tree->left) + CountNodes(tree->right) + 1;
}

The Function RetrieveItem


⮚ RetrieveItem: This function takes an item/key as input and searches the tree to retrieve the node
containing that item/key. It returns the node if found, otherwise, it returns a specific value (e.g.,
null or a sentinel value) to indicate that the item is not present in the tree.

Example:
void Retrieve(TreeNode* tree,
ItemType& item, bool& found){
if (tree == NULL)
found = false; // item is not found.
else if (item < tree->info)
Retrieve(tree->left, item, found); // Search left subtree.
else if (item > tree->info)
Retrieve(tree->right, item, found);// Search right subtree.
}
else {
item = tree->info;
found = true;
}
void TreeType::RetrieveItem(ItemType& item, bool& found) const{
Retrieve(root, item, found);
}

The Function InsertItem


⮚ InsertItem: This function inserts a new node with the given item/key into the binary search tree
while maintaining the order property of the tree. It traverses the tree to find the appropriate
position for insertion and places the new node accordingly.

Example:
void Insert(TreeNode*& tree, ItemType item)
// Inserts item into tree.
if (tree == NULL)
{// Insertion place found. tree = new TreeNode;
tree->right = NULL;
tree->left = NULL;
tree->info = item;
}
else if (item < tree->info)
}
Insert(tree->left, item);
else
Insert(tree->right, item);

9
}

The Function DeleteItem


⮚ DeleteItem: This function removes a node containing a specific item/key from the binary search
tree while preserving the binary search tree property. It handles cases where the node to be
deleted might have children.

Example:
void Delete(TreeNode*& tree, ItemType item){
if (item < tree->info)
Delete(tree->left, item);
else if (item > tree->info)
// Look in left subtree.
Delete(tree->right, item); // Look in right subtree.
else
DeleteNode(tree); // Node found; call DeleteNode.
}
void TreeType::DeleteItem(ItemType item)
{
Delete(root, item);
}

The Function Print


⮚ Print: This function prints the contents of the binary search tree in a specific order, such as in-
order, pre-order, or post-order traversal. It allows you to visualize the structure of the tree or the
sequence of items stored in it.

Example:
void PrintTree(TreeNode* tree, std::ofstream& outFile)
{
if (tree != NULL)
{
PrintTree(tree->left, outFile); // Print left subtree.
outFile << tree->info;
PrintTree(tree->right, outFile); // Print right subtree.
}}
void TreeType::Print(std::ofstream& outFile) const

{
PrintTree(root, outFile);
}

10
The Class Constructor and Destructor
⮚ Constructor: Initializes the binary search tree, often setting the root node to nullptr or any initial
values.
⮚ Destructor: Releases memory and resources occupied by the binary search tree when it goes out
of scope or is explicitly destroyed.

Example:
void Destroy(TreeNode*& tree);
TreeType::~TreeType()
// Calls recursive function Destroy to destroy the tree.
{
Destroy(root);
}
void Destroy(TreeNode*& tree)
// Post: tree is empty; nodes have been deallocated.
{
if (tree != NULL)
{
Destroy(tree->left);
Destroy(tree->right);
delete tree;
}
}

The Functions ResetTree and GetNextItem


⮚ ResetTree: This function resets the tree to an empty state, deallocating all nodes and resetting
the root.
⮚ GetNextItem: This function, when used with an iterator, retrieves the next item in the binary
search tree in a specific order (e.g., in-order traversal), allowing for sequential access to items in
the tree.

Example:
//Reset the binary search tree
void ResetTree(Node* root) {
if (root == nullptr) {
return;
}

ResetTree(root->left); // Recursively clear left subtree

11
ResetTree(root->right); // Recursively clear right subtree

delete root; // Delete the current node


root = nullptr;
}
// Get the next item in in-order traversal
Node* GetNextItem(Node* root) {
if (root == nullptr) {
return nullptr;
}

Node* current = root;


while (current->left != nullptr) {
current = current->left;
}

return current;
}

conclusion
In conclusion, BSTs are versatile data structures that provide efficient searching capabilities, making
them invaluable in numerous software applications. Their effectiveness lies in their ability to maintain
ordered data and perform operations swiftly, underscoring their significance in modern programming
and software development.

As technology evolves, further research and enhancements in balancing techniques and optimizing BST
operations will continue to refine their efficiency and applicability across various domains.

12

You might also like