tree.h
Go to the documentation of this file.
1 
9 #ifndef _TREE_H_
10 #define _TREE_H_
11 
12 #include <stdio.h>
13 
14 #include "vector.h"
15 
20 #define max(a,b) ((a) > (b) ? (a) : (b))
21 
25 typedef struct Tree {
27  int id;
29  char* str;
31  Vector(struct Tree) children;
32 } Tree;
33 
41 Tree* createLeaf(int id, char* str);
42 
51 Tree* createNode(int id, char* str, Tree* child);
52 
60 Tree* addChild(Tree* node, Tree* child);
61 
68 int isLeaf(Tree* t);
69 
76 int height(Tree* t);
77 
85 int depth(Tree* root, int id);
86 
101 Tree* LCA(Tree* root, int id1, int id2);
102 
107 void freeTree(Tree* t);
108 
117 int isNodeDepthSameOrSmaller(Tree* t, int node1, int node2);
118 
119 #endif // _TREE_H_
char * str
The name of this node or leaf.
Definition: tree.h:29
int isLeaf(Tree *t)
Check whether the tree is a leaf or not.
Definition: tree.c:44
Tree * createNode(int id, char *str, Tree *child)
Create a new node.
Definition: tree.c:29
Tree * LCA(Tree *root, int id1, int id2)
Find the lowest common ancestor We traverse from root to leaf. When we find a node matching at least ...
Definition: tree.c:99
Tree * createLeaf(int id, char *str)
Create a new leaf.
Definition: tree.c:20
Tree * addChild(Tree *node, Tree *child)
Add a child to a node.
Definition: tree.c:39
int height(Tree *t)
Get the height of a tree.
Definition: tree.c:61
Vector(struct Tree) children
All the children of this node (nothing means leaf)
int id
The unique identifier of this node or leaf.
Definition: tree.h:27
Contains the definition of the vectors (dynamic & generic arrays)
void freeTree(Tree *t)
Free the tree.
Definition: tree.c:133
int depth(Tree *root, int id)
Get the depth of a node in the tree.
Definition: tree.c:74
Defines the n-ary tree.
Definition: tree.h:25
int isNodeDepthSameOrSmaller(Tree *t, int node1, int node2)
Is node1 have same or smaller depth that node2?
Definition: tree.c:141