-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.cpp
101 lines (94 loc) · 2.73 KB
/
trie.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Created by harry7 on 30/8/17.
*
* This file contains the Tree implementation defined in `trie.h`
* and will be used to Apriori algorithm
*/
#include <bits/stdc++.h>
#include "trie.h"
/**
* Default constructor
*/
Trie::Trie() {
root = NULL;
}
/**
* constructs a new node and returns its pointer
*
* @param key key of the new node
* @param num_child number of children to be present in new node
* @return pointer to the node that is created
*/
Node *Trie::newNode(int key, int num_child) {
Node *node = (Node *) malloc(sizeof(node));
node->key = key;
node->num_child = num_child;
node->next = (Node **) malloc(num_child * sizeof(Node *));
for (int i = 0; i < num_child; i++) {
node->next[i] = NULL;
}
return node;
}
/**
* inserts a new node as a child at the specified index at a node if it is not present already
* and returns the pointer to that node
*
* @param root parent node
* @param key key at which child has to be inserted
* @param num_child number of children to be present at the child node
* @return pointer to the node at the specified index of the parent node
*/
Node *Trie::insertOne(Node *root, int key, int num_child) {
int index = key - root->key - 1;
if (root->next[index] == NULL) {
Node *node = newNode(key, num_child);
root->next[index] = node;
}
return root->next[index];
}
/**
* given set of items and a parent node insert all the elements at appropriate
* indices of tree
*
* @param root node at which item hierarchy starts
* @param item_set set of items to be inserted into the tree
* @param k length of item set
* @param limit limit on the number of children it can have
* @param count
*/
void Trie::insertAll(Node *root, vector<int> item_set, int k, int limit, int count) {
for (int i = 0; i < k; i++) {
root = insertOne(root, item_set[i], limit - item_set[i]);
}
root->count = count;
}
/**
* Insert the item set into the given tree
*
* @param item_set item set to be inserted
* @param limit limit specifying number of children that node can have
* @param count frequency of the item set
*/
void Trie::insert(vector<int> item_set, int limit, int count) {
insertAll(root, item_set, int(item_set.size()), limit, count);
}
/**
* Given an item set return its count
*
* @param item_set item set for which count is to be obtained
* @return count of the item set
*/
int Trie::getCount(vector<int> item_set) {
Node *tmp = root;
int len = (int) item_set.size();
int read = 0;
for (int i = 0; i < len; i++) {
int index = item_set[i] - read - 1;
if (tmp->next[index] == NULL) {
return 0;
}
tmp = tmp->next[index];
read = item_set[i];
}
return tmp->count;
}