private TreeNode <int> CreateTree() { int x = int.Parse(Console.ReadLine()); if (x == -1) { // this means we should not create a tree return(null); } TreeNode <int> newTreeNode = new TreeNodeSpecial <int>(x); // Console.Write("Enter the left child of " + x + " " ); newTreeNode.SetLeft(CreateTree()); // Console.Write("Enter the right child of " + x + " "); newTreeNode.SetRight(CreateTree()); return(newTreeNode); }
// Create Tree Function public TreeNodeSpecial <T> CreateTree() { T x; string input = Console.ReadLine(); x = (T)Convert.ChangeType(input, typeof(T)); if ((int)(object)x == -1) { return(null); } TreeNodeSpecial <T> newTreeNode = new TreeNodeSpecial <T>(x); Console.WriteLine("Enter the left child of " + x); newTreeNode.SetLeft(CreateTree()); Console.WriteLine("Enter the right child of " + x); newTreeNode.SetRight(CreateTree()); return(newTreeNode); }