//-------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------- public void StoreItem(int item) { var row = CalculateRowNumber(++_count); var previousRow = row - 1; var plainItem = new PlainItem(item); if (_stairs.ContainsKey(row) == false) { _stairs[row] = new PlainIndexPlace(plainItem); if (_count == 1) { Root = plainItem; return; } _stairs[previousRow].First().Left = plainItem; return; } _stairs[row].Push(plainItem); var index = _stairs[row].Count; if (index == row) { _stairs[previousRow].Last().Right = plainItem; return; } var previousLeft = index - 1; _stairs[previousRow][previousLeft].Right = plainItem; _stairs[previousRow][index].Left = plainItem; }
//-------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------- public IEnumerable <Report> CountOccurences(PlainItem root) { var reports = new Dictionary <int, Report>(); Analyze(root, reports); return(reports.Values.OrderByDescending(x => x.Frequence)); }
public static bool HasChildren(this PlainItem item) { if (item == null) { return(false); } return(item.Right != null || item.Left != null); }
//-------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------- private void Analyze(PlainItem root, Dictionary <int, Report> reports, int sum = 0) { if (root.HasChildren() == false) { var totalSum = sum + root.Item; if (reports.ContainsKey(totalSum) == false) { reports[totalSum] = new Report { Frequence = 1, Sum = totalSum }; } else { reports[totalSum].Frequence++; } return; } Analyze(root.Left, reports, sum + root.Item); Analyze(root.Right, reports, sum + root.Item); }