-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaryTree.c
57 lines (51 loc) · 1.18 KB
/
naryTree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* Group 3
Ashrya Agrawal 2018A7PS0210P
Kalit Naresh Inani 2018A7PS0207P
Prajwal Gupta 2018A7PS0231P */
#include <stdio.h>
#include <stdlib.h>
#include "naryTree.h"
TreeNode *initialiseParseTree()
{
TreeNode *node = createNonLeafNode(s);
node->parent = NULL;
node->depth = 0;
return node;
}
TreeNode *createLeafNode(Terminal terminal)
{
TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
node->isLeaf = 1;
node->terminal = terminal;
node->next = NULL;
node->child = NULL;
return node;
}
TreeNode *createNonLeafNode(NonTerminal nonterminal)
{
TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
node->isLeaf = 0;
node->nonterminal = nonterminal;
node->child = NULL;
node->next = NULL;
return node;
}
TreeNode *nthSibling(TreeNode *child, int n)
{
TreeNode *curr = child;
for (int i = 0; i < n; i++)
{
if (curr == NULL)
{
printf("Index out of Bounds Error for N-Ary Tree\n");
exit(1);
}
curr = curr->next;
}
return curr;
}
TreeNode *nthChild(TreeNode *root, int n)
{
TreeNode *child = root->child;
return nthSibling(child, n);
}