实例介绍
【实例简介】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
namespace fpTree
{
public class FPTreeTest
{
private readonly ButtonItem conditionTreeMode;
private readonly Table dataTable = new Table(new Point(1, 35), new Size(227, 350));
private readonly ButtonItem nextButton;
private readonly ButtonItem prevButton;
private readonly ButtonItem stepButton;
private readonly Table table = new Table(new Point(1, 420));
private readonly TreeModel treeMdel;
private List<TableHeadModel> HeadModels;
private List<TableHeadItem> HeadTables;
private List<List<string>> InputData;
private int MinSupportCount = 2;
private TreeModelItem Model;
private TreeNode Tree;
private int currentDataIndex = -1;
public FPTreeTest(Control parent)
{
Canvas = new ModelCanvas(parent);
table.AddColumn(new ColumnItem("Name"));
table.AddColumn(new ColumnItem("Count"));
table.AddColumn(new ColumnItem("Link"));
table.RowItemSelected = table_RowItemSelected;
treeMdel = new TreeModel(new Point(table.Width 10, 1));
conditionTreeMode = new ButtonItem(AnchorStyles.LeftTop, new Point(table.Width 5, 3));
conditionTreeMode.Text = "ConditionTreeMode";
conditionTreeMode.Size = new Size(160, 25);
conditionTreeMode.Click = ConditionTreeModeOnClick;
var buildButton = new ButtonItem(AnchorStyles.RightTop, new Point(920, 3));
buildButton.Text = "Build";
buildButton.Click = BuildButtonOnBuild;
prevButton = new ButtonItem(AnchorStyles.RightBottom, new Point(820, 615));
prevButton.Text = "Prev";
prevButton.IsEnabled = false;
prevButton.Click = prevButton_Click;
nextButton = new ButtonItem(AnchorStyles.RightBottom, new Point(920, 615));
nextButton.IsEnabled = false;
nextButton.Text = "Next";
nextButton.Click = nextButton_Click;
stepButton = new ButtonItem(AnchorStyles.RightBottom, new Point(720, 615));
stepButton.IsEnabled = false;
stepButton.Text = "Step";
stepButton.Click = stepButton_Click;
dataTable.AddColumn(new ColumnItem("TID"));
dataTable.AddColumn(new ColumnItem("Items", 150));
dataTable.RowItemSelected = dataTable_RowItemSelected;
Canvas.AddItem(dataTable);
Canvas.AddItem(table);
Canvas.AddItem(treeMdel);
Canvas.AddItem(conditionTreeMode);
Canvas.AddItem(buildButton);
Canvas.AddItem(prevButton);
Canvas.AddItem(nextButton);
Canvas.AddItem(stepButton);
Canvas.AddItem(new LabelItem(AnchorStyles.LeftTop, new Point(3, 5), "事务数据库"));
Canvas.AddItem(new LabelItem(AnchorStyles.LeftTop, new Point(3, 390), "倒序频繁项头表"));
}
protected ModelCanvas Canvas { get; set; }
public int CurrentDataIndex
{
get
{
return currentDataIndex;
}
set
{
currentDataIndex = value;
SetButtonState();
}
}
private void ConditionTreeModeOnClick(object sender, EventArgs eventArgs)
{
if (treeMdel != null)
{
treeMdel.SetConditionTreeMode();
conditionTreeMode.IsPressed = !conditionTreeMode.IsPressed;
}
}
private void dataTable_RowItemSelected(object sender, RowItemSelectedArgs e)
{
if (e.Selected != null)
{
var select = e.Selected.Tag as List<string>;
treeMdel.ClearSelectedDataItem();
treeMdel.SetCurrentDataItem(select);
}
}
private void stepButton_Click(object sender, EventArgs e)
{
CurrentDataIndex = -1;
nextButton.IsEnabled = true;
stepButton.IsEnabled = false;
OnBuild();
}
private void table_RowItemSelected(object sender, RowItemSelectedArgs e)
{
if (e.Selected != null)
{
RowItem row = e.Selected;
CellItem cell = row.Cells[row.Cells.Count - 1];
var select = e.Selected.Tag as TableHeadItem;
TableHeadModel find = HeadModels.Find(h => h.TableHead == select);
treeMdel.ClearSelectedHead();
treeMdel.SetCurrentHeadItem(find, cell.ItemCenter);
}
}
private void nextButton_Click(object sender, EventArgs e)
{
CurrentDataIndex ;
if (CurrentDataIndex > InputData.Count - 1)
{
CurrentDataIndex = InputData.Count - 1;
}
OnBuild();
}
private void prevButton_Click(object sender, EventArgs e)
{
CurrentDataIndex--;
if (CurrentDataIndex < 0)
{
CurrentDataIndex = 0;
}
OnBuild();
}
public event EventHandler Build;
public void OnBuild()
{
if (Build != null)
{
Build(this, EventArgs.Empty);
}
dataTable.SelectedIndex = CurrentDataIndex;
}
public void SetTreeModelData(List<List<string>> inputData)
{
if (inputData == null)
{
InputData = Initialize();
}
else
{
InputData = inputData;
}
dataTable.ClearRows();
for (int i = 0; i < InputData.Count; i )
{
List<string> data = InputData[i];
var item = new RowItem();
item.Tag = data;
item.AddCellItem(new CellItem("T" i));
string ditem = "";
for (int index = 0; index < data.Count; index )
{
string d = data[index];
ditem = d;
if (index < data.Count - 1)
{
ditem = ",";
}
}
item.AddCellItem(new CellItem(ditem,StringAlignment.Near));
dataTable.AddRow(item);
}
}
public void CreateTreeModel()
{
BuildHeads();
BuildFPTree();
table.ClearRows();
foreach (TableHeadItem headItem in HeadTables)
{
var item = new RowItem();
item.Tag = headItem;
item.AddCellItem(new CellItem(headItem.Name));
item.AddCellItem(new CellItem(headItem.Count.ToString(CultureInfo.InvariantCulture)));
item.AddCellItem(new CellItem(headItem.first == null ? "null" : headItem.first.Node.ToString()));
table.AddRow(item);
}
Model = LevelStats.GetTreeModel(Tree);
Model.Title = "null";
HeadModels = LevelStats.GetTableHeadModel(HeadTables, Model);
treeMdel.SetModel(Model);
treeMdel.SetMinSupportCount(MinSupportCount);
}
private void BuildButtonOnBuild(object sender, EventArgs eventArgs)
{
CurrentDataIndex = int.MaxValue;
stepButton.IsEnabled = true;
OnBuild();
}
private void SetButtonState()
{
if (currentDataIndex != int.MaxValue)
{
if (currentDataIndex > 0)
{
prevButton.IsEnabled = true;
}
else
{
prevButton.IsEnabled = false;
}
if (InputData != null && currentDataIndex < InputData.Count - 1)
{
nextButton.IsEnabled = true;
}
else
{
nextButton.IsEnabled = false;
}
}
}
private void BuildFPTree()
{
Tree = new TreeNode();
for (int i = 0; i < InputData.Count; i )
{
if (i > CurrentDataIndex)
{
return;
}
List<string> items = InputData[i];
List<string> sortitems = SortItems(items, HeadTables);
AddNodes(sortitems);
}
}
private List<string> SortItems(List<string> items, List<TableHeadItem> headTables)
{
var list = new List<string>();
foreach (TableHeadItem item in headTables)
{
string find = items.Find(s => s == item.Name);
if (find != null)
{
list.Add(find);
}
}
return list;
}
private void AddNodes(List<string> items)
{
int lenght = items.Count;
TreeNode parent = Tree;
for (int i = 0; i < lenght; i )
{
TreeNode node = parent.Nodes.Find(item => item.Name == items[i]);
if (node != null)
{
node.Count ;
}
else
{
node = new TreeNode(items[i]) { Parent = parent };
parent.Nodes.Add(node);
TableHeadItem headItem = HeadTables.Find(item => item.Name == items[i]);
headItem.AddHead(new HeadTreeItem(node));
}
parent = node;
}
}
private List<List<string>> Initialize()
{
var inputData = new List<List<string>>();
//inputData.Add(new List<string> { "A", });
//inputData.Add(new List<string> { "B", });
//inputData.Add(new List<string> { "A", "C" });
//inputData.Add(new List<string> { "A", "D", "L", "H", "O", "G", "P" });
//inputData.Add(new List<string> { "B", "C" });
//inputData.Add(new List<string> { "B", "D" });
inputData.Add(new List<string> { "A", "C", "D", "F", "G", "L", "M", "P" });
inputData.Add(new List<string> { "A", "B", "C", "F", "L", "M", "O", "P" });
inputData.Add(new List<string> { "B", "F", "L", "H", "O" });
inputData.Add(new List<string> { "B", "C", "K", "S", "N" });
inputData.Add(new List<string> { "A", "F", "C", "E", "L", "P", "M", "N" });
inputData.Add(new List<string> { "A", "B", "C", "F", "M", "P" });
inputData.Add(new List<string> { "O", "N" });
return inputData;
}
private void BuildHeads()
{
List<TableHeadItem> items = new List<TableHeadItem>();
foreach (var ts in InputData)
{
foreach (string t in ts)
{
TableHeadItem item = items.Find(o => o.Name == t);
if (item == null)
{
items.Add(new TableHeadItem { Name = t, Count = 1 });
}
else
{
item.Count ;
}
}
}
items.RemoveAll(item => item.Count < MinSupportCount);
items.Sort(
(item1, item2) =>
(item2.Count.CompareTo(item1.Count) == 0
? String.Compare(item1.Name, item2.Name, StringComparison.Ordinal)
: item2.Count.CompareTo(item1.Count)));
HeadTables = items;
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论