private void buildWinnersBracket(BinaryTree <Set> winnersBracket) { if (winnersBracket == null || winnersBracket.node == null || winnersBracket.node.Round == 1) { return; } int round = winnersBracket.node.Round.Value; int winnerId = winnersBracket.node.WinnerID; int loserId = winnersBracket.node.LoserID; Set leftSet = null; Set rightSet = null; bool buildLeftBracket = true; bool buildRightBracket = true; leftSet = rounds[round - 1].Where(s => s.WinnerID == winnerId).FirstOrDefault(); rightSet = rounds[round - 1].Where(s => s.WinnerID == loserId).FirstOrDefault(); if (leftSet == null) { leftSet = rounds[losersRounds].First(); if (leftSet.WinnerID != winnerId) { leftSet = null; } else { buildLeftBracket = false; } } if (rightSet == null) { rightSet = rounds[losersRounds].First(); if (rightSet.WinnerID != loserId) { rightSet = null; } else { buildRightBracket = false; } } if (leftSet == null && buildLeftBracket) { leftSet = new Set() { WinnerID = winnerId, LoserID = 0, Round = round - 1 }; } if (rightSet == null && buildRightBracket) { rightSet = new Set() { WinnerID = loserId, LoserID = 0, Round = round - 1 }; } if (buildLeftBracket) { winnersBracket.SetLeftValue(leftSet); buildWinnersBracket(winnersBracket.leftChild); } if (buildRightBracket) { winnersBracket.SetRightValue(rightSet); buildWinnersBracket(winnersBracket.rightChild); } }
private void buildLosersBracket(BinaryTree <Set> bracket) { if (bracket == null || bracket.node == null || bracket.node.Round == -1) { return; } int round = bracket.node.Round.Value; int winnerId = bracket.node.WinnerID; int loserId = bracket.node.LoserID; bool buildLeftBracket = true; bool buildRightBracket = true; Set leftSet = null; Set rightSet = null; leftSet = rounds.ContainsKey(round + 1) ? rounds[round + 1].Where(s => s.WinnerID == winnerId).FirstOrDefault() : null; rightSet = rounds.ContainsKey(round + 1) ? rounds[round + 1].Where(s => s.WinnerID == loserId).FirstOrDefault() : null; if ((-1 * round) % 2 == 0) //even rounds have one child from winners, one from losers { if (leftSet == null && rightSet == null) { leftSet = new Set() { WinnerID = winnerId, LoserID = 0, IsWinners = false, Round = round + 1 }; } else if (leftSet == null && rightSet != null) { buildLeftBracket = false; } else if (leftSet != null && rightSet == null) { buildRightBracket = false; } else //leftSEt != null && rightSet != null { //??? } if (leftSet != null) { bracket.SetLeftValue(leftSet); } if (rightSet != null) { bracket.SetRightValue(rightSet); } } else //odd rounds have both children sets from losers { if (leftSet == null && rightSet == null) { leftSet = new Set() { WinnerID = winnerId, LoserID = 0, Round = round + 1, IsWinners = false }; rightSet = new Set() { WinnerID = winnerId, LoserID = 0, Round = round + 1, IsWinners = false }; } else if (leftSet == null && rightSet != null) { leftSet = new Set() { WinnerID = winnerId, LoserID = 0, Round = round + 1, IsWinners = false }; } else if (leftSet != null && rightSet == null) { rightSet = new Set() { WinnerID = winnerId, LoserID = 0, Round = round + 1, IsWinners = false }; } else //leftSEt != null && rightSet != null { } bracket.SetLeftValue(leftSet); bracket.SetRightValue(rightSet); } if (buildLeftBracket) { buildLosersBracket(bracket.leftChild); } if (buildRightBracket) { buildLosersBracket(bracket.rightChild); } }