/// <summary> /// Uses LinearSearch from C++ DLL to find a certain score within the sorted scores array, and then gets the relevant data object from the binary tree (as the index of the array == the index of the binary tree). /// </summary> /// <param name="score"></param> private void SearchByScore(int score) { string query = searchInput.text; int arrIndex = HighScores.LinearSearch(scores.scores, scores.scores.Length, score); if (arrIndex != -1) { int sortedIndex = (scores.scores.Length - 1) - arrIndex; BinaryTree.BinaryTreeNode node = scores.dataTree.Find(sortedIndex); if (node != null) { GameObject newRecord = Instantiate(recordPrefab, resultsContent.transform); newRecord.GetComponent <TextMeshProUGUI>().text = $"Rank #{sortedIndex + 1} - Name: {node.data.name} || Score: {node.data.score} || Total Time: {node.data.seconds} seconds"; SwitchBoard(Leaderboard.Results); } else { EventManager.TriggerEvent("NoResults"); } } else { EventManager.TriggerEvent("NoResults"); } }
/// <summary> /// Searches the binary tree for a given rank. /// </summary> /// <param name="rank">The rank number to searched for.</param> public void GetRankFromBinaryTree(int rank) { BinaryTree.BinaryTreeNode result = dataTree.Find(rank - 1); if (result != null) { Debug.Log($"Result returned for rank #{rank}: [Name: {result.data.name}, Score: {result.data.score}, Total Time: {result.data.seconds} seconds, Record Last Updated: {result.data.date}]"); } }
public static int _01_GetNodeHeight(BinaryTree.BinaryTreeNode node) { if (node.Left == null && node.Right == null) { return(0); } return(1 + Math.Max( node.Left != null ? _01_GetNodeHeight(node.Left) : 0, node.Right != null ? _01_GetNodeHeight(node.Right) : 0 )); }
/// <summary> /// Traverses the passed LinkedList (in reverse order) and converts this into rankings. The index at which a record appears is its ranking. /// </summary> /// <param name="data">The LinkedList to be sorted into the binary tree.</param> /// <param name="tree">The BinaryTree for the LinkedList to be sorted into.</param> public void StoreDataInBinaryTree(LinkedList <DreamloData.Dreamlo.Leaderboard.HighScoreRecord> data, BinaryTree tree) { LinkedListNode <DreamloData.Dreamlo.Leaderboard.HighScoreRecord> currentNode = data.Last; for (int i = 0; i < data.Count; i++) { BinaryTree.BinaryTreeNode node = new BinaryTree.BinaryTreeNode(); node.index = i; node.data = currentNode.Value; tree.CreateNode(node); currentNode = currentNode.Previous; } }
/// <summary> /// Searches the binary tree for the rank the user has submitted. This equals the index of the binary tree - 1, as the binary tree is zero-based. /// </summary> /// <param name="rank">The rank being searched for within the binary tree.</param> private void SearchByRank(int rank) { BinaryTree.BinaryTreeNode result = scores.dataTree.Find(rank - 1); if (result != null) { GameObject newRecord = Instantiate(recordPrefab, resultsContent.transform); newRecord.GetComponent <TextMeshProUGUI>().text = $"Rank #{result.index + 1} - Name: {result.data.name} || Score: {result.data.score} || Total Time: {result.data.seconds} seconds"; SwitchBoard(Leaderboard.Results); } else { EventManager.TriggerEvent("NoResults"); } }
public static bool _01_IsBalancedNode(BinaryTree.BinaryTreeNode node) { if (node.Left == null && node.Right == null) { return(true); } int leftHeight = node.Left != null?_01_GetNodeHeight(node.Left) : 0; int rightHeight = node.Right != null?_01_GetNodeHeight(node.Right) : 0; if (Math.Abs(leftHeight - rightHeight) > 1) { return(false); } bool leftSubTreeBalanced = node.Left == null || _01_IsBalancedNode(node.Left); bool rightSubTreeBalanced = node.Right == null || _01_IsBalancedNode(node.Right); return(leftSubTreeBalanced && rightSubTreeBalanced); }