/// <summary> /// Creates the root of the Binary Search Tree and uses a Divide and Conquer, recursive algorithm to create /// the rest of the Tree in a balanced manner. /// </summary> /// <param name="scoringCategories"></param> /// <param name="min"></param> /// <param name="max"></param> public static void CreateRoot(ScoringCategories[] scoringCategories, int min, int max) { int mid = ((min + max) / 2); root = new ScoreCategoryNode(scoringCategories[mid], ScoringFunctions[mid]); root.left = InsertNode(scoringCategories, min, mid - 1, root); root.right = InsertNode(scoringCategories, mid + 1, max, root); }
/// <summary> /// A recursive, divide and conquer algorithm to insert nodes into the Binary Search Tree in a balanced fashion. /// </summary> /// <param name="scoringCategories"></param> /// <param name="min"></param> /// <param name="max"></param> /// <param name="currentNode"></param> /// <returns></returns> public static ScoreCategoryNode InsertNode(ScoringCategories[] scoringCategories, int min, int max, ScoreCategoryNode currentNode) { if (min > max) { return(null); } int mid = ((min + max) / 2); ScoreCategoryNode newNode = new ScoreCategoryNode(scoringCategories[mid], ScoringFunctions[mid], currentNode); newNode.left = InsertNode(scoringCategories, min, mid - 1, newNode); newNode.right = InsertNode(scoringCategories, mid + 1, max, newNode); newNode.parent = currentNode; return(newNode); }
/// <summary> /// Alternate constructor method /// </summary> /// <param name="name"></param> /// <param name="method"></param> /// <param name="parent"></param> public ScoreCategoryNode(ScoreCategoryBinaryTree.ScoringCategories name, Delegate method, ScoreCategoryNode parent) : this(name, method) { this.parent = parent; }
/// <summary> /// Constructor method /// </summary> public ScoreCategoryNode() { this.parent = null; this.left = null; this.right = null; }
/// <summary> /// Conducts a recursive, pre-order search of the Binary Search Tree for the ScoringCategories enum passed in /// and retrieves the associated ScoringDelegate. /// </summary> /// <param name="category"></param> /// <param name="currentNode"></param> /// <returns></returns> public static ScoringDelegate SearchScoringDelegates(ScoringCategories category, ScoreCategoryNode currentNode) { ScoreCategoryNode traversalNode; if (root == null) { throw new ArgumentException(); } else { if ((int)currentNode.name == (int)category) { calculationMethod = (ScoringDelegate)currentNode.method; } else if ((int)currentNode.name > (int)category) { traversalNode = currentNode.left; SearchScoringDelegates(category, traversalNode); } else if ((int)currentNode.name < (int)category) { traversalNode = currentNode.right; SearchScoringDelegates(category, traversalNode); } } return(calculationMethod); }