Turi Create  4.0
fp_node.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_FP_NODE_H
7 #define TURI_FP_NODE_H
8 
9 #include <vector>
10 #include <list>
11 #include <map>
12 #include <string>
13 #include <memory>
14 #include <algorithm>
15 
16 #include <core/data/sframe/gl_sframe.hpp>
17 #include <core/data/sframe/gl_sarray.hpp>
18 
19 #include <toolkits/feature_engineering/topk_indexer.hpp>
20 #include <toolkits/feature_engineering/statistics_tracker.hpp>
21 
22 namespace turi{
23 namespace pattern_mining {
24 
25 const size_t ROOT_ID = -1; // item id reserved for root node
26 
27 /**
28  * Nodes for the FP-Tree data structure
29  *
30  * Note: FP-Nodes should only be created via shared pointers
31  * std::make_shared<fp_node>(size_t id)
32  *
33  * Members:
34  * item_id is the size_teger index of items in the tree (e.g. 0 -> Dog, 1 -> Cat)
35  * Note: m_id = -1 should be reserved for the root node.
36  * item_count is the frequency of the index (e.g. 'Dog' occurs m_count times)
37  * depth is the depth of the node in the tree
38  * parent_node is the node's parent in the tree
39  * children_node are the node's children in the tree
40  * next_node is the next location of a node with the same item_id
41  */
42 class fp_node {
43  public:
44  size_t item_id;
45  size_t item_count;
46  size_t depth;
47  bool is_closed_node;
48  fp_node* next_node = nullptr;
49  fp_node* parent_node = nullptr;
50  std::vector<std::shared_ptr<fp_node>> children_nodes;
51 
52  // Constructor
53  /**
54  * Construct New Node
55  * Args:
56  * id (size_t) - item id for node (Note: ROOT_ID is reserved for root)
57  * depth (size_t) - depth of node in tree
58  */
59  fp_node(const size_t& id = ROOT_ID, const size_t& node_depth = 0);
60 
61  // Set Function
62  /**
63  * Add Child Node
64  * Adds a new child node with id if one does not currently exist.
65  * Current node will be set as child node's parent.
66  * Args:
67  * child_id (size_t) - id for child node (Note: ROOT_ID is reserved)
68  * Returns:
69  * A pointer to the child node
70  */
71  fp_node* add_child(const size_t& child_id);
72 
73  /**
74  * Get Child Node
75  * Gets a pointer to the child node with id
76  * Args:
77  * child_id (size_t) - id for child node (Note: ROOT_ID is reserved)
78  * Returns:
79  * A pointer to the child node, nullptr if no child exists
80  */
81  fp_node* get_child(const size_t& child_id) const;
82 
83  /**
84  * Get set on path to root from current node.
85  * Returns:
86  * set (vector of size_t) - set of item ids on path to root
87  */
88  std::vector<size_t> get_path_to_root();
89 
90  /**
91  * Check if node is a closed node.
92  * A node is closed if it's support is greater than any of its children.
93  * Once closed, a node will remain closed.
94  *
95  * Returns:
96  * is_closed (bool) - whether the node is closed
97  */
98  bool is_closed() const;
99 
100  /**
101  * Delete pointer to self from parent (if a parent exists)
102  */
103  void erase();
104 
105 };
106 
107 } // pattern_mining
108 } // turicreate
109 #endif
fp_node(const size_t &id=ROOT_ID, const size_t &node_depth=0)
std::vector< size_t > get_path_to_root()
fp_node * get_child(const size_t &child_id) const
fp_node * add_child(const size_t &child_id)