Implementing the sets without C++ STL containers28 Aug 2024 | 5 min read A set is defined as a collection of elements where each element is unique. It is different from arrays as sets can have variable lengths. The element added to a set once cannot be changed. If we want to add the same modified number, delete the number and add the modified one in the set. Following are the operations that can be performed on a set -
Implementation of sets using BSTThe set data structure internally implements BST (Binary Search Tree) data structure. Hence, we will add the elements in the tree and use this tree template to implement the Set.
In a tree, we have three members - the data of the node and the left and right pointers to the node. It is given as below - After creating a tree, we insert the nodes in the tree using the insert() function. In a BST, the data that is less than root lies on the left side of the tree and the greater one lies on the right side of the tree. The Function containsNode() used are for checking whether a node is present in the tree or not. The function inorder() is used to print the inorder traversal of the BST.
After we have created the BST for the internal working of the set, a set template is created to implement the BST. It has a root pointer node to store the data and the size variable returns the size of the set. The Set class has a default constructor to initialize the root of BST as NULL and a copy constructor to copy a set into another set. The function add() is used to add values in the set. It does not add the duplicate data in the set by calling the function containsNode(). If there is a new element it adds to the set. The function contains() checks if a particular element is present in the set or not. It internally calls containsNode() in the BST. The function displaySet() is used to print the set elements. It internally calls the inorder() function of the BST. The function getSize() returns the size of the set. Code Output A = { 1 2 3 }
A contains 3
A does not contain 4
Next TopicScope Resolution Operator in C++ |
What is runtime type information
? Runtime type information (RTTI), also known as runtime type identification (RTI), is a feature of several programming languages (such as C++, Object Pascal, and Ada) that makes data about an object's data type available at runtime. It is possible for runtime type information to be made...
4 min read
C++ Pre-processors
There are mainly four types of Pre-processor directives in C++ programming language, and they are: - Macros File Inclusion Conditional Compilation Other directives Macros The macros in C/C++ programming language are one of the most exciting concepts. These are the sentences written in C++ code with the help of #define, and whenever...
3 min read
C++ Program to find the number of Islands using DFS
A typical algorithmic issue that frequently arises in graph theory and image processing is the need for a C++ program to count the number of islands using Depth-First Search (DFS). In this article, we will discuss the C++ program to find the number of islands using...
5 min read
Finding a Peak Element in an Array in C++
Elements that are larger than the elements to them are called peak elements in an array. They can be very important in many different applications, such as dataset identification or algorithm optimization. In this article, we will learn how to find a peak element in...
4 min read
forward_list::reverse() in C++
The singly linked list forward_list has a unique set of member functions. Reverse() is one of these functions that is particularly useful for rearranging the elements in a list. We will examine the syntax, uses, and possible advantages of forward_list::reverse() in this post as we delve into...
4 min read
Why (non-const) global variables are evil in C++
In this article, you will learn about why global variables are evil in C++: Outside of any program function, global variables are defined and declared. For the duration of the program, they uphold their ideals. Throughout the program's execution, they are available. Global variables that are not const...
3 min read
std::stof in C++
Programming in a variety of fields, including systems programming, game development, and all in between, C++ is a strong and adaptable language. C++ has a number of functions for converting texts to numeric values and vice versa in order to handle numerical data effectively. The ability...
4 min read
Strxfrm() Function in C/C++
In this article, we will discuss the strxfrm() function in C/C++ with its syntax and examples. What is strxfrm() function? The strxfrm() function is a function in the C/C++ Library. It is used to insert the characters from the source string into the destination string after transforming them...
2 min read
Character Arithmetic in C and C++
In C and C++, character arithmetic involves arithmetic operations using characters and symbols. Characters are like numbers beneath, even though they are typically used for text. It implies that there are intriguing ways to deal with characters and to add and subtract from them. In this...
3 min read
out of range exception C++
An exception is a runtime error that tampers with the regular instructions that a program follows. It is an undesirable occurrence that is not anticipated to happen during the program's typical execution. One of the common scenarios where an out-of-range exception occurs is when accessing elements of...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India