Found 33676 Articles for Programming

C++ Program to Implement Dijkstra’s Algorithm Using Set

Nancy Den
Updated on 30-Jul-2019 22:30:25

630 Views

This is a C++ Program to Implement Dijkstra’s Algorithm using Set. Here we need to have two sets. We generate a shortest path tree with given source node as root. One set contains vertices included in shortest path tree and other set includes vertices not yet included in shortest path tree. At every step, we find a vertex which is in the other set (set of not yet included) and has minimum distance from source.Algorithm:Begin    function dijkstra() to find minimum distance:    1) Create a set Set that keeps track of vertices included in shortest    path tree, Initially, ... Read More

Exception Handling in C++ vs Java

Aman Kumar
Updated on 30-May-2025 18:40:08

720 Views

In both languages, exception handling is used to identify errors using the try, catch, and throw keywords. Exception handling is a programming mechanism that works with errors and unexpected events that occur during program execution. Exception Handling in C++ C++ provides an inbuilt feature for handling exceptions using try and catch block. It is an exception-handling mechanism; the code that has an exception is placed inside the try block, and the code that handles the exception is placed inside the catch block. Visit here to learn more about C++ Exception. Syntax Following is the syntax of C++ Exceptions: try { ... Read More

Set position with seekg() in C++ language file handling

Aman Kumar
Updated on 30-May-2025 18:39:14

4K+ Views

The setg() function sets the position of the file pointer in the file. C++ seekg() Function The seekg() is a function in the iostream library that allows us to seek an arbitrary position in a file. It is mainly used to set the position of the next character to be extracted from the input stream from a given file in C++ file handling. Syntax There are two types of syntax of seekg() function: istream&seekg(streampos position); Or, istream&seekg(streamoff offset, ios_base::seekdir dir); Parameters Following is the parameter of the seekg(): position: It is the new ... Read More

C++ Program to Implement Splay Tree

Aman Kumar
Updated on 30-May-2025 18:39:36

3K+ Views

Splay Tree Splay tree is a self-balanced binary searched tree. The idea of implementing the splay tree is to bring the most recently inserted element to the root of the tree by performing a sequence of tree rotations, called splaying. Following are the basic operation on AVL: Insertion Searching Deletion Rotation: There are two types of rotation in splay tree (zig rotation and zag rotation). Let's see the code snippet of the above operation: Insertion Efficiently inserts a new key into the ... Read More

C++ Program to Implement self Balancing Binary Search Tree

Aman Kumar
Updated on 29-May-2025 15:44:52

2K+ Views

Self Balancing Binary Search tree A self-balancing binary search tree (BST) is a height-balanced binary search tree that automatically keeps its height (the maximum number of levels below the root) as small as possible when insertion and deletion operations are performed on the tree. In the self-balancing binary search tree, the height is maintained in the order of O(logn), so that all operations take O(logn) time on average. Common examples of self-balancing binary search trees are: AVL Tree RED Black Tree Splay Tree AVL Tree An ... Read More

C++ Program to Implement Randomized Binary Search Tree

Aman Kumar
Updated on 29-May-2025 15:45:27

712 Views

A Randomized Binary Search Tree (RBST) is a variation of a Binary Search Tree (BST) that contains randomization to maintain balance and improve efficiency. One common implementation of an RBST is a Treap, which combines BST properties with heap properties. Why We Use Random Binary Tree Random binary trees are used for analyzing the average-case complexity of data structure based on the binary trees. Features of RBST Following are the features of the randomized binary tree: BST Properties: The left subtree contains smaller value, right subtree contains larger value. Randomization: ... Read More

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

456 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

Advertisements