Found 7197 Articles for C++

C++ Program to Implement Interval Tree

Aman Kumar
Updated on 28-May-2025 16:31:44

856 Views

What is an Interval Tree? An interval tree is a tree data structure that stores intervals. It helps us to efficiently find all intervals that overlap with a specific interval or point. The purpose is to enhance a self-balancing Binary Search Tree (BST) known as Interval Tree, which is similar to Red Black Tree, AVL Tree, and so on, with a set of intervals so that all operations can be performed in O(log n) time. Every node of interval trees stores the following details: i: An interval which is represented as a pair [start, end]. ... Read More

C++ Program to Implement Fusion Tree

Aman Kumar
Updated on 27-May-2025 16:33:56

661 Views

A fusion tree is a tree data structure that implements an associative array on w-bit integers. Here, W is the number of bits in the integer. A fusion tree is used to maintain the ordered set of elements. It uses a combination of a B-tree and a hash table that helps reduce the time complexity of the operations like insertion, deletion, and searching in the tree. How Fusion Tree Works? The following are the factors that should be considered while implementing the fusion tree: Bit manipulation: The tree extracts specific bits from stored integers and ... Read More

C++ Program to Implement Expression Tree Algorithm

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

457 Views

An expression tree is basically a binary which is used to represent expressions. In expression tree, internal nodes correspond to operators and each leaf node corresponds to an operand. Here is a C++ Program to implement the Expression Tree Algorithm which takes the postfix expression as an input and generates the corresponding expression tree traversed in inorder.AlgorithmBegin    function construct_expression_tree():       Flag = 1 when it is operand.       Flag = -1 when it is operator.       S = suffix[0] means read the first operand from the expression.    For i = 0 and ... Read More

Virtual Constructor in C++

Aman Kumar
Updated on 27-May-2025 16:31:50

23K+ Views

In C++, we cannot create a virtual constructor, this is because C++ is a statically typed language and the constructor is responsible for creating an object. So, the compiler needs to know the exact type of object at compile time. The virtual mechanism works only when we have a base class pointer to a derived class object. The constructor cannot be virtual, because when a constructor of a class is executed there is no virtual table in the memory, means no virtual pointer defined yet. So, the constructor should always be non-virtual. Implementation of Constructor & Destructor In the following ... Read More

Virtual Destructor in C++

Aman Kumar
Updated on 27-May-2025 16:32:51

2K+ Views

A virtual destructor is a destructor declared within the base class with a virtual keyword. In C++, destructors are special members of a class that frees memory occupied by an object when a memory leak occurs. Deleting a derived class object using a pointer to a base class, the base class should be defined with a virtual destructor. When to Use Virtual Destructor? Virtual destructorsare needed in scenarios where polymorphism and inheritance are involved, and instances of derived classes are managed by pointers to base classes. If our class has one or more virtual functions that are inherited from child ... Read More

Virtual functions in derived classes in C++

Aman Kumar
Updated on 26-May-2025 12:49:45

1K+ Views

Virtual Functions in Derived ClassesA virtual function is declared using the virtual keyword in the base class and becomes a member function of the base class overridden by the derived class. It becomes virtual in every class which is derived from the base class. So, the keyword virtual is not necessary in the derived class while declaring redefined versions of the virtual base class function. When we use a pointer or reference to the base class to refer to a derived class object, we can call its virtual function. Syntax Following is the syntax of the virtual function in ... Read More

Default arguments and virtual function in C++

Aman Kumar
Updated on 22-May-2025 18:28:36

325 Views

Default Argument A default argument is a value provided during the function declaration that can be automatically assigned if no argument is provided when the function is called. If a value is passed at the time of the function call, this default value is overridden, and the argument becomes a parametrized Argument. Syntax The syntax below shows how we can define a default argument function: int fun(int x = 0){ // function body } Example of Default Argument The following C++ example shows the working of the default argument: #include using namespace std; //function defined with ... Read More

Virtual Functions and Runtime Polymorphism in C++

Aman Kumar
Updated on 22-May-2025 18:11:33

6K+ Views

In C++, both virtual functions and runtime polymorphism are key features that enable dynamic behavior and code flexibility. Virtual Functions A virtual function is the member function that is declared in the base class using the keyword virtual and is overridden in the derived class. The virtual function enables runtime polymorphism means the function that will be executed is determined at runtime rather than at compile time. Characteristics of Virtual Function Following are the characteristics of the virtual function: Virtual function make sure the correct function is called for an object, regardless of the type ... Read More

C++ Program to Implement Sorted Singly Linked List

Aman Kumar
Updated on 22-May-2025 18:10:45

3K+ Views

A singly linked list is a type of linear data structure where each node contains two items: The data and a link to the next node in the list. Where, first node is called as head node and the last node is called the tail. So, you can traverse the linked list from the head node and continue until the link becomes null. Sorted Singly Linked List A singly linked list is supposed to be sorted when its elements are arranged in specific order (i.e., ascending or descending order), and each node contains data and a pointer (or reference) to ... Read More

C++ Program to Implement Sorted Doubly Linked List

Aman Kumar
Updated on 22-May-2025 18:12:31

2K+ Views

Sorted Doubly Linked List A sorted doubly linked list is a type of doubly linked list in which elements are arranged in a specific order, typically ascending or descending based on the data values. Where, insertion operation makes sure that the new node is placed in its correct sorted position. A doubly linked list is a two-way linked list in which a node is connected with two pointers, i.e., next and previous pointers, which are references to the next node and previous node, respectively. Characteristics of Sorted Doubly Linked List The following are the characteristics of the doubly linked ... Read More

Advertisements