-
Notifications
You must be signed in to change notification settings - Fork 0
/
FPTree.cpp
117 lines (90 loc) · 3.57 KB
/
FPTree.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
102
103
104
105
106
107
108
109
110
111
112
113
//
// FPTree.cpp
// cs412_text-fp-mining
//
// Created by Yvette Luo on 10/21/13.
// Copyright (c) 2013 Yvette Luo. All rights reserved.
//
#include "FPTree.h"
void FPTree::AddTransaction(std::vector<std::pair<int, int>>& transaction){
FPNode* currParent = root;
for(int i = 0; i < transaction.size(); ++i) {
int item = transaction[i].first;
FPNode* currNode = currParent->GetChildByData(item);
//if currNode doesn't exist, insert it into the tree
if(currNode == NULL){
//create a new node and set data & parent
FPNode* newNode = new FPNode();
newNode->data = item;
newNode->parent = currParent;
//link new node to its parent
currParent->children.push_back(newNode);
//check single/multiple paths
if(containSinglePath && currParent->children.size() > 1) {
containSinglePath = false;
}
//replace parent with the new node for next iteration
currParent = newNode;
//update headerlist
if(headerNodeLink.find(item) == headerNodeLink.end()) {
//if not, create new pair (int node) and insert it into map
headerNodeLink[item] = newNode;
} else{
//otherwise, update the corresponding link
FPNode* currLink = headerNodeLink[item];
while(currLink->nodeLink != NULL) {
currLink = currLink->nodeLink;
}
currLink->nodeLink = newNode;
}
} else {
++currNode->count;
currParent = currNode;
}
}
}
void FPTree::AddPrefixPath(std::vector<FPNode*>& prefixPath,
std::map<int,int>& oneItemSetBeta,
int minSupport) {
FPNode* currParent = root;
int pathSupport = prefixPath[0]->count;
for(int i = prefixPath.size() - 1; i >= 1; --i) {
int item = prefixPath[i]->data;
if(oneItemSetBeta[item] < minSupport) {
continue;
}
FPNode* currNode = currParent->GetChildByData(item);
if(currNode == NULL) {
FPNode* newNode = new FPNode();
newNode->data = item;
newNode->parent = currParent;
newNode->count = pathSupport;
currParent->children.push_back(newNode);
//check single/multiple paths
if(containSinglePath && currParent->children.size() > 1) {
containSinglePath = false;
}
currParent = newNode;
//update headerlist
if(headerNodeLink.find(item) == headerNodeLink.end()) {
headerNodeLink[item] = newNode;
} else {
FPNode* currLink = headerNodeLink[item];
while(currLink->nodeLink != NULL) {
currLink = currLink->nodeLink;
}
currLink->nodeLink = newNode;
}
} else {
currNode->count += pathSupport;
currParent = currNode;
}
}
}
bool decreasingByValue(std::pair<int, int> a, std::pair<int, int> b){
return a.second > b.second;
}
void FPTree::CreateHeaderList(std::map<int,int> oneItemSet) {
headerList = std::vector<std::pair<int,int>>(oneItemSet.begin(), oneItemSet.end());
sort(headerList.begin(), headerList.end(), decreasingByValue);
}