// Constructor. public TalentTreeNode(string talentName, bool attained) { ability = talentName; achieved = attained; left = null; right = null; }
private void Insert(string ability, bool attained, TalentTreeNode current) { // If less than current value. if (String.Compare(ability, current.Ability) < 0) { // try to add node to the left if (current.Left == null) // space is available { current.Left = new TalentTreeNode(ability, attained); } else // space is already occupied { Insert(ability, attained, current.Left); // go down a level and do the same thing } } else // greater than or equal to current value, go right { // try to add the node to right if (current.Right == null) { current.Right = new TalentTreeNode(ability, attained); } else // space occupied { Insert(ability, attained, current.Right); } } }
// traverse the tree in order public void Traverse(TalentTreeNode current) { if (current != null) { Traverse(current.Left); Console.Write(current.Ability + " "); Traverse(current.Right); } }
// Insert methods. public void Insert(string ability, bool attained) { if (root == null) { root = new TalentTreeNode(ability, attained); } else { Insert(ability, attained, root); } }
public static void Main(string[] args) { // Create the TalentTree. TalentTree myTalentTree = new TalentTree(); // Populate the TalentTree manually. TalentTreeNode root = new TalentTreeNode("Magika", true); TalentTreeNode level2_left = new TalentTreeNode("Blizzard", true); TalentTreeNode level2_right = new TalentTreeNode("Fire", true); root.Left = level2_left; root.Right = level2_right; TalentTreeNode level3_left1 = new TalentTreeNode("Icicle", false); TalentTreeNode level3_right1 = new TalentTreeNode("Blizzaga", false); level2_left.Left = level3_left1; level2_left.Right = level3_right1; TalentTreeNode level3_left2 = new TalentTreeNode("Pyro", false); TalentTreeNode level3_right2 = new TalentTreeNode("Firaga", true); level2_right.Left = level3_left2; level2_right.Right = level3_right2; myTalentTree.Root = root; Console.WriteLine("List of Known Abilities:"); Console.WriteLine(myTalentTree.ListKnownAbilities(myTalentTree.Root)); Console.WriteLine(); Console.ReadLine(); Console.WriteLine("List of Possible Abilities:"); Console.WriteLine(myTalentTree.ListPossibleAbilities(myTalentTree.Root)); Console.WriteLine(); Console.ReadLine(); Console.WriteLine("Traversal of Talent Tree:"); myTalentTree.Traverse(myTalentTree.Root); Console.WriteLine(); Console.ReadLine(); }
// List Possible Abilities. // Prints out abilities that the player could learn next. public string ListPossibleAbilities(TalentTreeNode node) { string list = ""; // If not achieved none of its children can be achieved either. // So do not return a value for this. if (node == null || !node.Achieved) { list += ""; } // If it IS achieved: else { // Check to see if its left children nodes can also be added if they aren't null and ARE achieved. if (node.Left != null && node.Left.Achieved) { list += ListPossibleAbilities(node.Left); } else if (node.Left != null && !node.Left.Achieved) { list += node.Left.Ability + " "; } // Check to see if its right children nodes can also be added if they aren't null and ARE achieved. if (node.Right != null && node.Right.Achieved) { list += ListPossibleAbilities(node.Right); } else if (node.Right != null && !node.Right.Achieved) { list += node.Right.Ability + " "; } } // Returns whatever may come out of the recursive process. return(list); }