-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.c
More file actions
56 lines (42 loc) · 850 Bytes
/
Tree.c
File metadata and controls
56 lines (42 loc) · 850 Bytes
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
/*
*Batch Number 6
*Abhinav Bhatia (2011A7PS371P)
*Mukul Bhutani (2011A7PS343P)
*/
#include "Tree.h"
#include <stdlib.h>
Tree tree_createNew()
{
Tree t;
t.root = NULL;
t.totalLeafNodes = 0;
t.totalNodes = 0;
return t;
}
Tree tree_add(Tree t, TreeLink parent, void* value)
{
TreeNode* newNode;
if (parent == NULL && t.totalNodes != 0) return t;
newNode = (TreeNode*)malloc(sizeof(TreeNode));
newNode->data = value;
newNode->no_of_children = 0;
newNode->parent = parent;
if (t.totalNodes > 0)
{
newNode->childID = parent->no_of_children;
parent->children[parent->no_of_children] = newNode;
parent->no_of_children++;
if (parent->no_of_children > 1)
{
t.totalLeafNodes++;
}
}
else
{
newNode->childID = 0;//or should it be -1
t.root = newNode;
t.totalLeafNodes = 1;
}
t.totalNodes++;
return t;
}