Total nodes traversed in Euler Tour Tree
Euler tour of tree has been already discussed which flattens the hierarchical structure of tree into array which contains exactly 2*N-1 values. In this post, the task is to prove that the degree of Euler Tour Tree is 2 times the number of nodes minus one. Here degree means the total number of nodes get traversed in Euler Tour.
Examples:
Input:
Output: 15
Input:
Output: 17
Explanation:
Using Example 1:

where

It can be seen that each node’s count in Euler Tour is exactly equal to the out-degree of node plus 1.
Mathematically, it can be represented as:
Where
Total represents total number of nodes in Euler Tour Tree
represents ith node in given Tree
N represents the total number of node in given Tree
represents number of child of
![]()
// C++ program to check the number of nodes // in Euler Tour tree. #include <bits/stdc++.h> using namespace std; #define MAX 1001 // Adjacency list representation of tree vector<int> adj[MAX]; // Function to add edges to tree void add_edge(int u, int v) { adj[u].push_back(v); } // Program to check if calculated Value is // equal to 2*size-1 void checkTotalNumberofNodes(int actualAnswer, int size) { int calculatedAnswer = size; // Add out-degree of each node for (int i = 1; i <= size; i++) calculatedAnswer += adj[i].size(); if (actualAnswer == calculatedAnswer) cout << "Calculated Answer is " << calculatedAnswer << " and is Equal to Actual Answer\n"; else cout << "Calculated Answer is Incorrect\n"; } int main() { // Constructing 1st tree from example int N = 8; add_edge(1, 2); add_edge(1, 3); add_edge(2, 4); add_edge(2, 5); add_edge(3, 6); add_edge(3, 7); add_edge(6, 8); // Out_deg[node[i]] is equal to adj[i].size() checkTotalNumberofNodes(2 * N - 1, N); // clear previous stored tree for (int i = 1; i <= N; i++) adj[i].clear(); // Constructing 2nd tree from example N = 9; add_edge(1, 2); add_edge(1, 3); add_edge(2, 4); add_edge(2, 5); add_edge(2, 6); add_edge(3, 9); add_edge(5, 7); add_edge(5, 8); // Out_deg[node[i]] is equal to adj[i].size() checkTotalNumberofNodes(2 * N - 1, N); return 0; } |
Output:
Calculated Answer is 15 and is Equal to Actual Answer Calculated Answer is 17 and is Equal to Actual Answer
Recommended Posts:
- Euler Tour of Tree
- Euler tour of Binary Tree
- Euler Tour | Subtree Sum using Segment Tree
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Total sum except adjacent of a given node in a Binary Tree
- Subtree of all nodes in a tree using DFS
- Sum of all nodes in a binary tree
- XOR of all the nodes in the sub-tree of the given node
- Sum of all leaf nodes of binary tree
- Product of all nodes in a Binary Tree
- Convert a tree to forest of even nodes
- XOR of path between any two nodes in a Binary Tree
- Sum of all the Boundary Nodes of a Binary Tree
- Count greater nodes in AVL tree
- Print all leaf nodes of an n-ary tree using DFS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.






![Rendered by QuickLaTeX.com $\displaystyle Total=\sum_{node_i=1}^{N} Out_D_e_g[node_i]+1$](https://web.archive.org/web/20190818170221im_/https://www.geeksforgeeks.org/wp-content/ql-cache/quicklatex.com-da778cc570cababd54720825873f0572_l3.png)
![Rendered by QuickLaTeX.com $\displaystyle Total= N + \sum_{node_i=1}^{N} Out_D_e_g[node_i]$](https://web.archive.org/web/20190818170221im_/https://www.geeksforgeeks.org/wp-content/ql-cache/quicklatex.com-388306a5c73ce80e35623dc52949b630_l3.png)