
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Depth of an N-Ary tree in C++?
Let us first define the struct that would represent a tree node that contains a character key and a vector of Node *.
struct Node{ char key; vector<Node *> children; };
Next we create our createNode(int key) function that takes an int key value and assign it to the key member of the node. The function returns the pointer to the created struct node.
Node *createNode(int key){ Node *node = new Node; node->key = key; return node; }
Our depthOfTree(struct Node* root) function takes the root node as parameter. If the root is NULL then the depth is returned as 0.
int depthOfTree(struct Node *root){ if (root==NULL) return 0;
We then define maxDepth variable and assign its value to 0. We then iterate over all the children of the tree and increment maxDepth on each recursion. Once the base condition is met and the recursion is over we then return the maxDepth.
int depthOfTree(struct Node *root){ if (root==NULL) return 0; int maxDepth = 0; for(auto i: root->children){ maxDepth = depthOfTree(i) + 1; } return maxDepth; }
Example
Let us look at the following implementation to find the depth of the N-Ary tree −
#include <iostream> #include <vector> using namespace std; struct Node{ char key; vector<Node *> children; }; Node *createNode(int key){ Node *node = new Node; node->key = key; return node; } int depthOfTree(struct Node *root){ if (root==NULL) return 0; int maxDepth = 0; for(auto i: root->children){ maxDepth = depthOfTree(i) + 1; } return maxDepth; } int main(){ Node *root = createNode('S'); (root->children).push_back(createNode('O')); (root->children).push_back(createNode('A')); (root->children).push_back(createNode('D')); (root->children).push_back(createNode('N')); (root->children[0]->children).push_back(createNode('L')); (root->children[0]->children).push_back(createNode('I')); (root->children[2]->children).push_back(createNode('R')); (root->children[3]->children).push_back(createNode('C')); (root->children[3]->children).push_back(createNode('H')); (root->children[3]->children).push_back(createNode('I')); cout <<"The depth of the n-ary tree is "<< depthOfTree(root) << endl; return 0; }
Output
The above code will produce the following output −
The depth of the n-ary tree is 2
- Related Articles
- Depth of an N-Ary tree in C++ Program
- Mirror of n-ary Tree in C++
- Encode N-ary Tree to Binary Tree in C++
- N-ary Tree Preorder Traversal in C++
- Print all leaf nodes of an n-ary tree using DFS in C++
- N-ary Tree Level Order Traversal in C++
- Serialize and Deserialize N-ary Tree in C++
- Preorder Traversal of N-ary Tree Without Recursion in C++
- Find the Number of Ways to Traverse an N-ary Tree using C++
- Even size subtree in n-ary tree in C++
- Next Larger element in n-ary tree in C++
- Program to find length of the longest path in an n-ary tree in Python
- Number of leaf nodes in the subtree of every node of an n-ary tree in C++
- m-ary tree
- Program to make a copy of a n-ary tree in Python
