Questions tagged [n-ary-tree]
38 questions
1
vote
1 answer
Can't wrap my head around populating an n-ary tree
I am working on a homework project where I read a list of connecting stations from a file and create a hashmap in the format (key = String station, value = ArrayList connecting stations) so far so good.
The user can then select a home station at…

Astabh
- 59
- 2
- 11
0
votes
1 answer
Copy constructor of N-ary tree in C++
I have the following header class:
class Tree
{
private:
string label;
vector children;
void free(Tree* tree);
Tree* copyFrom(const Tree* other);
public:
Tree(string _label, vector _children= vector());
…

ManGoose
- 25
- 5
0
votes
1 answer
Print sum of nodes' level of a n-ary tree in C
I need to find a solution for this problem:
I have a n-ary tree structured in this way:
struct kTreeVertex {
int key;
struct kTreeVertex* child;
struct kTreeVertex* sibling;
};
typedef struct kTreeVertex* kTree;
I…

hawk
- 28
- 5
0
votes
1 answer
How to deep copy an N-Ary tree in kotlin
If you want to create a deep copy of a tree (so that each node in the tree is deep-copied), you have to use a recursive algorithm provided in the answer below.
I have not found such an article on SO, regarding realization of this in Kotlin, let…

Tim Kochetkov
- 149
- 1
- 11
0
votes
2 answers
N-ary tree in C, struggling with double pointers node connection
I am stuck with a leetcode problem on N-ary tree since last few hours. Could anyone please help me?
struct Node {
int val;
int numChildren;
struct Node **children;
};
typedef struct Node node;
node *new_node(int numC, int data) {
node…

Amar
- 15
- 3
0
votes
1 answer
What is the time complexity of N-ary Tree Level Order Traversal. Is it always O(N) irrespective of no. of loops used?
Let us consider the following code. I am trying to create a resultant vector containing each level of an n-ary tree in a separate inner vector. What can be time complexity for the following code?
// Definition for a Node.
class Node {
public:
…
0
votes
1 answer
Build n-ary tree from txt file
I'm doing a homework for university. Is the first time I use tree as a data structure and I don't understand how to implement it in Java.
I have to read from a .txt file a string like this
( 1 ( 2 ( 5 ( 13 ) 6 7 ) 3 ( 8 9 ) 4 ( 10 11 12 ) ) )
where…

giasco
- 1
- 2
0
votes
1 answer
Create a k-ary tree from a list of coordinates and list of edges connecting them.
I have a list of Nodes/Vertices, and a list of lines/edges connecting these nodes.The lists are not sorted or ordered in any way, but contain all the edges and nodes for a particular data set.
The edges are line segments defined by Cartesian…

Cudzie Mavhunga
- 1
- 3
0
votes
0 answers
Data structure, tree, n-ary tree, DFS
I am solving a simple question based on n-ary tree. Question is simple, finding total number of hand shaking between soldiers where each node of tree represents soldiers. Only ancestors nodes can…

Hritik Kumar
- 89
- 2
- 6
0
votes
1 answer
N-Ary Tree C++ - How to find the level of a node
I would like to returns the level of a given node. I've been able to do this for binary trees but for n-ary trees there is no way to run it. Any ideas ?
For the binary tree the solution was:
int findLevel(BinAlbero::node root,…

Antonio M
- 1
- 2
0
votes
1 answer
number of nodes for a level in a first child - next sibling tree
I have to write a function to find the number of nodes in a level of a first child - next sibling N-ary tree.
My function is:
int nodesAtLevel(NTree root, int level) {
if (root == NULL) {
return 0;
}
if (level == 0) {
…

J.D.
- 105
- 6
0
votes
1 answer
Find element in a n-Ary tree
Given the following structure
struct nNode {
int val;
struct nNode parent;
struct nNode children;
struct nNode next;
struct nNode prev;
};
Where the children points to first child and to traverse to the other childs we need to…

Lin
- 1,145
- 11
- 28
0
votes
1 answer
get path to the children from parent
I have a list of children and a list of parents. I also have a map of childeId-parentId. Parents can have n number of children but children have one immediate parent.
I want to get the path to each child from parent in Java. How can I do it…

Akshay Talathi
- 83
- 1
- 2
- 13
0
votes
1 answer
Python - Flat list tree implementation: Given child, get parent?
I'm creating a python class for a tree in which each node has a number of children given by "order" (but each child only has one node). I have a method, children(self,i), which returns the children of a node at index i. I need to implement…

Nicole R
- 3
- 3
0
votes
1 answer
Source for studying data structures
Does anybody know a good source where to study data structures? In particular i am looking for trees and graphs. I've already tried geekforgeeks but i wonder if there is something else as good as it.
Thank's.

J.D.
- 105
- 6