1672364808
Spanning Tree Protocol (STP) Explained. You heard the words Spanning Tree, now it is time to understand what it is all about. This is a tutorial that will start from the very beginning of spanning tree and you will walk away with how it begins.
We start with the very beginning, discussing how L3 uses a mechanism to prevent loops. We quickly find out that L2 does not have this mechanism and why Spanning Tree is needed. Then we enter the world of Spanning Tree. Exploring how spanning tree forms it topology and the election processes.
From there we enter into a step by step process, understanding how Spanning Tree starts to build its own topology. We learn about the Root Bridge, Root Ports, Designated Ports, Blocking ports, and so on.
If you want to really understand Spanning Tree, this is where you begin.
#cisco #ccna
1598227320
Given a Generic tree, the task is to delete the leaf nodes from the tree.
** Examples:**
Input:
5
/ / \ \
1 2 3 8
/ / \ \
15 4 5 6
Output:
5 : 1 2 3
1 :
2 :
3 :
Explanation:
Deleted leafs are:
8, 15, 4, 5, 6
Input:
8
/ | \
9 7 2
/ | \ | / / | \ \
4 5 6 10 11 1 2 2 3
Output:
8: 9 7 2
9:
7:
2:
**Approach: **Follow the steps given below to solve the problem
Below is the implementation of the above approach:
#data structures #recursion #tree #n-ary-tree #tree-traversal #data analysis
1599043260
Given a Binary Tree and an integer D, the task is to check if the distance between all pairs of same node values in the Tree is ? D or not. If found to be true, then print Yes. Otherwise, print No.
Examples:
Input:_ D = 7 _
1
/ \
2 3
/ \ / \
4 3 4 4
Output:_ Yes _
Explanation:
_The repeated value of nodes are 3 and 4. _
_The distance between the two nodes valued 3, is 3. _
_The maximum distance between any pair of nodes valued 4 is 4. _
Therefore, none of the distances exceed 7
Input:_ D = 1 _
3
/ \
3 3
\
3
Output:_ No _
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
**Approach: **
The idea is to observe that the problem is similar to finding the distance between two nodes of a tree. But there can be multiple pairs of nodes for which we have to find the distance. Follow the steps below:
#greedy #recursion #searching #tree #binary tree #frequency-counting #postorder traversal #tree-traversal
1592667300
The Minimum Spanning Tree connects all vertices utilizing the minimum path. This is just a quick intro since we will cover MSTs in detail in a couple of later articles, but we’ll show a quick example of what an MST looks like. We’re not going to take any algorithmic approach, but instead use intuition in this example.
We can see that there are two negative edges in this graph, so most likely they’ll be part of the minimum spanning tree. We’ll start by adding those edges.
#programming #computer-science #minimum-spanning-tree #algorithms #developer
1672364808
Spanning Tree Protocol (STP) Explained. You heard the words Spanning Tree, now it is time to understand what it is all about. This is a tutorial that will start from the very beginning of spanning tree and you will walk away with how it begins.
We start with the very beginning, discussing how L3 uses a mechanism to prevent loops. We quickly find out that L2 does not have this mechanism and why Spanning Tree is needed. Then we enter the world of Spanning Tree. Exploring how spanning tree forms it topology and the election processes.
From there we enter into a step by step process, understanding how Spanning Tree starts to build its own topology. We learn about the Root Bridge, Root Ports, Designated Ports, Blocking ports, and so on.
If you want to really understand Spanning Tree, this is where you begin.
#cisco #ccna
1597978800
Given a string** S** of Size N. Initially, the value of count is 0. The task is to find the value of count after N operations to remove all the N characters of the given string S where each operation is:
Note: Consider string as 1-based indexing.
Examples:
Input:_ N = 5, S = “abcab” _
Output:_ 8 _
Explanation:
_Remove character ‘a’ then string becomes “bcab” and the count = 1. _
_Remove character ‘a’ then string becomes “bcb” and the count = 1 + 3 = 4. _
_Remove character ‘b’ then string becomes “cb” and the count = 4 + 1 = 5. _
_Remove character ‘b’ then string becomes “c” and the count = 5 + 2 = 7. _
Remove character ‘c’ then string becomes “” and the count = 7 + 1 = 8.
Input:_ N = 5 S = “aabbc” _
Output:_ 5 _
Explanation:
The value after 5 operations to remove all the 5 characters of String aabbc is 5.
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
**Naive Approach: **The idea is to check if the string is empty or not. If it is not empty then following are the steps to solve the problem:
Below is the implementation of the above approach:
// C++ program for the above approach
#include <iostream>
#include <string>
**using**
**namespace**
std;
// Function to find the value after
// N operations to remove all the N
// characters of string S
**void**
charactersCount(string str,
**int**
n)
{
**int**
count = 0;
// Iterate till N
**while**
(n > 0) {
**char**
cur = str[0];
**int**
ind = 0;
**for**
(``**int**
j = 1; j < n; j++) {
**if**
(str[j] < cur) {
cur = str[j];
ind = j;
}
}
// Remove character at ind and
// decrease n(size of string)
str.erase(str.begin() + ind);
n--;
// Increase count by ind+1
count += ind + 1;
}
cout << count << endl;
}
// Driver Code
**int**
main()
{
// Given string str
string str =
"aabbc"``;
**int**
n = 5;
// Function call
charactersCount(str, n);
**return**
0;
}
Output:
5
Time Complexity:_ O(N2)_
Auxiliary Space:_ O(1)_
**Efficient Approach: **This problem can be solved using the concept of Binary Index Tree or Fenwick Tree. Below are the steps:
#advanced data structure #divide and conquer #strings #tree #segment-tree #trees